1 /* Copyright (c) 2000, 2020, 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 GROUP_PARSER Parser
25 @{
26 */
27
28 #ifndef SQL_LEX_INCLUDED
29 #define SQL_LEX_INCLUDED
30
31 #include <string.h>
32 #include <sys/types.h>
33 #include <algorithm>
34 #include <map>
35 #include <memory>
36 #include <new>
37 #include <string>
38 #include <type_traits>
39 #include <utility>
40
41 #include "lex_string.h"
42 #include "m_ctype.h"
43 #include "m_string.h"
44 #include "map_helpers.h"
45 #include "mem_root_deque.h"
46 #include "memory_debugging.h"
47 #include "my_base.h"
48 #include "my_compiler.h"
49 #include "my_dbug.h"
50 #include "my_inttypes.h"
51 #include "my_sqlcommand.h"
52 #include "my_sys.h"
53 #include "my_table_map.h"
54 #include "my_thread_local.h"
55 #include "my_time.h"
56 #include "mysql/components/services/psi_statement_bits.h"
57 #include "mysql/psi/psi_base.h"
58 #include "mysql_com.h"
59 #include "mysqld_error.h"
60 #include "prealloced_array.h" // Prealloced_array
61 #include "sql/composite_iterators.h"
62 #include "sql/dd/info_schema/table_stats.h" // dd::info_schema::Table_stati...
63 #include "sql/dd/info_schema/tablespace_stats.h" // dd::info_schema::Tablesp...
64 #include "sql/enum_query_type.h"
65 #include "sql/field.h"
66 #include "sql/handler.h"
67 #include "sql/item.h" // Name_resolution_context
68 #include "sql/key_spec.h" // KEY_CREATE_INFO
69 #include "sql/lex_symbol.h" // LEX_SYMBOL
70 #include "sql/lexer_yystype.h" // Lexer_yystype
71 #include "sql/mdl.h"
72 #include "sql/mem_root_array.h" // Mem_root_array
73 #include "sql/opt_hints.h"
74 #include "sql/parse_tree_hints.h"
75 #include "sql/parse_tree_node_base.h" // enum_parsing_context
76 #include "sql/parser_yystype.h"
77 #include "sql/query_options.h" // OPTION_NO_CONST_TABLES
78 #include "sql/row_iterator.h"
79 #include "sql/set_var.h"
80 #include "sql/sql_alter.h" // Alter_info
81 #include "sql/sql_array.h"
82 #include "sql/sql_connect.h" // USER_RESOURCES
83 #include "sql/sql_const.h"
84 #include "sql/sql_data_change.h" // enum_duplicates
85 #include "sql/sql_list.h"
86 #include "sql/sql_plugin_ref.h"
87 #include "sql/sql_servers.h" // Server_options
88 #include "sql/sql_udf.h" // Item_udftype
89 #include "sql/table.h" // TABLE_LIST
90 #include "sql/thr_malloc.h"
91 #include "sql/trigger_def.h" // enum_trigger_action_time_type
92 #include "sql_chars.h"
93 #include "sql_string.h"
94 #include "thr_lock.h" // thr_lock_type
95 #include "violite.h" // SSL_type
96
97 class Item_cond;
98 class Item_exists_subselect;
99 class Item_subselect;
100 class Item_sum;
101 class Item_rollup_group_item;
102 class Item_rollup_sum_switcher;
103 class Event_parse_data;
104 class Item_func_match;
105 class Parse_tree_root;
106 class Query_result_interceptor;
107 class Window;
108 class sp_pcontext;
109 struct sql_digest_state;
110
111 const size_t INITIAL_LEX_PLUGIN_LIST_SIZE = 16;
112
113 /*
114 There are 8 different type of table access so there is no more than
115 combinations 2^8 = 256:
116
117 . STMT_READS_TRANS_TABLE
118
119 . STMT_READS_NON_TRANS_TABLE
120
121 . STMT_READS_TEMP_TRANS_TABLE
122
123 . STMT_READS_TEMP_NON_TRANS_TABLE
124
125 . STMT_WRITES_TRANS_TABLE
126
127 . STMT_WRITES_NON_TRANS_TABLE
128
129 . STMT_WRITES_TEMP_TRANS_TABLE
130
131 . STMT_WRITES_TEMP_NON_TRANS_TABLE
132
133 The unsafe conditions for each combination is represented within a byte
134 and stores the status of the option --binlog-direct-non-trans-updates,
135 whether the trx-cache is empty or not, and whether the isolation level
136 is lower than ISO_REPEATABLE_READ:
137
138 . option (OFF/ON)
139 . trx-cache (empty/not empty)
140 . isolation (>= ISO_REPEATABLE_READ / < ISO_REPEATABLE_READ)
141
142 bits 0 : . OFF, . empty, . >= ISO_REPEATABLE_READ
143 bits 1 : . OFF, . empty, . < ISO_REPEATABLE_READ
144 bits 2 : . OFF, . not empty, . >= ISO_REPEATABLE_READ
145 bits 3 : . OFF, . not empty, . < ISO_REPEATABLE_READ
146 bits 4 : . ON, . empty, . >= ISO_REPEATABLE_READ
147 bits 5 : . ON, . empty, . < ISO_REPEATABLE_READ
148 bits 6 : . ON, . not empty, . >= ISO_REPEATABLE_READ
149 bits 7 : . ON, . not empty, . < ISO_REPEATABLE_READ
150 */
151 extern uint binlog_unsafe_map[256];
152 /*
153 Initializes the array with unsafe combinations and its respective
154 conditions.
155 */
156 void binlog_unsafe_map_init();
157
158 /*
159 If we encounter a diagnostics statement (GET DIAGNOSTICS, or e.g.
160 the old SHOW WARNINGS|ERRORS, or "diagnostics variables" such as
161 @@warning_count | @@error_count, we'll set some hints so this
162 information is not lost. DA_KEEP_UNSPECIFIED is used in LEX constructor to
163 avoid leaving variables uninitialized.
164 */
165 enum enum_keep_diagnostics {
166 DA_KEEP_NOTHING = 0, /**< keep nothing */
167 DA_KEEP_DIAGNOSTICS, /**< keep the diagnostics area */
168 DA_KEEP_COUNTS, /**< keep \@warning_count / \@error_count */
169 DA_KEEP_PARSE_ERROR, /**< keep diagnostics area after parse error */
170 DA_KEEP_UNSPECIFIED /**< keep semantics is unspecified */
171 };
172
173 enum enum_sp_suid_behaviour {
174 SP_IS_DEFAULT_SUID = 0,
175 SP_IS_NOT_SUID,
176 SP_IS_SUID
177 };
178
179 enum enum_sp_data_access {
180 SP_DEFAULT_ACCESS = 0,
181 SP_CONTAINS_SQL,
182 SP_NO_SQL,
183 SP_READS_SQL_DATA,
184 SP_MODIFIES_SQL_DATA
185 };
186
187 /**
188 enum_sp_type defines type codes of stored programs.
189
190 @note these codes are used when dealing with the mysql.routines system table,
191 so they must not be changed.
192
193 @note the following macros were used previously for the same purpose. Now they
194 are used for ACL only.
195 */
196 enum class enum_sp_type { FUNCTION = 1, PROCEDURE, TRIGGER, EVENT };
197
to_sp_type(longlong val)198 inline enum_sp_type to_sp_type(longlong val) {
199 DBUG_ASSERT(val >= 1 && val <= 4);
200 return static_cast<enum_sp_type>(val);
201 }
202
to_longlong(enum_sp_type val)203 inline longlong to_longlong(enum_sp_type val) {
204 return static_cast<longlong>(val);
205 }
206
to_uint(enum_sp_type val)207 inline uint to_uint(enum_sp_type val) { return static_cast<uint>(val); }
208
209 /*
210 Values for the type enum. This reflects the order of the enum declaration
211 in the CREATE TABLE command. These values are used to enumerate object types
212 for the ACL statements.
213
214 These values were also used for enumerating stored program types. However, now
215 enum_sp_type should be used for that instead of them.
216 */
217 #define TYPE_ENUM_FUNCTION 1
218 #define TYPE_ENUM_PROCEDURE 2
219 #define TYPE_ENUM_TRIGGER 3
220 #define TYPE_ENUM_PROXY 4
221
222 enum class Acl_type {
223 TABLE = 0,
224 FUNCTION = TYPE_ENUM_FUNCTION,
225 PROCEDURE = TYPE_ENUM_PROCEDURE,
226 };
227
228 const LEX_CSTRING sp_data_access_name[] = {
229 {STRING_WITH_LEN("")},
230 {STRING_WITH_LEN("CONTAINS SQL")},
231 {STRING_WITH_LEN("NO SQL")},
232 {STRING_WITH_LEN("READS SQL DATA")},
233 {STRING_WITH_LEN("MODIFIES SQL DATA")}};
234
235 enum class enum_view_create_mode {
236 VIEW_CREATE_NEW, // check that there are not such VIEW/table
237 VIEW_ALTER, // check that VIEW with such name exists
238 VIEW_CREATE_OR_REPLACE // check only that there are not such table
239 };
240
241 enum class enum_alter_user_attribute {
242 ALTER_USER_COMMENT_NOT_USED, // No user metadata ALTER in the AST
243 ALTER_USER_COMMENT, // A text comment is expected
244 ALTER_USER_ATTRIBUTE // A JSON object is expected
245 };
246
247 /* Options to add_table_to_list() */
248 #define TL_OPTION_UPDATING 1
249 #define TL_OPTION_FORCE_INDEX 2
250 #define TL_OPTION_IGNORE_LEAVES 4
251 #define TL_OPTION_ALIAS 8
252
253 /* Structure for db & table in sql_yacc */
254 class Table_function;
255
256 class Table_ident {
257 public:
258 LEX_CSTRING db;
259 LEX_CSTRING table;
260 SELECT_LEX_UNIT *sel;
261 Table_function *table_function;
262
263 Table_ident(Protocol *protocol, const LEX_CSTRING &db_arg,
264 const LEX_CSTRING &table_arg, bool force);
Table_ident(const LEX_CSTRING & db_arg,const LEX_CSTRING & table_arg)265 Table_ident(const LEX_CSTRING &db_arg, const LEX_CSTRING &table_arg)
266 : db(db_arg), table(table_arg), sel(nullptr), table_function(nullptr) {}
Table_ident(const LEX_CSTRING & table_arg)267 Table_ident(const LEX_CSTRING &table_arg)
268 : table(table_arg), sel(nullptr), table_function(nullptr) {
269 db = NULL_CSTR;
270 }
271 /**
272 This constructor is used only for the case when we create a derived
273 table. A derived table has no name and doesn't belong to any database.
274 Later, if there was an alias specified for the table, it will be set
275 by add_table_to_list.
276 */
Table_ident(SELECT_LEX_UNIT * s)277 Table_ident(SELECT_LEX_UNIT *s) : sel(s), table_function(nullptr) {
278 db = EMPTY_CSTR; /* a subject to casedn_str */
279 table = EMPTY_CSTR;
280 }
281 /*
282 This constructor is used only for the case when we create a table function.
283 It has no name and doesn't belong to any database as it exists only
284 during query execution. Later, if there was an alias specified for the
285 table, it will be set by add_table_to_list.
286 */
Table_ident(LEX_CSTRING & table_arg,Table_function * table_func_arg)287 Table_ident(LEX_CSTRING &table_arg, Table_function *table_func_arg)
288 : table(table_arg), sel(nullptr), table_function(table_func_arg) {
289 /* We must have a table name here as this is used with add_table_to_list */
290 db = EMPTY_CSTR; /* a subject to casedn_str */
291 }
292 // True if we can tell from syntax that this is a table function.
is_table_function()293 bool is_table_function() const { return (table_function != nullptr); }
294 // True if we can tell from syntax that this is an unnamed derived table.
is_derived_table()295 bool is_derived_table() const { return sel; }
change_db(const char * db_name)296 void change_db(const char *db_name) {
297 db.str = db_name;
298 db.length = strlen(db_name);
299 }
300 };
301
302 typedef List<Item> List_item;
303 typedef Mem_root_array<ORDER *> Group_list_ptrs;
304
305 /**
306 Structure to hold parameters for CHANGE MASTER, START SLAVE, and STOP SLAVE.
307
308 Remark: this should not be confused with Master_info (and perhaps
309 would better be renamed to st_lex_replication_info). Some fields,
310 e.g., delay, are saved in Relay_log_info, not in Master_info.
311 */
312 struct LEX_MASTER_INFO {
313 /*
314 The array of IGNORE_SERVER_IDS has a preallocation, and is not expected
315 to grow to any significant size, so no instrumentation.
316 */
LEX_MASTER_INFOLEX_MASTER_INFO317 LEX_MASTER_INFO() : repl_ignore_server_ids(PSI_NOT_INSTRUMENTED) {
318 initialize();
319 }
320 char *host, *user, *password, *log_file_name, *bind_addr, *network_namespace;
321 uint port, connect_retry;
322 float heartbeat_period;
323 int sql_delay;
324 ulonglong pos;
325 ulong server_id, retry_count;
326 char *gtid;
327 char *view_id;
328 const char *channel; // identifier similar to database name
329 enum {
330 UNTIL_SQL_BEFORE_GTIDS = 0,
331 UNTIL_SQL_AFTER_GTIDS
332 } gtid_until_condition;
333 bool until_after_gaps;
334 bool slave_until;
335 bool for_channel;
336
337 /*
338 Enum is used for making it possible to detect if the user
339 changed variable or if it should be left at old value
340 */
341 enum {
342 LEX_MI_UNCHANGED = 0,
343 LEX_MI_DISABLE,
344 LEX_MI_ENABLE
345 } ssl,
346 ssl_verify_server_cert, heartbeat_opt, repl_ignore_server_ids_opt,
347 retry_count_opt, auto_position, port_opt, get_public_key;
348 char *ssl_key, *ssl_cert, *ssl_ca, *ssl_capath, *ssl_cipher;
349 char *ssl_crl, *ssl_crlpath, *tls_version;
350 /*
351 Ciphersuites used for TLS 1.3 communication with the master server.
352 */
353 enum enum_tls_ciphersuites {
354 UNSPECIFIED = 0,
355 SPECIFIED_NULL,
356 SPECIFIED_STRING
357 };
358 enum enum_tls_ciphersuites tls_ciphersuites;
359 char *tls_ciphersuites_string;
360 char *public_key_path;
361 char *relay_log_name;
362 ulong relay_log_pos;
363 char *compression_algorithm;
364 uint zstd_compression_level;
365 Prealloced_array<ulong, 2> repl_ignore_server_ids;
366 /**
367 Flag that is set to `true` whenever `PRIVILEGE_CHECKS_USER` is set to `NULL`
368 as a part of a `CHANGE MASTER TO` statement.
369 */
370 bool privilege_checks_none;
371 /**
372 Username and hostname parts of the `PRIVILEGE_CHECKS_USER`, when it's set to
373 a user.
374 */
375 const char *privilege_checks_username, *privilege_checks_hostname;
376 /**
377 Flag indicating if row format should be enforced for this channel event
378 stream.
379 */
380 int require_row_format;
381
382 /**
383 Identifies what is the slave policy on primary keys in tables.
384 If set to STREAM it just replicates the value of sql_require_primary_key.
385 If set to ON it fails when the source tries to replicate a table creation
386 or alter operation that does not have a primary key.
387 If set to OFF it does not enforce any policies on the channel for primary
388 keys.
389 */
390 enum {
391 LEX_MI_PK_CHECK_UNCHANGED = 0,
392 LEX_MI_PK_CHECK_STREAM = 1,
393 LEX_MI_PK_CHECK_ON = 2,
394 LEX_MI_PK_CHECK_OFF = 3
395 } require_table_primary_key_check;
396
397 /// Initializes everything to zero/NULL/empty.
398 void initialize();
399 /// Sets all fields to their "unspecified" value.
400 void set_unspecified();
401
402 private:
403 // Not copyable or assignable.
404 LEX_MASTER_INFO(const LEX_MASTER_INFO &);
405 LEX_MASTER_INFO &operator=(const LEX_MASTER_INFO &);
406 };
407
408 struct LEX_RESET_SLAVE {
409 bool all;
410 };
411
412 enum sub_select_type {
413 UNSPECIFIED_TYPE,
414 UNION_TYPE,
415 INTERSECT_TYPE,
416 EXCEPT_TYPE,
417 GLOBAL_OPTIONS_TYPE,
418 DERIVED_TABLE_TYPE,
419 OLAP_TYPE
420 };
421
422 /*
423 String names used to print a statement with index hints.
424 Keep in sync with index_hint_type.
425 */
426 extern const char *index_hint_type_name[];
427 typedef uchar index_clause_map;
428
429 /*
430 Bits in index_clause_map : one for each possible FOR clause in
431 USE/FORCE/IGNORE INDEX index hint specification
432 */
433 #define INDEX_HINT_MASK_JOIN (1)
434 #define INDEX_HINT_MASK_GROUP (1 << 1)
435 #define INDEX_HINT_MASK_ORDER (1 << 2)
436
437 #define INDEX_HINT_MASK_ALL \
438 (INDEX_HINT_MASK_JOIN | INDEX_HINT_MASK_GROUP | INDEX_HINT_MASK_ORDER)
439
440 /* Single element of an USE/FORCE/IGNORE INDEX list specified as a SQL hint */
441 class Index_hint {
442 public:
443 /* The type of the hint : USE/FORCE/IGNORE */
444 enum index_hint_type type;
445 /* Where the hit applies to. A bitmask of INDEX_HINT_MASK_<place> values */
446 index_clause_map clause;
447 /*
448 The index name. Empty (str=NULL) name represents an empty list
449 USE INDEX () clause
450 */
451 LEX_CSTRING key_name;
452
Index_hint(const char * str,uint length)453 Index_hint(const char *str, uint length) {
454 key_name.str = str;
455 key_name.length = length;
456 }
457
458 void print(const THD *thd, String *str);
459 };
460
461 /*
462 Class SELECT_LEX_UNIT represents a query expression.
463 Class SELECT_LEX represents a query block.
464 A query expression contains one or more query blocks (more than one means
465 that we have a UNION query).
466 These classes are connected as follows:
467 Both classes have a master, a slave, a next and a prev field.
468 For class SELECT_LEX, master and slave connect to objects of type
469 SELECT_LEX_UNIT, whereas for class SELECT_LEX_UNIT, they connect
470 to SELECT_LEX.
471 master is pointer to outer node.
472 slave is pointer to the first inner node
473
474 neighbors are two SELECT_LEX or SELECT_LEX_UNIT objects on
475 the same level.
476
477 The structures are linked with the following pointers:
478 - list of neighbors (next/prev) (prev of first element point to slave
479 pointer of outer structure)
480 - For SELECT_LEX, this is a list of query blocks.
481 - For SELECT_LEX_UNIT, this is a list of subqueries.
482
483 - pointer to outer node (master), which is
484 If this is SELECT_LEX_UNIT
485 - pointer to outer select_lex.
486 If this is SELECT_LEX
487 - pointer to outer SELECT_LEX_UNIT.
488
489 - pointer to inner objects (slave), which is either:
490 If this is an SELECT_LEX_UNIT:
491 - first query block that belong to this query expression.
492 If this is an SELECT_LEX
493 - first query expression that belong to this query block (subqueries).
494
495 - list of all SELECT_LEX objects (link_next/link_prev)
496 This is to be used for things like derived tables creation, where we
497 go through this list and create the derived tables.
498
499 If query expression contain several query blocks (UNION now,
500 INTERSECT etc later) then it has a special select_lex called
501 fake_select_lex. It used for storing global parameters (like ORDER BY,
502 LIMIT) and executing union.
503 Subqueries used in global ORDER BY clause will be attached to this
504 fake_select_lex, which will allow them to correctly resolve fields of
505 the containing UNION and outer selects.
506
507 For example for following query:
508
509 select *
510 from table1
511 where table1.field IN (select * from table1_1_1 union
512 select * from table1_1_2)
513 union
514 select *
515 from table2
516 where table2.field=(select (select f1 from table2_1_1_1_1
517 where table2_1_1_1_1.f2=table2_1_1.f3)
518 from table2_1_1
519 where table2_1_1.f1=table2.f2)
520 union
521 select * from table3;
522
523 we will have following structure:
524
525 select1: (select * from table1 ...)
526 select2: (select * from table2 ...)
527 select3: (select * from table3)
528 select1.1.1: (select * from table1_1_1)
529 ...
530
531 main unit
532 fake0
533 select1 select2 select3
534 |^^ |^
535 s||| ||master
536 l||| |+---------------------------------+
537 a||| +---------------------------------+|
538 v|||master slave ||
539 e||+-------------------------+ ||
540 V| neighbor | V|
541 unit1.1<+==================>unit1.2 unit2.1
542 fake1.1
543 select1.1.1 select 1.1.2 select1.2.1 select2.1.1
544 |^
545 ||
546 V|
547 unit2.1.1.1
548 select2.1.1.1.1
549
550
551 relation in main unit will be following:
552 (bigger picture for:
553 main unit
554 fake0
555 select1 select2 select3
556 in the above picture)
557
558 main unit
559 |^^^^|fake_select_lex
560 |||||+--------------------------------------------+
561 ||||+--------------------------------------------+|
562 |||+------------------------------+ ||
563 ||+--------------+ | ||
564 slave||master | | ||
565 V| neighbor | neighbor | master|V
566 select1<========>select2<========>select3 fake0
567
568 list of all select_lex will be following (as it will be constructed by
569 parser):
570
571 select1->select2->select3->select2.1.1->select 2.1.2->select2.1.1.1.1-+
572 |
573 +---------------------------------------------------------------------+
574 |
575 +->select1.1.1->select1.1.2
576
577 */
578
579 class JOIN;
580 class PT_with_clause;
581 class Query_result;
582 class Query_result_union;
583 class RowIterator;
584 struct LEX;
585
586 /**
587 This class represents a query expression (one query block or
588 several query blocks combined with UNION).
589 */
590 class SELECT_LEX_UNIT {
591 /**
592 Intrusive double-linked list of all query expressions
593 immediately contained within the same query block.
594 */
595 SELECT_LEX_UNIT *next;
596 SELECT_LEX_UNIT **prev;
597
598 /**
599 The query block wherein this query expression is contained,
600 NULL if the query block is the outer-most one.
601 */
602 SELECT_LEX *master;
603 /// The first query block in this query expression.
604 SELECT_LEX *slave;
605
606 private:
607 /**
608 Marker for subqueries in WHERE, HAVING, ORDER BY, GROUP BY and
609 SELECT item lists.
610 Must be read/written when holding LOCK_query_plan.
611
612 See Item_subselect::explain_subquery_checker
613 */
614 enum_parsing_context explain_marker;
615
616 bool prepared; ///< All query blocks in query expression are prepared
617 bool optimized; ///< All query blocks in query expression are optimized
618 bool executed; ///< Query expression has been executed
619
620 TABLE_LIST result_table_list{};
621 Query_result_union *union_result;
622 /// Temporary table using for appending UNION results.
623 /// Not used if we materialize directly into a parent query expression's
624 /// result table (see optimize()).
625 TABLE *table;
626 /// Object to which the result for this query expression is sent.
627 /// Not used if we materialize directly into a parent query expression's
628 /// result table (see optimize()).
629 Query_result *m_query_result;
630
631 /**
632 An iterator you can read from to get all records for this query.
633
634 May be nullptr even after create_iterators(), or in the case of an
635 unfinished materialization (see optimize()).
636 */
637 unique_ptr_destroy_only<RowIterator> m_root_iterator;
638
639 /**
640 If there is an unfinished materialization (see optimize()),
641 contains one element for each query block in this query expression.
642 */
643 Mem_root_array<MaterializeIterator::QueryBlock> m_query_blocks_to_materialize;
644
645 /**
646 Sets up each query block in this query expression for materialization
647 into the given table.
648
649 @param thd thread handle
650 @param dst_table the table to materialize into
651 @param union_distinct_only if true, keep only UNION DISTINCT query blocks
652 (any UNION ALL blocks are presumed handled higher up, by AppendIterator)
653 */
654 Mem_root_array<MaterializeIterator::QueryBlock> setup_materialization(
655 THD *thd, TABLE *dst_table, bool union_distinct_only);
656
657 /**
658 If possible, convert the executor structures to a set of row iterators,
659 storing the result in m_root_iterator. If not, m_root_iterator will remain
660 nullptr.
661 */
662 void create_iterators(THD *thd);
663
664 public:
665 /**
666 result of this query can't be cached, bit field, can be :
667 UNCACHEABLE_DEPENDENT
668 UNCACHEABLE_RAND
669 UNCACHEABLE_SIDEEFFECT
670 */
671 uint8 uncacheable;
672
673 explicit SELECT_LEX_UNIT(enum_parsing_context parsing_context);
674
675 /// @return true for a query expression without UNION or multi-level ORDER
is_simple()676 bool is_simple() const { return !(is_union() || fake_select_lex); }
677
678 /// Values for SELECT_LEX_UNIT::cleaned
679 enum enum_clean_state {
680 UC_DIRTY, ///< Unit isn't cleaned
681 UC_PART_CLEAN, ///< Unit were cleaned, except JOIN and JOIN_TABs were
682 ///< kept for possible EXPLAIN
683 UC_CLEAN ///< Unit completely cleaned, all underlying JOINs were
684 ///< freed
685 };
686 enum_clean_state cleaned; ///< cleanliness state
687
688 // list of fields which points to temporary table for union
689 List<Item> item_list{};
690 /*
691 list of types of items inside union (used for union & derived tables)
692
693 Item_type_holders from which this list consist may have pointers to Field,
694 pointers is valid only after preparing SELECTS of this unit and before
695 any SELECT of this unit execution
696
697 TODO:
698 Possibly this member should be protected, and its direct use replaced
699 by get_unit_column_types(). Check the places where it is used.
700 */
701 List<Item> types;
702
703 /**
704 Pointer to query block containing global parameters for query.
705 Global parameters may include ORDER BY, LIMIT and OFFSET.
706
707 If this is a union of multiple query blocks, the global parameters are
708 stored in fake_select_lex. If the union doesn't use a temporary table,
709 SELECT_LEX_UNIT::prepare() nulls out fake_select_lex, but saves a copy
710 in saved_fake_select_lex in order to preserve the global parameters.
711
712 If this is not a union, and the query expression has no multi-level
713 ORDER BY/LIMIT, global parameters are in the single query block.
714
715 @return query block containing the global parameters
716 */
global_parameters()717 inline SELECT_LEX *global_parameters() const {
718 if (fake_select_lex != nullptr)
719 return fake_select_lex;
720 else if (saved_fake_select_lex != nullptr)
721 return saved_fake_select_lex;
722 return first_select();
723 }
724 /* LIMIT clause runtime counters */
725 ha_rows select_limit_cnt, offset_limit_cnt;
726 /// Points to subquery if this query expression is used in one, otherwise NULL
727 Item_subselect *item;
728 /**
729 Used by transform_scalar_subqueries_to_join_with_derived to remember where
730 we came from, i.e. the place value of SELECT_LEX_UNIT::item. This is needed
731 because at EXECUTE time, we re-resolve the subquery that was transformed
732 into a derived table during PREPARE, in the position where it was originally
733 located, i.e. in the SELECT list. For this resolution to work, we need the
734 correct place information. This helps us get that, cf.
735 SELECT_LEX_UNIT::place. Can be removed after WL#6570.
736 */
737 enum_parsing_context m_place_before_transform{CTX_NONE};
738 /**
739 Helper query block for query expression with UNION or multi-level
740 ORDER BY/LIMIT
741 */
742 SELECT_LEX *fake_select_lex;
743 /**
744 SELECT_LEX that stores LIMIT and OFFSET for UNION ALL when no
745 fake_select_lex is used.
746 */
747 SELECT_LEX *saved_fake_select_lex;
748 /**
749 Points to last query block which has UNION DISTINCT on its left.
750 In a list of UNIONed blocks, UNION is left-associative; so UNION DISTINCT
751 eliminates duplicates in all blocks up to the first one on its right
752 included. Which is why we only need to remember that query block.
753 */
754 SELECT_LEX *union_distinct;
755
756 /**
757 The WITH clause which is the first part of this query expression. NULL if
758 none.
759 */
760 PT_with_clause *m_with_clause;
761 /**
762 If this query expression is underlying of a derived table, the derived
763 table. NULL if none.
764 */
765 TABLE_LIST *derived_table;
766 /**
767 First query block (in this UNION) which references the CTE.
768 NULL if not the query expression of a recursive CTE.
769 */
770 SELECT_LEX *first_recursive;
771
772 /**
773 If 'this' is body of lateral derived table:
774 map of tables in the same FROM clause as this derived table, and to which
775 the derived table's body makes references.
776 In pre-resolution stages, this is OUTER_REF_TABLE_BIT, just to indicate
777 that this has LATERAL; after resolution, which has found references in the
778 body, this is the proper map (with no PSEUDO_TABLE_BITS anymore).
779 */
780 table_map m_lateral_deps;
781 /**
782 True if the with-recursive algorithm has produced the complete result.
783 In a recursive CTE, a JOIN is executed several times in a loop, and
784 should not be cleaned up (e.g. by join_free()) before all iterations of
785 the loop are done (i.e. before the CTE's result is complete).
786 */
787 bool got_all_recursive_rows;
788
789 /// @return true if query expression can be merged into an outer query
790 bool is_mergeable() const;
791
792 /// @return true if query expression is recommended to be merged
793 bool merge_heuristic(const LEX *lex) const;
794
795 /// @return the query block this query expression belongs to as subquery
outer_select()796 SELECT_LEX *outer_select() const { return master; }
797
798 /// @return the first query block inside this query expression
first_select()799 SELECT_LEX *first_select() const { return slave; }
800
801 /// @return the next query expression within same query block (next subquery)
next_unit()802 SELECT_LEX_UNIT *next_unit() const { return next; }
803
804 /// @return the query result object in use for this query expression
query_result()805 Query_result *query_result() const { return m_query_result; }
806
root_iterator()807 RowIterator *root_iterator() const { return m_root_iterator.get(); }
release_root_iterator()808 unique_ptr_destroy_only<RowIterator> release_root_iterator() {
809 return move(m_root_iterator);
810 }
811
812 /// See optimize().
unfinished_materialization()813 bool unfinished_materialization() const {
814 return !m_query_blocks_to_materialize.empty();
815 }
816
817 /// See optimize().
818 Mem_root_array<MaterializeIterator::QueryBlock>
release_query_blocks_to_materialize()819 release_query_blocks_to_materialize() {
820 return std::move(m_query_blocks_to_materialize);
821 }
822
823 /// Set new query result object for this query expression
set_query_result(Query_result * res)824 void set_query_result(Query_result *res) { m_query_result = res; }
825
826 /**
827 Whether there is a chance that optimize() is capable of materializing
828 directly into a result table if given one. Note that even if this function
829 returns true, optimize() can choose later not to do so, since it depends
830 on information (in particular, whether the query blocks can run under
831 the iterator executor or not) that is not available before optimize time.
832
833 TODO(sgunders): Now that all query blocks can run under the iterator
834 executor, the above may no longer be true. This needs investigation.
835 */
836 bool can_materialize_directly_into_result() const;
837
838 bool prepare(THD *thd, Query_result *result, ulonglong added_options,
839 ulonglong removed_options);
840
841 /**
842 If and only if materialize_destination is non-nullptr, it means that the
843 caller intends to materialize our result into the given table. If it is
844 advantageous (in particular, if this query expression is a UNION DISTINCT),
845 optimize() will not create an iterator by itself, but rather do an
846 unfinished materialize. This means that it will collect iterators for
847 all the query blocks and prepare them for materializing into the given
848 table, but not actually create a root iterator for this query expression;
849 the caller is responsible for calling release_query_blocks_to_materialize()
850 and creating the iterator itself.
851
852 Even if materialize_destination is non-nullptr, this function may choose
853 to make a regular iterator. The caller is responsible for checking
854 unfinished_materialization() if it has given a non-nullptr table.
855
856 @param thd Thread handle.
857
858 @param materialize_destination What table to try to materialize into,
859 or nullptr if the caller does not intend to materialize the result.
860 */
861 bool optimize(THD *thd, TABLE *materialize_destination);
862
863 /**
864 Do everything that would be needed before running Init() on the root
865 iterator. In particular, clear out data from previous execution iterations,
866 if needed.
867 */
868 bool ClearForExecution(THD *thd);
869
870 bool ExecuteIteratorQuery(THD *thd);
871 bool execute(THD *thd);
872 bool explain(THD *explain_thd, const THD *query_thd);
873 bool cleanup(THD *thd, bool full);
unclean()874 inline void unclean() { cleaned = UC_DIRTY; }
875 void reinit_exec_mechanism();
876
877 void print(const THD *thd, String *str, enum_query_type query_type);
878 bool accept(Select_lex_visitor *visitor);
879
880 bool add_fake_select_lex(THD *thd);
881 bool prepare_fake_select_lex(THD *thd);
set_prepared()882 void set_prepared() { prepared = true; }
set_optimized()883 void set_optimized() { optimized = true; }
set_executed()884 void set_executed() { executed = true; }
reset_executed()885 void reset_executed() { executed = false; }
is_prepared()886 bool is_prepared() const { return prepared; }
is_optimized()887 bool is_optimized() const { return optimized; }
is_executed()888 bool is_executed() const { return executed; }
889 bool change_query_result(THD *thd, Query_result_interceptor *result,
890 Query_result_interceptor *old_result);
891 bool prepare_limit(THD *thd, SELECT_LEX *provider);
892 bool set_limit(THD *thd, SELECT_LEX *provider);
893
894 inline bool is_union() const;
895 bool union_needs_tmp_table(LEX *lex);
896 /// @returns true if mixes UNION DISTINCT and UNION ALL
897 bool mixed_union_operators() const;
898
899 /// Include a query expression below a query block.
900 void include_down(LEX *lex, SELECT_LEX *outer);
901
902 /// Exclude this unit and immediately contained select_lex objects
903 void exclude_level();
904
905 /// Exclude subtree of current unit from tree of SELECTs
906 void exclude_tree(THD *thd);
907
908 /// Renumber query blocks of a query expression according to supplied LEX
909 void renumber_selects(LEX *lex);
910
911 friend class SELECT_LEX;
912
913 List<Item> *get_unit_column_types();
914 List<Item> *get_field_list();
915
916 // If we are doing a query with global LIMIT but without fake_select_lex,
917 // we need somewhere to store the record count for FOUND_ROWS().
918 // It can't be in any of the JOINs, since they may have their own
919 // LimitOffsetIterators, which will write to join->send_records
920 // whenever there is an OFFSET. (It also can't be in saved_fake_select_lex,
921 // which has no join.) Thus, we'll keep it here instead.
922 //
923 // If we have a fake_select_lex, we use its send_records instead
924 // (since its LimitOffsetIterator will write there), and if we don't
925 // have a UNION, FOUND_ROWS() refers to the (single) JOIN, and thus,
926 // we use its send_records.
927 ha_rows send_records;
928
929 enum_parsing_context get_explain_marker(const THD *thd) const;
930 void set_explain_marker(THD *thd, enum_parsing_context m);
931 void set_explain_marker_from(THD *thd, const SELECT_LEX_UNIT *u);
932
933 #ifndef DBUG_OFF
934 /**
935 Asserts that none of {this unit and its children units} is fully cleaned
936 up.
937 */
938 void assert_not_fully_clean();
939 #else
assert_not_fully_clean()940 void assert_not_fully_clean() {}
941 #endif
942 void invalidate();
943
is_recursive()944 bool is_recursive() const { return first_recursive != nullptr; }
945
946 bool check_materialized_derived_query_blocks(THD *thd);
947
948 bool clear_correlated_query_blocks();
949
950 void fix_after_pullout(SELECT_LEX *parent_select, SELECT_LEX *removed_select);
951
952 /**
953 If unit is a subquery, which forms an object of the upper level (an
954 Item_subselect, a derived TABLE_LIST), adds to this object a map
955 of tables of the upper level which the unit references.
956 */
957 void accumulate_used_tables(table_map map);
958
959 /**
960 If unit is a subquery, which forms an object of the upper level (an
961 Item_subselect, a derived TABLE_LIST), returns the place of this object
962 in the upper level query block.
963 */
964 enum_parsing_context place() const;
965
966 bool walk(Item_processor processor, enum_walk walk, uchar *arg);
967
968 /*
969 An exception: this is the only function that needs to adjust
970 explain_marker.
971 */
972 friend bool parse_view_definition(THD *thd, TABLE_LIST *view_ref);
973 };
974
975 typedef Bounds_checked_array<Item *> Ref_item_array;
976 class Semijoin_decorrelation;
977
978 /**
979 SELECT_LEX type enum
980 */
981 enum class enum_explain_type {
982 EXPLAIN_NONE = 0,
983 EXPLAIN_PRIMARY,
984 EXPLAIN_SIMPLE,
985 EXPLAIN_DERIVED,
986 EXPLAIN_SUBQUERY,
987 EXPLAIN_UNION,
988 EXPLAIN_UNION_RESULT,
989 EXPLAIN_MATERIALIZED,
990 // Total:
991 EXPLAIN_total ///< fake type, total number of all valid types
992
993 // Don't insert new types below this line!
994 };
995
996 /**
997 This class represents a query block, aka a query specification, which is
998 a query consisting of a SELECT keyword, followed by a table list,
999 optionally followed by a WHERE clause, a GROUP BY, etc.
1000 */
1001 class SELECT_LEX {
1002 public:
1003 /**
1004 @note the group_by and order_by lists below will probably be added to the
1005 constructor when the parser is converted into a true bottom-up design.
1006
1007 //SQL_I_LIST<ORDER> *group_by, SQL_I_LIST<ORDER> order_by
1008 */
1009 SELECT_LEX(MEM_ROOT *mem_root, Item *where, Item *having);
1010
where_cond()1011 Item *where_cond() const { return m_where_cond; }
where_cond_ref()1012 Item **where_cond_ref() { return &m_where_cond; }
set_where_cond(Item * cond)1013 void set_where_cond(Item *cond) { m_where_cond = cond; }
having_cond()1014 Item *having_cond() const { return m_having_cond; }
having_cond_ref()1015 Item **having_cond_ref() { return &m_having_cond; }
set_having_cond(Item * cond)1016 void set_having_cond(Item *cond) { m_having_cond = cond; }
set_query_result(Query_result * result)1017 void set_query_result(Query_result *result) { m_query_result = result; }
query_result()1018 Query_result *query_result() const { return m_query_result; }
1019 bool change_query_result(THD *thd, Query_result_interceptor *new_result,
1020 Query_result_interceptor *old_result);
1021
1022 /// Set base options for a query block (and active options too)
set_base_options(ulonglong options_arg)1023 void set_base_options(ulonglong options_arg) {
1024 DBUG_EXECUTE_IF("no_const_tables", options_arg |= OPTION_NO_CONST_TABLES;);
1025
1026 // Make sure we do not overwrite options by accident
1027 DBUG_ASSERT(m_base_options == 0 && m_active_options == 0);
1028 m_base_options = options_arg;
1029 m_active_options = options_arg;
1030 }
1031
1032 /// Add base options to a query block, also update active options
add_base_options(ulonglong options)1033 void add_base_options(ulonglong options) {
1034 DBUG_ASSERT(first_execution);
1035 m_base_options |= options;
1036 m_active_options |= options;
1037 }
1038
1039 /**
1040 Remove base options from a query block.
1041 Active options are also updated, and we assume here that "extra" options
1042 cannot override removed base options.
1043 */
remove_base_options(ulonglong options)1044 void remove_base_options(ulonglong options) {
1045 DBUG_ASSERT(first_execution);
1046 m_base_options &= ~options;
1047 m_active_options &= ~options;
1048 }
1049
1050 /// Make active options from base options, supplied options and environment:
1051 void make_active_options(ulonglong added_options, ulonglong removed_options);
1052
1053 /// Adjust the active option set
add_active_options(ulonglong options)1054 void add_active_options(ulonglong options) { m_active_options |= options; }
1055
1056 /// @return the active query options
active_options()1057 ulonglong active_options() const { return m_active_options; }
1058
1059 /**
1060 Set associated tables as read_only, ie. they cannot be inserted into,
1061 updated or deleted from during this statement.
1062 Commonly used for query blocks that are part of derived tables or
1063 views that are materialized.
1064 */
set_tables_readonly()1065 void set_tables_readonly() {
1066 // Set all referenced base tables as read only.
1067 for (TABLE_LIST *tr = leaf_tables; tr != nullptr; tr = tr->next_leaf)
1068 tr->set_readonly();
1069 }
1070
1071 /// @returns a map of all tables references in the query block
all_tables_map()1072 table_map all_tables_map() const { return (1ULL << leaf_table_count) - 1; }
1073
1074 bool field_of_table_present(TABLE_LIST *tl, bool *found);
1075 void remove_derived(THD *thd, TABLE_LIST *tl);
1076 bool remove_aggregates(THD *thd, SELECT_LEX *select);
1077
master_unit()1078 SELECT_LEX_UNIT *master_unit() const { return master; }
first_inner_unit()1079 SELECT_LEX_UNIT *first_inner_unit() const { return slave; }
outer_select()1080 SELECT_LEX *outer_select() const { return master->outer_select(); }
next_select()1081 SELECT_LEX *next_select() const { return next; }
1082
1083 TABLE_LIST *find_table_by_name(const Table_ident *ident);
1084
1085 /**
1086 @return true If STRAIGHT_JOIN applies to all tables.
1087 @return false Else.
1088 */
is_straight_join()1089 bool is_straight_join() {
1090 bool straight_join = true;
1091 /// false for exmaple in t1 STRAIGHT_JOIN t2 JOIN t3.
1092 for (TABLE_LIST *tbl = leaf_tables->next_leaf; tbl; tbl = tbl->next_leaf)
1093 straight_join &= tbl->straight;
1094 return straight_join || (active_options() & SELECT_STRAIGHT_JOIN);
1095 }
1096
last_select()1097 SELECT_LEX *last_select() {
1098 SELECT_LEX *mylast = this;
1099 for (; mylast->next_select(); mylast = mylast->next_select()) {
1100 }
1101 return mylast;
1102 }
1103
next_select_in_list()1104 SELECT_LEX *next_select_in_list() const { return link_next; }
1105
1106 void mark_as_dependent(SELECT_LEX *last, bool aggregate);
1107
1108 /// @return true if query block is explicitly grouped (non-empty GROUP BY)
is_explicitly_grouped()1109 bool is_explicitly_grouped() const { return group_list.elements > 0; }
1110
1111 /**
1112 @return true if this query block is implicitly grouped, ie it is not
1113 explicitly grouped but contains references to set functions.
1114 The query will return max. 1 row (@see also is_single_grouped()).
1115 */
is_implicitly_grouped()1116 bool is_implicitly_grouped() const {
1117 return m_agg_func_used && group_list.elements == 0;
1118 }
1119
1120 /**
1121 True if this query block is implicitly grouped.
1122
1123 @note Not reliable before name resolution.
1124
1125 @return true if this query block is implicitly grouped and returns exactly
1126 one row, which happens when it does not have a HAVING clause.
1127
1128 @remark This function is currently unused.
1129 */
is_single_grouped()1130 bool is_single_grouped() const {
1131 return m_agg_func_used && group_list.elements == 0 &&
1132 m_having_cond == nullptr;
1133 }
1134
1135 /**
1136 @return true if this query block is explicitly or implicitly grouped.
1137 @note a query with DISTINCT is not considered to be aggregated.
1138 @note in standard SQL, a query with HAVING is defined as grouped, however
1139 MySQL allows HAVING without any aggregation to be the same as WHERE.
1140 */
is_grouped()1141 bool is_grouped() const { return group_list.elements > 0 || m_agg_func_used; }
1142
1143 /// @return true if this query block contains DISTINCT at start of select list
is_distinct()1144 bool is_distinct() const { return active_options() & SELECT_DISTINCT; }
1145
1146 /**
1147 @return true if this query block contains an ORDER BY clause.
1148
1149 @note returns false if ORDER BY has been eliminated, e.g if the query
1150 can return max. 1 row.
1151 */
is_ordered()1152 bool is_ordered() const { return order_list.elements > 0; }
1153
1154 /**
1155 Based on the structure of the query at resolution time, it is possible to
1156 conclude that DISTINCT is useless and remove it.
1157 This is the case if:
1158 - all GROUP BY expressions are in SELECT list, so resulting group rows are
1159 distinct,
1160 - and ROLLUP is not specified, so it adds no row for NULLs.
1161
1162 @returns true if we can remove DISTINCT.
1163
1164 @todo could refine this to if ROLLUP were specified and all GROUP
1165 expressions were non-nullable, because ROLLUP then adds only NULL values.
1166 Currently, ROLLUP+DISTINCT is rejected because executor cannot handle
1167 it in all cases.
1168 */
can_skip_distinct()1169 bool can_skip_distinct() const {
1170 return is_grouped() && hidden_group_field_count == 0 &&
1171 olap == UNSPECIFIED_OLAP_TYPE;
1172 }
1173
1174 /// @return true if this query block has a LIMIT clause
has_limit()1175 bool has_limit() const { return select_limit != nullptr; }
1176
has_explicit_limit_or_order()1177 bool has_explicit_limit_or_order() const {
1178 return explicit_limit || order_list.elements > 0;
1179 }
1180 /// @return true if query block references full-text functions
has_ft_funcs()1181 bool has_ft_funcs() const { return ftfunc_list->elements > 0; }
1182
1183 /// @returns true if query block is a recursive member of a recursive unit
is_recursive()1184 bool is_recursive() const { return recursive_reference != nullptr; }
1185
1186 bool is_in_select_list(Item *i);
1187
1188 /**
1189 Finds a group expression matching the given item, or nullptr if
1190 none. When there are multiple candidates, ones that match in name are
1191 given priority (such that “a AS c GROUP BY a,b,c” resolves to c, not a);
1192 if there is still a tie, the leftmost is given priority.
1193
1194 @param item The item to search for.
1195 @param [out] rollup_level If not nullptr, will be set to the group
1196 expression's index (0-based).
1197 */
1198 ORDER *find_in_group_list(Item *item, int *rollup_level) const;
1199 int group_list_size() const;
1200
1201 /// @returns true if query block contains window functions
has_windows()1202 bool has_windows() const { return m_windows.elements > 0; }
1203
1204 void invalidate();
1205
get_in_sum_expr()1206 uint get_in_sum_expr() const { return in_sum_expr; }
1207
1208 bool add_item_to_list(Item *item);
1209 bool add_ftfunc_to_list(Item_func_match *func);
1210 void add_order_to_list(ORDER *order);
1211 TABLE_LIST *add_table_to_list(THD *thd, Table_ident *table, const char *alias,
1212 ulong table_options,
1213 thr_lock_type flags = TL_UNLOCK,
1214 enum_mdl_type mdl_type = MDL_SHARED_READ,
1215 List<Index_hint> *hints = nullptr,
1216 List<String> *partition_names = nullptr,
1217 LEX_STRING *option = nullptr,
1218 Parse_context *pc = nullptr);
1219
1220 /**
1221 Add item to the hidden part of select list
1222
1223 @param item item to add
1224
1225 @return Pointer to reference of the added item
1226 */
1227 Item **add_hidden_item(Item *item);
1228
get_table_list()1229 TABLE_LIST *get_table_list() const { return table_list.first; }
1230 bool init_nested_join(THD *thd);
1231 TABLE_LIST *end_nested_join();
1232 TABLE_LIST *nest_last_join(THD *thd, size_t table_cnt = 2);
1233 bool add_joined_table(TABLE_LIST *table);
1234 TABLE_LIST *convert_right_join();
get_fields_list()1235 List<Item> *get_fields_list() { return &fields_list; }
1236
1237 // Check privileges for views that are merged into query block
1238 bool check_view_privileges(THD *thd, ulong want_privilege_first,
1239 ulong want_privilege_next);
1240
1241 // Resolve and prepare information about tables for one query block
1242 bool setup_tables(THD *thd, TABLE_LIST *tables, bool select_insert);
1243
1244 // Resolve derived table, view, table function information for a query block
1245 bool resolve_placeholder_tables(THD *thd, bool apply_semijoin);
1246
1247 // Propagate exclusion from table uniqueness test into subqueries
1248 void propagate_unique_test_exclusion();
1249
1250 /// Merge name resolution context objects of a subquery into its parent
1251 void merge_contexts(SELECT_LEX *inner);
1252
1253 /// Merge derived table into query block
1254 bool merge_derived(THD *thd, TABLE_LIST *derived_table);
1255
1256 bool flatten_subqueries(THD *thd);
1257
1258 /**
1259 Update available semijoin strategies for semijoin nests.
1260
1261 Available semijoin strategies needs to be updated on every execution since
1262 optimizer_switch setting may have changed.
1263
1264 @param thd Pointer to THD object for session.
1265 Used to access optimizer_switch
1266 */
1267 void update_semijoin_strategies(THD *thd);
1268
1269 /**
1270 Returns which subquery execution strategies can be used for this query
1271 block.
1272
1273 @param thd Pointer to THD object for session.
1274 Used to access optimizer_switch
1275
1276 @retval SUBQ_MATERIALIZATION Subquery Materialization should be used
1277 @retval SUBQ_EXISTS In-to-exists execution should be used
1278 @retval CANDIDATE_FOR_IN2EXISTS_OR_MAT A cost-based decision should be made
1279 */
1280 Subquery_strategy subquery_strategy(const THD *thd) const;
1281
1282 /**
1283 Returns whether semi-join is enabled for this query block
1284
1285 @see @c Opt_hints_qb::semijoin_enabled for details on how hints
1286 affect this decision. If there are no hints for this query block,
1287 optimizer_switch setting determines whether semi-join is used.
1288
1289 @param thd Pointer to THD object for session.
1290 Used to access optimizer_switch
1291
1292 @return true if semijoin is enabled,
1293 false otherwise
1294 */
1295 bool semijoin_enabled(const THD *thd) const;
1296
set_sj_candidates(Mem_root_array<Item_exists_subselect * > * sj_cand)1297 void set_sj_candidates(Mem_root_array<Item_exists_subselect *> *sj_cand) {
1298 sj_candidates = sj_cand;
1299 }
1300
has_sj_candidates()1301 bool has_sj_candidates() const {
1302 return sj_candidates != nullptr && !sj_candidates->empty();
1303 }
1304
1305 void remove_semijoin_candidate(Item_exists_subselect *sub_query);
1306
1307 // Add full-text function elements from a list into this query block
1308 bool add_ftfunc_list(List<Item_func_match> *ftfuncs);
1309
1310 void set_lock_for_table(const Lock_descriptor &descriptor, TABLE_LIST *table);
1311
1312 void set_lock_for_tables(thr_lock_type lock_type);
1313
init_order()1314 inline void init_order() {
1315 DBUG_ASSERT(order_list.elements == 0);
1316 order_list.elements = 0;
1317 order_list.first = nullptr;
1318 order_list.next = &order_list.first;
1319 }
1320 /*
1321 This method created for reiniting LEX in mysql_admin_table() and can be
1322 used only if you are going remove all SELECT_LEX & units except belonger
1323 to LEX (LEX::unit & LEX::select, for other purposes use
1324 SELECT_LEX_UNIT::exclude_level()
1325 */
cut_subtree()1326 void cut_subtree() { slave = nullptr; }
1327 bool test_limit();
1328 /**
1329 Get offset for LIMIT.
1330
1331 Evaluate offset item if necessary.
1332
1333 @return Number of rows to skip.
1334
1335 @todo Integrate better with SELECT_LEX_UNIT::set_limit()
1336 */
1337 ha_rows get_offset(THD *thd);
1338 /**
1339 Get limit.
1340
1341 Evaluate limit item if necessary.
1342
1343 @return Limit of rows in result.
1344
1345 @todo Integrate better with SELECT_LEX_UNIT::set_limit()
1346 */
1347 ha_rows get_limit(THD *thd);
1348
1349 /// Assign a default name resolution object for this query block.
1350 bool set_context(Name_resolution_context *outer_context);
1351
1352 /// Setup the array containing references to base items
1353 bool setup_base_ref_items(THD *thd);
1354 void print(const THD *thd, String *str, enum_query_type query_type);
1355
1356 /**
1357 Print detail of the SELECT_LEX object.
1358
1359 @param thd Thread handler
1360 @param query_type Options to print out string output
1361 @param[out] str String of output.
1362 */
1363 void print_select(const THD *thd, String *str, enum_query_type query_type);
1364
1365 /**
1366 Print detail of the UPDATE statement.
1367
1368 @param thd Thread handler
1369 @param[out] str String of output
1370 @param query_type Options to print out string output
1371 */
1372 void print_update(const THD *thd, String *str, enum_query_type query_type);
1373
1374 /**
1375 Print detail of the DELETE statement.
1376
1377 @param thd Thread handler
1378 @param[out] str String of output
1379 @param query_type Options to print out string output
1380 */
1381 void print_delete(const THD *thd, String *str, enum_query_type query_type);
1382
1383 /**
1384 Print detail of the INSERT statement.
1385
1386 @param thd Thread handler
1387 @param[out] str String of output
1388 @param query_type Options to print out string output
1389 */
1390 void print_insert(const THD *thd, String *str, enum_query_type query_type);
1391
1392 /**
1393 Print detail of Hints.
1394
1395 @param thd Thread handler
1396 @param[out] str String of output
1397 @param query_type Options to print out string output
1398 */
1399 void print_hints(const THD *thd, String *str, enum_query_type query_type);
1400
1401 /**
1402 Print error.
1403
1404 @param thd Thread handler
1405 @param[out] str String of output
1406
1407 @retval false If there is no error
1408 @retval true else
1409 */
1410 bool print_error(const THD *thd, String *str);
1411
1412 /**
1413 Print select options.
1414
1415 @param[out] str String of output
1416 */
1417 void print_select_options(String *str);
1418
1419 /**
1420 Print UPDATE options.
1421
1422 @param[out] str String of output
1423 */
1424 void print_update_options(String *str);
1425
1426 /**
1427 Print DELETE options.
1428
1429 @param[out] str String of output
1430 */
1431 void print_delete_options(String *str);
1432
1433 /**
1434 Print INSERT options.
1435
1436 @param[out] str String of output
1437 */
1438 void print_insert_options(String *str);
1439
1440 /**
1441 Print list of tables.
1442
1443 @param thd Thread handler
1444 @param[out] str String of output
1445 @param table_list TABLE_LIST object
1446 @param query_type Options to print out string output
1447 */
1448 void print_table_references(const THD *thd, String *str,
1449 TABLE_LIST *table_list,
1450 enum_query_type query_type);
1451
1452 /**
1453 Print list of items in SELECT_LEX object.
1454
1455 @param thd Thread handle
1456 @param[out] str String of output
1457 @param query_type Options to print out string output
1458 */
1459 void print_item_list(const THD *thd, String *str, enum_query_type query_type);
1460
1461 /**
1462 Print assignments list. Used in UPDATE and
1463 INSERT ... ON DUPLICATE KEY UPDATE ...
1464
1465 @param thd Thread handle
1466 @param[out] str String of output
1467 @param query_type Options to print out string output
1468 @param fields List columns to be assigned.
1469 @param values List of values.
1470 */
1471 void print_update_list(const THD *thd, String *str,
1472 enum_query_type query_type, List<Item> fields,
1473 List<Item> values);
1474
1475 /**
1476 Print column list to be inserted into. Used in INSERT.
1477
1478 @param thd Thread handle
1479 @param[out] str String of output
1480 @param query_type Options to print out string output
1481 */
1482 void print_insert_fields(const THD *thd, String *str,
1483 enum_query_type query_type);
1484
1485 /**
1486 Print list of values, used in INSERT and for general VALUES clause.
1487
1488 @param thd Thread handle
1489 @param[out] str String of output
1490 @param query_type Options to print out string output
1491 @param values List of values
1492 @param prefix Prefix to print before each row in value list
1493 = nullptr: No prefix wanted
1494 */
1495 void print_values(const THD *thd, String *str, enum_query_type query_type,
1496 List<List<Item>> values, const char *prefix);
1497
1498 /**
1499 Print list of tables in FROM clause.
1500
1501 @param thd Thread handler
1502 @param[out] str String of output
1503 @param query_type Options to print out string output
1504 */
1505 void print_from_clause(const THD *thd, String *str,
1506 enum_query_type query_type);
1507
1508 /**
1509 Print list of conditions in WHERE clause.
1510
1511 @param thd Thread handle
1512 @param[out] str String of output
1513 @param query_type Options to print out string output
1514 */
1515 void print_where_cond(const THD *thd, String *str,
1516 enum_query_type query_type);
1517
1518 /**
1519 Print list of items in GROUP BY clause.
1520
1521 @param thd Thread handle
1522 @param[out] str String of output
1523 @param query_type Options to print out string output
1524 */
1525 void print_group_by(const THD *thd, String *str, enum_query_type query_type);
1526
1527 /**
1528 Print list of items in HAVING clause.
1529
1530 @param thd Thread handle
1531 @param[out] str String of output
1532 @param query_type Options to print out string output
1533 */
1534 void print_having(const THD *thd, String *str, enum_query_type query_type);
1535
1536 /**
1537 Print details of Windowing functions.
1538
1539 @param thd Thread handler
1540 @param[out] str String of output
1541 @param query_type Options to print out string output
1542 */
1543 void print_windows(const THD *thd, String *str, enum_query_type query_type);
1544
1545 /**
1546 Print list of items in ORDER BY clause.
1547
1548 @param thd Thread handle
1549 @param[out] str String of output
1550 @param query_type Options to print out string output
1551 */
1552 void print_order_by(const THD *thd, String *str, enum_query_type query_type);
1553
1554 static void print_order(const THD *thd, String *str, ORDER *order,
1555 enum_query_type query_type);
1556 void print_limit(const THD *thd, String *str, enum_query_type query_type);
1557 void fix_prepare_information(THD *thd);
1558
1559 /**
1560 Accept function for SELECT and DELETE.
1561
1562 @param visitor Select_lex_visitor Object
1563 */
1564 bool accept(Select_lex_visitor *visitor);
1565
1566 /**
1567 Cleanup this subtree (this SELECT_LEX and all nested SELECT_LEXes and
1568 SELECT_LEX_UNITs).
1569 @param thd thread handle
1570 @param full if false only partial cleanup is done, JOINs and JOIN_TABs are
1571 kept to provide info for EXPLAIN CONNECTION; if true, complete cleanup is
1572 done, all JOINs are freed.
1573 */
1574 bool cleanup(THD *thd, bool full);
1575 /*
1576 Recursively cleanup the join of this select lex and of all nested
1577 select lexes. This is not a full cleanup.
1578 */
1579 void cleanup_all_joins();
1580
1581 /// Return true if this query block is part of a UNION
is_part_of_union()1582 bool is_part_of_union() const { return master_unit()->is_union(); }
1583
1584 /**
1585 @return true if query block is found during preparation to produce no data.
1586 Notice that if query is implicitly grouped, an aggregation row will
1587 still be returned.
1588 */
is_empty_query()1589 bool is_empty_query() const { return m_empty_query; }
1590
1591 /// Set query block as returning no data
1592 /// @todo This may also be set when we have an always false WHERE clause
set_empty_query()1593 void set_empty_query() {
1594 DBUG_ASSERT(join == nullptr);
1595 m_empty_query = true;
1596 }
1597 /*
1598 For MODE_ONLY_FULL_GROUP_BY we need to know if
1599 this query block is the aggregation query of at least one aggregate
1600 function.
1601 */
agg_func_used()1602 bool agg_func_used() const { return m_agg_func_used; }
json_agg_func_used()1603 bool json_agg_func_used() const { return m_json_agg_func_used; }
1604
set_agg_func_used(bool val)1605 void set_agg_func_used(bool val) { m_agg_func_used = val; }
1606
set_json_agg_func_used(bool val)1607 void set_json_agg_func_used(bool val) { m_json_agg_func_used = val; }
1608
1609 /// Lookup for SELECT_LEX type
1610 enum_explain_type type();
1611
1612 /// Lookup for a type string
get_type_str()1613 const char *get_type_str() { return type_str[static_cast<int>(type())]; }
get_type_str(enum_explain_type type)1614 static const char *get_type_str(enum_explain_type type) {
1615 return type_str[static_cast<int>(type)];
1616 }
1617
is_dependent()1618 bool is_dependent() const { return uncacheable & UNCACHEABLE_DEPENDENT; }
is_cacheable()1619 bool is_cacheable() const { return !uncacheable; }
1620
1621 /// @returns true if this query block outputs at most one row.
source_table_is_one_row()1622 bool source_table_is_one_row() const {
1623 return (table_list.size() == 0 &&
1624 (!is_table_value_constructor || row_value_list->size() == 1));
1625 }
1626
1627 /// Include query block inside a query expression.
1628 void include_down(LEX *lex, SELECT_LEX_UNIT *outer);
1629
1630 /// Include a query block next to another query block.
1631 void include_neighbour(LEX *lex, SELECT_LEX *before);
1632
1633 /// Include query block inside a query expression, but do not link.
1634 void include_standalone(SELECT_LEX_UNIT *sel, SELECT_LEX **ref);
1635
1636 /// Include query block into global list.
1637 void include_in_global(SELECT_LEX **plink);
1638
1639 /// Include chain of query blocks into global list.
1640 void include_chain_in_global(SELECT_LEX **start);
1641
1642 /// Renumber query blocks of contained query expressions
1643 void renumber(LEX *lex);
1644
1645 /**
1646 Does permanent transformations which are local to a query block (which do
1647 not merge it to another block).
1648 */
1649 bool apply_local_transforms(THD *thd, bool prune);
1650
1651 bool get_optimizable_conditions(THD *thd, Item **new_where,
1652 Item **new_having);
1653
1654 bool validate_outermost_option(LEX *lex, const char *wrong_option) const;
1655 bool validate_base_options(LEX *lex, ulonglong options) const;
1656
1657 bool walk(Item_processor processor, enum_walk walk, uchar *arg);
1658
1659 bool add_tables(THD *thd, const Mem_root_array<Table_ident *> *tables,
1660 ulong table_options, thr_lock_type lock_type,
1661 enum_mdl_type mdl_type);
1662
1663 bool resolve_rollup_wfs(THD *thd);
1664
1665 bool setup_conds(THD *thd);
1666 bool prepare(THD *thd);
1667 bool optimize(THD *thd);
1668 void reset_nj_counters(mem_root_deque<TABLE_LIST *> *join_list = nullptr);
1669
1670 bool change_group_ref_for_func(THD *thd, Item *func, bool *changed);
1671 bool change_group_ref_for_cond(THD *thd, Item_cond *cond, bool *changed);
1672
1673 // ************************************************
1674 // * Members (most of these should not be public) *
1675 // ************************************************
1676
1677 /**
1678 List of columns and expressions:
1679 SELECT: Columns and expressions in the SELECT list.
1680 UPDATE: Columns in the SET clause.
1681
1682 @see is_item_list_lookup
1683 */
1684 List<Item> fields_list{}; ///< hold field list
1685 /**
1686 All expressions needed after join and filtering, ie
1687 select list, group by list, having clause, window clause, order by clause.
1688 Does not include join conditions nor where clause.
1689 */
1690 List<Item> all_fields{};
1691
1692 /**
1693 All windows defined on the select, both named and inlined
1694 */
1695 List<Window> m_windows;
1696
1697 /**
1698 Usually a pointer to ftfunc_list_alloc, but in UNION this is used to create
1699 fake select_lex that consolidates result fields of UNION
1700 */
1701 List<Item_func_match> *ftfunc_list;
1702 List<Item_func_match> ftfunc_list_alloc{};
1703
1704 /// The VALUES items of a table value constructor.
1705 List<List<Item>> *row_value_list{nullptr};
1706
1707 /// List of semi-join nests generated for this query block
1708 mem_root_deque<TABLE_LIST *> sj_nests;
1709
1710 /// List of tables in FROM clause - use TABLE_LIST::next_local to traverse
1711 SQL_I_List<TABLE_LIST> table_list{};
1712
1713 /**
1714 ORDER BY clause.
1715 This list may be mutated during optimization (by remove_const()),
1716 so for prepared statements, we keep a copy of the ORDER.next pointers in
1717 order_list_ptrs, and re-establish the original list before each execution.
1718 */
1719 SQL_I_List<ORDER> order_list{};
1720 Group_list_ptrs *order_list_ptrs{nullptr};
1721
1722 /**
1723 GROUP BY clause.
1724 This list may be mutated during optimization (by remove_const()),
1725 so for prepared statements, we keep a copy of the ORDER.next pointers in
1726 group_list_ptrs, and re-establish the original list before each execution.
1727 */
1728 SQL_I_List<ORDER> group_list{};
1729 Group_list_ptrs *group_list_ptrs{nullptr};
1730
1731 // Used so that AggregateIterator knows which items to signal when the rollup
1732 // level changes. Obviously only used in the presence of rollup.
1733 Prealloced_array<Item_rollup_group_item *, 4> rollup_group_items{
1734 PSI_NOT_INSTRUMENTED};
1735 Prealloced_array<Item_rollup_sum_switcher *, 4> rollup_sums{
1736 PSI_NOT_INSTRUMENTED};
1737
1738 /// Query-block-level hints, for this query block
1739 Opt_hints_qb *opt_hints_qb{nullptr};
1740
1741 char *db{nullptr};
1742
1743 /**
1744 If this query block is a recursive member of a recursive unit: the
1745 TABLE_LIST, in this recursive member, referencing the query
1746 name.
1747 */
1748 TABLE_LIST *recursive_reference{nullptr};
1749
1750 /**
1751 To pass the first steps of resolution, a recursive reference is made to
1752 be a dummy derived table; after the temporary table is created based on
1753 the non-recursive members' types, the recursive reference is made to be a
1754 reference to the tmp table. Its dummy-derived-table unit is saved in this
1755 member, so that when the statement's execution ends, the reference can be
1756 restored to be a dummy derived table for the next execution, which is
1757 necessary if we have a prepared statement.
1758 WL#6570 should allow to remove this.
1759 */
1760 SELECT_LEX_UNIT *recursive_dummy_unit{nullptr};
1761
1762 /// Reference to LEX that this query block belongs to
1763 LEX *parent_lex{nullptr};
1764
1765 /**
1766 The set of those tables whose fields are referenced in the select list of
1767 this select level.
1768 */
1769 table_map select_list_tables{0};
1770 table_map outer_join{0}; ///< Bitmap of all inner tables from outer joins
1771
1772 /**
1773 Context for name resolution for all column references except columns
1774 from joined tables.
1775 */
1776 Name_resolution_context context{};
1777
1778 /**
1779 Pointer to first object in list of Name res context objects that have
1780 this query block as the base query block.
1781 Includes field "context" which is embedded in this query block.
1782 */
1783 Name_resolution_context *first_context;
1784
1785 /**
1786 After optimization it is pointer to corresponding JOIN. This member
1787 should be changed only when THD::LOCK_query_plan mutex is taken.
1788 */
1789 JOIN *join{nullptr};
1790 /// join list of the top level
1791 mem_root_deque<TABLE_LIST *> top_join_list;
1792 /// list for the currently parsed join
1793 mem_root_deque<TABLE_LIST *> *join_list;
1794 /// table embedding the above list
1795 TABLE_LIST *embedding{nullptr};
1796 /**
1797 Points to first leaf table of query block. After setup_tables() is done,
1798 this is a list of base tables and derived tables. After derived tables
1799 processing is done, this is a list of base tables only.
1800 Use TABLE_LIST::next_leaf to traverse the list.
1801 */
1802 TABLE_LIST *leaf_tables{nullptr};
1803 // Last table for LATERAL join, used by table functions
1804 TABLE_LIST *end_lateral_table{nullptr};
1805
1806 /// LIMIT clause, NULL if no limit is given
1807 Item *select_limit{nullptr};
1808 /// LIMIT ... OFFSET clause, NULL if no offset is given
1809 Item *offset_limit{nullptr};
1810
1811 /// Circular linked list of sum func in nested selects
1812 Item_sum *inner_sum_func_list{nullptr};
1813
1814 /**
1815 Array of pointers to "base" items; one each for every selected expression
1816 and referenced item in the query block. All references to fields are to
1817 buffers associated with the primary input tables.
1818 */
1819 Ref_item_array base_ref_items;
1820
1821 uint select_number{0}; ///< Query block number (used for EXPLAIN)
1822
1823 /**
1824 Saved values of the WHERE and HAVING clauses. Allowed values are:
1825 - COND_UNDEF if the condition was not specified in the query or if it
1826 has not been optimized yet
1827 - COND_TRUE if the condition is always true
1828 - COND_FALSE if the condition is impossible
1829 - COND_OK otherwise
1830 */
1831 Item::cond_result cond_value{Item::COND_UNDEF};
1832 Item::cond_result having_value{Item::COND_UNDEF};
1833
1834 /// Parse context: indicates where the current expression is being parsed
1835 enum_parsing_context parsing_place{CTX_NONE};
1836 /// Parse context: is inside a set function if this is positive
1837 uint in_sum_expr{0};
1838
1839 /**
1840 Three fields used by semi-join transformations to know when semi-join is
1841 possible, and in which condition tree the subquery predicate is located.
1842 */
1843 enum Resolve_place {
1844 RESOLVE_NONE,
1845 RESOLVE_JOIN_NEST,
1846 RESOLVE_CONDITION,
1847 RESOLVE_HAVING,
1848 RESOLVE_SELECT_LIST
1849 };
1850 Resolve_place resolve_place{
1851 RESOLVE_NONE}; ///< Indicates part of query being resolved
1852
1853 /**
1854 Number of fields used in select list or where clause of current select
1855 and all inner subselects.
1856 */
1857 uint select_n_where_fields{0};
1858 /**
1859 number of items in select_list and HAVING clause used to get number
1860 bigger then can be number of entries that will be added to all item
1861 list during split_sum_func
1862 */
1863 uint select_n_having_items{0};
1864 uint cond_count{0}; ///< number of arguments of and/or/xor in where/having/on
1865 uint between_count{0}; ///< number of between predicates in where/having/on
1866 uint max_equal_elems{
1867 0}; ///< maximal number of elements in multiple equalities
1868
1869 /**
1870 Number of Item_sum-derived objects in this SELECT. Keeps count of
1871 aggregate functions and window functions(to allocate items in ref array).
1872 See SELECT_LEX::setup_base_ref_items.
1873 */
1874 uint n_sum_items{0};
1875 /// Number of Item_sum-derived objects in children and descendant SELECTs
1876 uint n_child_sum_items{0};
1877
1878 /// Keep track for allocation of base_ref_items: scalar subqueries may be
1879 /// replaced by a field during scalar_to_derived transformation
1880 uint n_scalar_subqueries{0};
1881
1882 /// Number of materialized derived tables and views in this query block.
1883 uint materialized_derived_table_count{0};
1884 /// Number of partitioned tables
1885 uint partitioned_table_count{0};
1886
1887 /**
1888 Number of wildcards used in the SELECT list. For example,
1889 SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
1890 has 3 wildcards.
1891 */
1892 uint with_wild{0};
1893
1894 /// Number of leaf tables in this query block.
1895 uint leaf_table_count{0};
1896 /// Number of derived tables and views in this query block.
1897 uint derived_table_count{0};
1898 /// Number of table functions in this query block
1899 uint table_func_count{0};
1900
1901 /**
1902 Nesting level of query block, outer-most query block has level 0,
1903 its subqueries have level 1, etc. @see also sql/item_sum.h.
1904 */
1905 int nest_level{0};
1906
1907 /// Indicates whether this query block contains the WITH ROLLUP clause
1908 olap_type olap{UNSPECIFIED_OLAP_TYPE};
1909
1910 /// @see enum_condition_context
1911 enum_condition_context condition_context{enum_condition_context::ANDS};
1912
1913 /// If set, the query block is of the form VALUES row_list.
1914 bool is_table_value_constructor{false};
1915
1916 /// Describes context of this query block (e.g if it is a derived table).
1917 sub_select_type linkage{UNSPECIFIED_TYPE};
1918
1919 /**
1920 result of this query can't be cached, bit field, can be :
1921 UNCACHEABLE_DEPENDENT
1922 UNCACHEABLE_RAND
1923 UNCACHEABLE_SIDEEFFECT
1924 */
1925 uint8 uncacheable{0};
1926
1927 /**
1928 This variable is required to ensure proper work of subqueries and
1929 stored procedures. Generally, one should use the states of
1930 Query_arena to determine if it's a statement prepare or first
1931 execution of a stored procedure. However, in case when there was an
1932 error during the first execution of a stored procedure, the SP body
1933 is not expelled from the SP cache. Therefore, a deeply nested
1934 subquery might be left unoptimized. So we need this per-subquery
1935 variable to inidicate the optimization/execution state of every
1936 subquery. Prepared statements work OK in that regard, as in
1937 case of an error during prepare the PS is not created.
1938 */
1939 bool first_execution{true};
1940
1941 /// True when semi-join pull-out processing is complete
1942 bool sj_pullout_done{false};
1943
1944 /// Used by nested scalar_to_derived transformations
1945 bool m_was_implicitly_grouped{false};
1946
1947 /// True: skip local transformations during prepare() call (used by INSERT)
1948 bool skip_local_transforms{false};
1949
1950 bool is_item_list_lookup{false};
1951
1952 /// true when having fix field called in processing of this query block
1953 bool having_fix_field{false};
1954 /// true when GROUP BY fix field called in processing of this query block
1955 bool group_fix_field{false};
1956
1957 /**
1958 True if contains or aggregates set functions.
1959 @note this is wrong when a locally found set function is aggregated
1960 in an outer query block.
1961 */
1962 bool with_sum_func{false};
1963
1964 /**
1965 HAVING clause contains subquery => we can't close tables before
1966 query processing end even if we use temporary table
1967 */
1968 bool subquery_in_having{false};
1969
1970 /// explicit LIMIT clause is used
1971 bool explicit_limit{false};
1972
1973 /// exclude this query block from unique_table() check
1974 bool exclude_from_table_unique_test{false};
1975
1976 bool no_table_names_allowed{false}; ///< used for global order by
1977
1978 private:
1979 friend class SELECT_LEX_UNIT;
1980 friend class Condition_context;
1981
1982 bool record_join_nest_info(mem_root_deque<TABLE_LIST *> *tables);
1983 bool simplify_joins(THD *thd, mem_root_deque<TABLE_LIST *> *join_list,
1984 bool top, bool in_sj, Item **new_conds,
1985 uint *changelog = nullptr);
1986 /// Remove semijoin condition for this query block
1987 void clear_sj_expressions(NESTED_JOIN *nested_join);
1988 /// Build semijoin condition for th query block
1989 bool build_sj_cond(THD *thd, NESTED_JOIN *nested_join,
1990 SELECT_LEX *subq_select, table_map outer_tables_map,
1991 Item **sj_cond);
1992 bool decorrelate_condition(Semijoin_decorrelation &sj_decor,
1993 TABLE_LIST *join_nest);
1994
1995 bool convert_subquery_to_semijoin(THD *thd, Item_exists_subselect *subq_pred);
1996 TABLE_LIST *synthesize_derived(THD *thd, SELECT_LEX_UNIT *unit,
1997 Item *join_cond, bool left_outer,
1998 bool use_inner_join);
1999 bool transform_subquery_to_derived(THD *thd, TABLE_LIST **out_tl,
2000 SELECT_LEX_UNIT *subs_unit,
2001 Item_subselect *subq, bool use_inner_join,
2002 Item *join_condition);
2003 bool transform_table_subquery_to_join_with_derived(
2004 THD *thd, Item_exists_subselect *subq_pred);
2005 void remap_tables(THD *thd);
2006 bool resolve_subquery(THD *thd);
2007 void mark_item_as_maybe_null_if_rollup_item(Item *item);
2008 Item *resolve_rollup_item(THD *thd, Item *item);
2009 bool resolve_rollup(THD *thd);
2010
2011 bool setup_wild(THD *thd);
2012 bool setup_order_final(THD *thd);
2013 bool setup_group(THD *thd);
2014 void fix_after_pullout(SELECT_LEX *parent_select, SELECT_LEX *removed_select);
2015 void remove_redundant_subquery_clauses(THD *thd,
2016 int hidden_group_field_count);
2017 void repoint_contexts_of_join_nests(mem_root_deque<TABLE_LIST *> join_list);
2018 void empty_order_list(SELECT_LEX *sl);
2019 bool setup_join_cond(THD *thd, mem_root_deque<TABLE_LIST *> *tables,
2020 bool in_update);
2021 bool find_common_table_expr(THD *thd, Table_ident *table_id, TABLE_LIST *tl,
2022 Parse_context *pc, bool *found);
2023 /**
2024 Transform eligible scalar subqueries in the SELECT list, WHERE condition,
2025 HAVING condition or JOIN conditions of a query block[*] to an equivalent
2026 derived table of a LEFT OUTER join, e.g. as shown in this uncorrelated
2027 subquery:
2028
2029 [*] a.k.a "transformed query block" throughout this method and its minions.
2030
2031 <pre>
2032 SELECT * FROM t1
2033 WHERE t1.a > (SELECT COUNT(a) AS cnt FROM t2); ->
2034
2035 SELECT t1.* FROM t1 LEFT OUTER JOIN
2036 (SELECT COUNT(a) AS cnt FROM t2) AS derived
2037 ON TRUE WHERE t1.a > derived.cnt;
2038 </pre>
2039
2040 Grouping in the transformed query block may necessitate the grouping to be
2041 moved down to another derived table, cf. transform_grouped_to_derived.
2042
2043 Limitations:
2044 - only implicitly grouped subqueries (guaranteed to have cardinality one)
2045 are identified as scalar subqueries.
2046 _ Correlated subqueries are not handled
2047
2048 @param[in,out] thd the session context
2049 @returns true on error
2050 */
2051 bool transform_scalar_subqueries_to_join_with_derived(THD *thd);
2052 bool transform_grouped_to_derived(THD *thd, bool *break_off);
2053 bool replace_subquery_in_expr(THD *thd, Item_singlerow_subselect *subquery,
2054 Item **expr);
2055 TABLE_LIST *nest_derived(THD *thd, Item *join_cond,
2056 mem_root_deque<TABLE_LIST *> *join_list,
2057 TABLE_LIST *new_derived_table);
2058 void update_join_cond_context(SELECT_LEX *new_context,
2059 mem_root_deque<TABLE_LIST *> *join_list);
2060
2061 bool resolve_table_value_constructor_values(THD *thd);
2062
2063 // Delete unused columns from merged derived tables
2064 void delete_unused_merged_columns(mem_root_deque<TABLE_LIST *> *tables);
2065
2066 /// Helper for fix_prepare_information()
2067 void fix_prepare_information_for_order(THD *thd, SQL_I_List<ORDER> *list,
2068 Group_list_ptrs **list_ptrs);
2069
2070 bool prepare_values(THD *thd);
2071 bool check_only_full_group_by(THD *thd);
2072
2073 //
2074 // Members:
2075 //
2076
2077 /**
2078 Pointer to collection of subqueries candidate for semi/antijoin
2079 conversion.
2080 Template parameter is "true": no need to run DTORs on pointers.
2081 */
2082 Mem_root_array<Item_exists_subselect *> *sj_candidates{nullptr};
2083
2084 /// How many expressions are part of the order by but not select list.
2085 int hidden_order_field_count{0};
2086
2087 /**
2088 Intrusive double-linked list of all query blocks within the same
2089 query expression.
2090 */
2091 SELECT_LEX *next{nullptr};
2092 SELECT_LEX **prev{nullptr};
2093
2094 /// The query expression containing this query block.
2095 SELECT_LEX_UNIT *master{nullptr};
2096 /// The first query expression contained within this query block.
2097 SELECT_LEX_UNIT *slave{nullptr};
2098
2099 /// Intrusive double-linked global list of query blocks.
2100 SELECT_LEX *link_next{nullptr};
2101 SELECT_LEX **link_prev{nullptr};
2102
2103 /// Result of this query block
2104 Query_result *m_query_result{nullptr};
2105
2106 /**
2107 Options assigned from parsing and throughout resolving,
2108 should not be modified after resolving is done.
2109 */
2110 ulonglong m_base_options{0};
2111 /**
2112 Active options. Derived from base options, modifiers added during
2113 resolving and values from session variable option_bits. Since the latter
2114 may change, active options are refreshed per execution of a statement.
2115 */
2116 ulonglong m_active_options{0};
2117
2118 TABLE_LIST *resolve_nest{
2119 nullptr}; ///< Used when resolving outer join condition
2120
2121 /**
2122 Condition to be evaluated after all tables in a query block are joined.
2123 After all permanent transformations have been conducted by
2124 SELECT_LEX::prepare(), this condition is "frozen", any subsequent changes
2125 to it must be done with change_item_tree(), unless they only modify AND/OR
2126 items and use a copy created by SELECT_LEX::get_optimizable_conditions().
2127 Same is true for 'having_cond'.
2128 */
2129 Item *m_where_cond;
2130
2131 /// Condition to be evaluated on grouped rows after grouping.
2132 Item *m_having_cond;
2133
2134 List<TABLE_LIST> m_scalar_to_derived;
2135
2136 /// Number of GROUP BY expressions added to all_fields
2137 int hidden_group_field_count;
2138
2139 /**
2140 True if query block has semi-join nests merged into it. Notice that this
2141 is updated earlier than sj_nests, so check this if info is needed
2142 before the full resolver process is complete.
2143 */
2144 bool has_sj_nests{false};
2145 bool has_aj_nests{false}; ///< @see has_sj_nests; counts antijoin nests.
2146
2147 /// Allow merge of immediate unnamed derived tables
2148 bool allow_merge_derived{true};
2149
2150 bool m_agg_func_used{false};
2151 bool m_json_agg_func_used{false};
2152
2153 /**
2154 True if query block does not generate any rows before aggregation,
2155 determined during preparation (not optimization).
2156 */
2157 bool m_empty_query{false};
2158
2159 static const char
2160 *type_str[static_cast<int>(enum_explain_type::EXPLAIN_total)];
2161 };
2162
is_union()2163 inline bool SELECT_LEX_UNIT::is_union() const {
2164 return first_select()->next_select() &&
2165 first_select()->next_select()->linkage == UNION_TYPE;
2166 }
2167
2168 /// Utility RAII class to save/modify/restore the condition_context information
2169 /// of a query block. @see enum_condition_context.
2170 class Condition_context {
2171 public:
2172 Condition_context(SELECT_LEX *select_ptr, enum_condition_context new_type =
2173 enum_condition_context::NEITHER)
select(nullptr)2174 : select(nullptr), saved_value() {
2175 if (select_ptr) {
2176 select = select_ptr;
2177 saved_value = select->condition_context;
2178 // More restrictive wins over less restrictive:
2179 if (new_type == enum_condition_context::NEITHER ||
2180 (new_type == enum_condition_context::ANDS_ORS &&
2181 saved_value == enum_condition_context::ANDS))
2182 select->condition_context = new_type;
2183 }
2184 }
~Condition_context()2185 ~Condition_context() {
2186 if (select) select->condition_context = saved_value;
2187 }
2188
2189 private:
2190 SELECT_LEX *select;
2191 enum_condition_context saved_value;
2192 };
2193
2194 bool walk_join_list(mem_root_deque<TABLE_LIST *> &list,
2195 std::function<bool(TABLE_LIST *)> action);
2196
2197 /**
2198 Base class for secondary engine execution context objects. Secondary
2199 storage engines may create classes derived from this one which
2200 contain state they need to preserve between optimization and
2201 execution of statements. The context objects should be allocated on
2202 the execution MEM_ROOT.
2203 */
2204 class Secondary_engine_execution_context {
2205 public:
2206 /**
2207 Destructs the secondary engine execution context object. It is
2208 called after the query execution has completed. Secondary engines
2209 may override the destructor in subclasses and add code that
2210 performs cleanup tasks that are needed after query execution.
2211 */
2212 virtual ~Secondary_engine_execution_context() = default;
2213 };
2214
2215 typedef struct struct_slave_connection {
2216 char *user;
2217 char *password;
2218 char *plugin_auth;
2219 char *plugin_dir;
2220
2221 void reset();
2222 } LEX_SLAVE_CONNECTION;
2223
2224 struct st_sp_chistics {
2225 LEX_CSTRING comment;
2226 enum enum_sp_suid_behaviour suid;
2227 bool detistic;
2228 enum enum_sp_data_access daccess;
2229 };
2230
2231 extern const LEX_STRING null_lex_str;
2232
2233 struct st_trg_chistics {
2234 enum enum_trigger_action_time_type action_time;
2235 enum enum_trigger_event_type event;
2236
2237 /**
2238 FOLLOWS or PRECEDES as specified in the CREATE TRIGGER statement.
2239 */
2240 enum enum_trigger_order_type ordering_clause;
2241
2242 /**
2243 Trigger name referenced in the FOLLOWS/PRECEDES clause of the CREATE TRIGGER
2244 statement.
2245 */
2246 LEX_CSTRING anchor_trigger_name;
2247 };
2248
2249 extern sys_var *trg_new_row_fake_var;
2250
2251 class Sroutine_hash_entry;
2252
2253 /*
2254 Class representing list of all tables used by statement and other
2255 information which is necessary for opening and locking its tables,
2256 like SQL command for this statement.
2257
2258 Also contains information about stored functions used by statement
2259 since during its execution we may have to add all tables used by its
2260 stored functions/triggers to this list in order to pre-open and lock
2261 them.
2262
2263 Also used by LEX::reset_n_backup/restore_backup_query_tables_list()
2264 methods to save and restore this information.
2265 */
2266
2267 class Query_tables_list {
2268 public:
2269 Query_tables_list &operator=(Query_tables_list &&) = default;
2270
2271 /**
2272 SQL command for this statement. Part of this class since the
2273 process of opening and locking tables for the statement needs
2274 this information to determine correct type of lock for some of
2275 the tables.
2276 */
2277 enum_sql_command sql_command;
2278 /* Global list of all tables used by this statement */
2279 TABLE_LIST *query_tables;
2280 /* Pointer to next_global member of last element in the previous list. */
2281 TABLE_LIST **query_tables_last;
2282 /*
2283 If non-0 then indicates that query requires prelocking and points to
2284 next_global member of last own element in query table list (i.e. last
2285 table which was not added to it as part of preparation to prelocking).
2286 0 - indicates that this query does not need prelocking.
2287 */
2288 TABLE_LIST **query_tables_own_last;
2289 /*
2290 Set of stored routines called by statement.
2291 (Note that we use lazy-initialization for this hash).
2292
2293 See Sroutine_hash_entry for explanation why this hash uses binary
2294 key comparison.
2295 */
2296 enum { START_SROUTINES_HASH_SIZE = 16 };
2297 std::unique_ptr<malloc_unordered_map<std::string, Sroutine_hash_entry *>>
2298 sroutines;
2299 /*
2300 List linking elements of 'sroutines' set. Allows you to add new elements
2301 to this set as you iterate through the list of existing elements.
2302 'sroutines_list_own_last' is pointer to ::next member of last element of
2303 this list which represents routine which is explicitly used by query.
2304 'sroutines_list_own_elements' number of explicitly used routines.
2305 We use these two members for restoring of 'sroutines_list' to the state
2306 in which it was right after query parsing.
2307 */
2308 SQL_I_List<Sroutine_hash_entry> sroutines_list;
2309 Sroutine_hash_entry **sroutines_list_own_last;
2310 uint sroutines_list_own_elements;
2311
2312 /**
2313 Locking state of tables in this particular statement.
2314
2315 If we under LOCK TABLES or in prelocked mode we consider tables
2316 for the statement to be "locked" if there was a call to lock_tables()
2317 (which called handler::start_stmt()) for tables of this statement
2318 and there was no matching close_thread_tables() call.
2319
2320 As result this state may differ significantly from one represented
2321 by Open_tables_state::lock/locked_tables_mode more, which are always
2322 "on" under LOCK TABLES or in prelocked mode.
2323 */
2324 enum enum_lock_tables_state { LTS_NOT_LOCKED = 0, LTS_LOCKED };
2325 enum_lock_tables_state lock_tables_state;
is_query_tables_locked()2326 bool is_query_tables_locked() const {
2327 return (lock_tables_state == LTS_LOCKED);
2328 }
2329
2330 /**
2331 Number of tables which were open by open_tables() and to be locked
2332 by lock_tables().
2333 Note that we set this member only in some cases, when this value
2334 needs to be passed from open_tables() to lock_tables() which are
2335 separated by some amount of code.
2336 */
2337 uint table_count;
2338
2339 /*
2340 These constructor and destructor serve for creation/destruction
2341 of Query_tables_list instances which are used as backup storage.
2342 */
Query_tables_list()2343 Query_tables_list() {}
~Query_tables_list()2344 ~Query_tables_list() {}
2345
2346 /* Initializes (or resets) Query_tables_list object for "real" use. */
2347 void reset_query_tables_list(bool init);
2348 void destroy_query_tables_list();
set_query_tables_list(Query_tables_list * state)2349 void set_query_tables_list(Query_tables_list *state) {
2350 *this = std::move(*state);
2351 }
2352
2353 /*
2354 Direct addition to the list of query tables.
2355 If you are using this function, you must ensure that the table
2356 object, in particular table->db member, is initialized.
2357 */
add_to_query_tables(TABLE_LIST * table)2358 void add_to_query_tables(TABLE_LIST *table) {
2359 *(table->prev_global = query_tables_last) = table;
2360 query_tables_last = &table->next_global;
2361 }
requires_prelocking()2362 bool requires_prelocking() { return query_tables_own_last; }
mark_as_requiring_prelocking(TABLE_LIST ** tables_own_last)2363 void mark_as_requiring_prelocking(TABLE_LIST **tables_own_last) {
2364 query_tables_own_last = tables_own_last;
2365 }
2366 /* Return pointer to first not-own table in query-tables or 0 */
first_not_own_table()2367 TABLE_LIST *first_not_own_table() {
2368 return (query_tables_own_last ? *query_tables_own_last : nullptr);
2369 }
chop_off_not_own_tables()2370 void chop_off_not_own_tables() {
2371 if (query_tables_own_last) {
2372 *query_tables_own_last = nullptr;
2373 query_tables_last = query_tables_own_last;
2374 query_tables_own_last = nullptr;
2375 }
2376 }
2377
2378 /**
2379 All types of unsafe statements.
2380
2381 @note The int values of the enum elements are used to point to
2382 bits in two bitmaps in two different places:
2383
2384 - Query_tables_list::binlog_stmt_flags
2385 - THD::binlog_unsafe_warning_flags
2386
2387 Hence in practice this is not an enum at all, but a map from
2388 symbols to bit indexes.
2389
2390 The ordering of elements in this enum must correspond to the order of
2391 elements in the array binlog_stmt_unsafe_errcode.
2392 */
2393 enum enum_binlog_stmt_unsafe {
2394 /**
2395 SELECT..LIMIT is unsafe because the set of rows returned cannot
2396 be predicted.
2397 */
2398 BINLOG_STMT_UNSAFE_LIMIT = 0,
2399 /**
2400 Access to log tables is unsafe because slave and master probably
2401 log different things.
2402 */
2403 BINLOG_STMT_UNSAFE_SYSTEM_TABLE,
2404 /**
2405 Inserting into an autoincrement column in a stored routine is unsafe.
2406 Even with just one autoincrement column, if the routine is invoked more
2407 than once slave is not guaranteed to execute the statement graph same way
2408 as the master. And since it's impossible to estimate how many times a
2409 routine can be invoked at the query pre-execution phase (see lock_tables),
2410 the statement is marked pessimistically unsafe.
2411 */
2412 BINLOG_STMT_UNSAFE_AUTOINC_COLUMNS,
2413 /**
2414 Using a UDF (user-defined function) is unsafe.
2415 */
2416 BINLOG_STMT_UNSAFE_UDF,
2417 /**
2418 Using most system variables is unsafe, because slave may run
2419 with different options than master.
2420 */
2421 BINLOG_STMT_UNSAFE_SYSTEM_VARIABLE,
2422 /**
2423 Using some functions is unsafe (e.g., UUID).
2424 */
2425 BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION,
2426
2427 /**
2428 Mixing transactional and non-transactional statements are unsafe if
2429 non-transactional reads or writes are occur after transactional
2430 reads or writes inside a transaction.
2431 */
2432 BINLOG_STMT_UNSAFE_NONTRANS_AFTER_TRANS,
2433
2434 /**
2435 Mixing self-logging and non-self-logging engines in a statement
2436 is unsafe.
2437 */
2438 BINLOG_STMT_UNSAFE_MULTIPLE_ENGINES_AND_SELF_LOGGING_ENGINE,
2439
2440 /**
2441 Statements that read from both transactional and non-transactional
2442 tables and write to any of them are unsafe.
2443 */
2444 BINLOG_STMT_UNSAFE_MIXED_STATEMENT,
2445
2446 /**
2447 INSERT...IGNORE SELECT is unsafe because which rows are ignored depends
2448 on the order that rows are retrieved by SELECT. This order cannot be
2449 predicted and may differ on master and the slave.
2450 */
2451 BINLOG_STMT_UNSAFE_INSERT_IGNORE_SELECT,
2452
2453 /**
2454 INSERT...SELECT...UPDATE is unsafe because which rows are updated depends
2455 on the order that rows are retrieved by SELECT. This order cannot be
2456 predicted and may differ on master and the slave.
2457 */
2458 BINLOG_STMT_UNSAFE_INSERT_SELECT_UPDATE,
2459
2460 /**
2461 Query that writes to a table with auto_inc column after selecting from
2462 other tables are unsafe as the order in which the rows are retrieved by
2463 select may differ on master and slave.
2464 */
2465 BINLOG_STMT_UNSAFE_WRITE_AUTOINC_SELECT,
2466
2467 /**
2468 INSERT...REPLACE SELECT is unsafe because which rows are replaced depends
2469 on the order that rows are retrieved by SELECT. This order cannot be
2470 predicted and may differ on master and the slave.
2471 */
2472 BINLOG_STMT_UNSAFE_REPLACE_SELECT,
2473
2474 /**
2475 CREATE TABLE... IGNORE... SELECT is unsafe because which rows are ignored
2476 depends on the order that rows are retrieved by SELECT. This order cannot
2477 be predicted and may differ on master and the slave.
2478 */
2479 BINLOG_STMT_UNSAFE_CREATE_IGNORE_SELECT,
2480
2481 /**
2482 CREATE TABLE...REPLACE... SELECT is unsafe because which rows are replaced
2483 depends on the order that rows are retrieved from SELECT. This order
2484 cannot be predicted and may differ on master and the slave
2485 */
2486 BINLOG_STMT_UNSAFE_CREATE_REPLACE_SELECT,
2487
2488 /**
2489 CREATE TABLE...SELECT on a table with auto-increment column is unsafe
2490 because which rows are replaced depends on the order that rows are
2491 retrieved from SELECT. This order cannot be predicted and may differ on
2492 master and the slave
2493 */
2494 BINLOG_STMT_UNSAFE_CREATE_SELECT_AUTOINC,
2495
2496 /**
2497 UPDATE...IGNORE is unsafe because which rows are ignored depends on the
2498 order that rows are updated. This order cannot be predicted and may differ
2499 on master and the slave.
2500 */
2501 BINLOG_STMT_UNSAFE_UPDATE_IGNORE,
2502
2503 /**
2504 INSERT... ON DUPLICATE KEY UPDATE on a table with more than one
2505 UNIQUE KEYS is unsafe.
2506 */
2507 BINLOG_STMT_UNSAFE_INSERT_TWO_KEYS,
2508
2509 /**
2510 INSERT into auto-inc field which is not the first part in composed
2511 primary key.
2512 */
2513 BINLOG_STMT_UNSAFE_AUTOINC_NOT_FIRST,
2514
2515 /**
2516 Using a plugin is unsafe.
2517 */
2518 BINLOG_STMT_UNSAFE_FULLTEXT_PLUGIN,
2519 BINLOG_STMT_UNSAFE_SKIP_LOCKED,
2520 BINLOG_STMT_UNSAFE_NOWAIT,
2521
2522 /**
2523 XA transactions and statements.
2524 */
2525 BINLOG_STMT_UNSAFE_XA,
2526
2527 /**
2528 If a substatement inserts into or updates a table that has a column with
2529 an unsafe DEFAULT expression, it may not have the same effect on the
2530 slave.
2531 */
2532 BINLOG_STMT_UNSAFE_DEFAULT_EXPRESSION_IN_SUBSTATEMENT,
2533
2534 /* the last element of this enumeration type. */
2535 BINLOG_STMT_UNSAFE_COUNT
2536 };
2537 /**
2538 This has all flags from 0 (inclusive) to BINLOG_STMT_FLAG_COUNT
2539 (exclusive) set.
2540 */
2541 static const int BINLOG_STMT_UNSAFE_ALL_FLAGS =
2542 ((1 << BINLOG_STMT_UNSAFE_COUNT) - 1);
2543
2544 /**
2545 Maps elements of enum_binlog_stmt_unsafe to error codes.
2546 */
2547 static const int binlog_stmt_unsafe_errcode[BINLOG_STMT_UNSAFE_COUNT];
2548
2549 /**
2550 Determine if this statement is marked as unsafe.
2551
2552 @retval 0 if the statement is not marked as unsafe.
2553 @retval nonzero if the statement is marked as unsafe.
2554 */
is_stmt_unsafe()2555 inline bool is_stmt_unsafe() const { return get_stmt_unsafe_flags() != 0; }
2556
is_stmt_unsafe(enum_binlog_stmt_unsafe unsafe)2557 inline bool is_stmt_unsafe(enum_binlog_stmt_unsafe unsafe) {
2558 return binlog_stmt_flags & (1 << unsafe);
2559 }
2560
2561 /**
2562 Flag the current (top-level) statement as unsafe.
2563 The flag will be reset after the statement has finished.
2564
2565 @param unsafe_type The type of unsafety: one of the @c
2566 BINLOG_STMT_FLAG_UNSAFE_* flags in @c enum_binlog_stmt_flag.
2567 */
set_stmt_unsafe(enum_binlog_stmt_unsafe unsafe_type)2568 inline void set_stmt_unsafe(enum_binlog_stmt_unsafe unsafe_type) {
2569 DBUG_TRACE;
2570 DBUG_ASSERT(unsafe_type >= 0 && unsafe_type < BINLOG_STMT_UNSAFE_COUNT);
2571 binlog_stmt_flags |= (1U << unsafe_type);
2572 return;
2573 }
2574
2575 /**
2576 Set the bits of binlog_stmt_flags determining the type of
2577 unsafeness of the current statement. No existing bits will be
2578 cleared, but new bits may be set.
2579
2580 @param flags A binary combination of zero or more bits, (1<<flag)
2581 where flag is a member of enum_binlog_stmt_unsafe.
2582 */
set_stmt_unsafe_flags(uint32 flags)2583 inline void set_stmt_unsafe_flags(uint32 flags) {
2584 DBUG_TRACE;
2585 DBUG_ASSERT((flags & ~BINLOG_STMT_UNSAFE_ALL_FLAGS) == 0);
2586 binlog_stmt_flags |= flags;
2587 return;
2588 }
2589
2590 /**
2591 Return a binary combination of all unsafe warnings for the
2592 statement. If the statement has been marked as unsafe by the
2593 'flag' member of enum_binlog_stmt_unsafe, then the return value
2594 from this function has bit (1<<flag) set to 1.
2595 */
get_stmt_unsafe_flags()2596 inline uint32 get_stmt_unsafe_flags() const {
2597 DBUG_TRACE;
2598 return binlog_stmt_flags & BINLOG_STMT_UNSAFE_ALL_FLAGS;
2599 }
2600
2601 /**
2602 Determine if this statement is a row injection.
2603
2604 @retval 0 if the statement is not a row injection
2605 @retval nonzero if the statement is a row injection
2606 */
is_stmt_row_injection()2607 inline bool is_stmt_row_injection() const {
2608 return binlog_stmt_flags &
2609 (1U << (BINLOG_STMT_UNSAFE_COUNT + BINLOG_STMT_TYPE_ROW_INJECTION));
2610 }
2611
2612 /**
2613 Flag the statement as a row injection. A row injection is either
2614 a BINLOG statement, or a row event in the relay log executed by
2615 the slave SQL thread.
2616 */
set_stmt_row_injection()2617 inline void set_stmt_row_injection() {
2618 DBUG_TRACE;
2619 binlog_stmt_flags |=
2620 (1U << (BINLOG_STMT_UNSAFE_COUNT + BINLOG_STMT_TYPE_ROW_INJECTION));
2621 return;
2622 }
2623
2624 enum enum_stmt_accessed_table {
2625 /*
2626 If a transactional table is about to be read. Note that
2627 a write implies a read.
2628 */
2629 STMT_READS_TRANS_TABLE = 0,
2630 /*
2631 If a non-transactional table is about to be read. Note that
2632 a write implies a read.
2633 */
2634 STMT_READS_NON_TRANS_TABLE,
2635 /*
2636 If a temporary transactional table is about to be read. Note
2637 that a write implies a read.
2638 */
2639 STMT_READS_TEMP_TRANS_TABLE,
2640 /*
2641 If a temporary non-transactional table is about to be read. Note
2642 that a write implies a read.
2643 */
2644 STMT_READS_TEMP_NON_TRANS_TABLE,
2645 /*
2646 If a transactional table is about to be updated.
2647 */
2648 STMT_WRITES_TRANS_TABLE,
2649 /*
2650 If a non-transactional table is about to be updated.
2651 */
2652 STMT_WRITES_NON_TRANS_TABLE,
2653 /*
2654 If a temporary transactional table is about to be updated.
2655 */
2656 STMT_WRITES_TEMP_TRANS_TABLE,
2657 /*
2658 If a temporary non-transactional table is about to be updated.
2659 */
2660 STMT_WRITES_TEMP_NON_TRANS_TABLE,
2661 /*
2662 The last element of the enumeration. Please, if necessary add
2663 anything before this.
2664 */
2665 STMT_ACCESS_TABLE_COUNT
2666 };
2667
2668 #ifndef DBUG_OFF
stmt_accessed_table_string(enum_stmt_accessed_table accessed_table)2669 static inline const char *stmt_accessed_table_string(
2670 enum_stmt_accessed_table accessed_table) {
2671 switch (accessed_table) {
2672 case STMT_READS_TRANS_TABLE:
2673 return "STMT_READS_TRANS_TABLE";
2674 break;
2675 case STMT_READS_NON_TRANS_TABLE:
2676 return "STMT_READS_NON_TRANS_TABLE";
2677 break;
2678 case STMT_READS_TEMP_TRANS_TABLE:
2679 return "STMT_READS_TEMP_TRANS_TABLE";
2680 break;
2681 case STMT_READS_TEMP_NON_TRANS_TABLE:
2682 return "STMT_READS_TEMP_NON_TRANS_TABLE";
2683 break;
2684 case STMT_WRITES_TRANS_TABLE:
2685 return "STMT_WRITES_TRANS_TABLE";
2686 break;
2687 case STMT_WRITES_NON_TRANS_TABLE:
2688 return "STMT_WRITES_NON_TRANS_TABLE";
2689 break;
2690 case STMT_WRITES_TEMP_TRANS_TABLE:
2691 return "STMT_WRITES_TEMP_TRANS_TABLE";
2692 break;
2693 case STMT_WRITES_TEMP_NON_TRANS_TABLE:
2694 return "STMT_WRITES_TEMP_NON_TRANS_TABLE";
2695 break;
2696 case STMT_ACCESS_TABLE_COUNT:
2697 default:
2698 DBUG_ASSERT(0);
2699 break;
2700 }
2701 MY_ASSERT_UNREACHABLE();
2702 return "";
2703 }
2704 #endif /* DBUG */
2705
2706 #define BINLOG_DIRECT_ON \
2707 0xF0 /* unsafe when \
2708 --binlog-direct-non-trans-updates \
2709 is ON */
2710
2711 #define BINLOG_DIRECT_OFF \
2712 0xF /* unsafe when \
2713 --binlog-direct-non-trans-updates \
2714 is OFF */
2715
2716 #define TRX_CACHE_EMPTY 0x33 /* unsafe when trx-cache is empty */
2717
2718 #define TRX_CACHE_NOT_EMPTY 0xCC /* unsafe when trx-cache is not empty */
2719
2720 #define IL_LT_REPEATABLE 0xAA /* unsafe when < ISO_REPEATABLE_READ */
2721
2722 #define IL_GTE_REPEATABLE 0x55 /* unsafe when >= ISO_REPEATABLE_READ */
2723
2724 /**
2725 Sets the type of table that is about to be accessed while executing a
2726 statement.
2727
2728 @param accessed_table Enumeration type that defines the type of table,
2729 e.g. temporary, transactional, non-transactional.
2730 */
set_stmt_accessed_table(enum_stmt_accessed_table accessed_table)2731 inline void set_stmt_accessed_table(enum_stmt_accessed_table accessed_table) {
2732 DBUG_TRACE;
2733
2734 DBUG_ASSERT(accessed_table >= 0 &&
2735 accessed_table < STMT_ACCESS_TABLE_COUNT);
2736 stmt_accessed_table_flag |= (1U << accessed_table);
2737
2738 return;
2739 }
2740
2741 /**
2742 Checks if a type of table is about to be accessed while executing a
2743 statement.
2744
2745 @param accessed_table Enumeration type that defines the type of table,
2746 e.g. temporary, transactional, non-transactional.
2747
2748 @retval true if the type of the table is about to be accessed
2749 @retval false otherwise
2750 */
stmt_accessed_table(enum_stmt_accessed_table accessed_table)2751 inline bool stmt_accessed_table(enum_stmt_accessed_table accessed_table) {
2752 DBUG_TRACE;
2753
2754 DBUG_ASSERT(accessed_table >= 0 &&
2755 accessed_table < STMT_ACCESS_TABLE_COUNT);
2756
2757 return (stmt_accessed_table_flag & (1U << accessed_table)) != 0;
2758 }
2759
2760 /*
2761 Checks if a mixed statement is unsafe.
2762
2763
2764 @param in_multi_stmt_transaction_mode defines if there is an on-going
2765 multi-transactional statement.
2766 @param binlog_direct defines if --binlog-direct-non-trans-updates is
2767 active.
2768 @param trx_cache_is_not_empty defines if the trx-cache is empty or not.
2769 @param trx_isolation defines the isolation level.
2770
2771 @return
2772 @retval true if the mixed statement is unsafe
2773 @retval false otherwise
2774 */
is_mixed_stmt_unsafe(bool in_multi_stmt_transaction_mode,bool binlog_direct,bool trx_cache_is_not_empty,uint tx_isolation)2775 inline bool is_mixed_stmt_unsafe(bool in_multi_stmt_transaction_mode,
2776 bool binlog_direct,
2777 bool trx_cache_is_not_empty,
2778 uint tx_isolation) {
2779 bool unsafe = false;
2780
2781 if (in_multi_stmt_transaction_mode) {
2782 uint condition =
2783 (binlog_direct ? BINLOG_DIRECT_ON : BINLOG_DIRECT_OFF) &
2784 (trx_cache_is_not_empty ? TRX_CACHE_NOT_EMPTY : TRX_CACHE_EMPTY) &
2785 (tx_isolation >= ISO_REPEATABLE_READ ? IL_GTE_REPEATABLE
2786 : IL_LT_REPEATABLE);
2787
2788 unsafe = (binlog_unsafe_map[stmt_accessed_table_flag] & condition);
2789
2790 #if !defined(DBUG_OFF)
2791 DBUG_PRINT("LEX::is_mixed_stmt_unsafe",
2792 ("RESULT %02X %02X %02X\n", condition,
2793 binlog_unsafe_map[stmt_accessed_table_flag],
2794 (binlog_unsafe_map[stmt_accessed_table_flag] & condition)));
2795
2796 int type_in = 0;
2797 for (; type_in < STMT_ACCESS_TABLE_COUNT; type_in++) {
2798 if (stmt_accessed_table((enum_stmt_accessed_table)type_in))
2799 DBUG_PRINT("LEX::is_mixed_stmt_unsafe",
2800 ("ACCESSED %s ", stmt_accessed_table_string(
2801 (enum_stmt_accessed_table)type_in)));
2802 }
2803 #endif
2804 }
2805
2806 if (stmt_accessed_table(STMT_WRITES_NON_TRANS_TABLE) &&
2807 stmt_accessed_table(STMT_READS_TRANS_TABLE) &&
2808 tx_isolation < ISO_REPEATABLE_READ)
2809 unsafe = true;
2810 else if (stmt_accessed_table(STMT_WRITES_TEMP_NON_TRANS_TABLE) &&
2811 stmt_accessed_table(STMT_READS_TRANS_TABLE) &&
2812 tx_isolation < ISO_REPEATABLE_READ)
2813 unsafe = true;
2814
2815 return (unsafe);
2816 }
2817
2818 /**
2819 true if the parsed tree contains references to stored procedures
2820 or functions, false otherwise
2821 */
uses_stored_routines()2822 bool uses_stored_routines() const { return sroutines_list.elements != 0; }
2823
set_using_match()2824 void set_using_match() { using_match = true; }
get_using_match()2825 bool get_using_match() { return using_match; }
2826
set_stmt_unsafe_with_mixed_mode()2827 void set_stmt_unsafe_with_mixed_mode() { stmt_unsafe_with_mixed_mode = true; }
is_stmt_unsafe_with_mixed_mode()2828 bool is_stmt_unsafe_with_mixed_mode() const {
2829 return stmt_unsafe_with_mixed_mode;
2830 }
2831
2832 private:
2833 /**
2834 Enumeration listing special types of statements.
2835
2836 Currently, the only possible type is ROW_INJECTION.
2837 */
2838 enum enum_binlog_stmt_type {
2839 /**
2840 The statement is a row injection (i.e., either a BINLOG
2841 statement or a row event executed by the slave SQL thread).
2842 */
2843 BINLOG_STMT_TYPE_ROW_INJECTION = 0,
2844
2845 /** The last element of this enumeration type. */
2846 BINLOG_STMT_TYPE_COUNT
2847 };
2848
2849 /**
2850 Bit field indicating the type of statement.
2851
2852 There are two groups of bits:
2853
2854 - The low BINLOG_STMT_UNSAFE_COUNT bits indicate the types of
2855 unsafeness that the current statement has.
2856
2857 - The next BINLOG_STMT_TYPE_COUNT bits indicate if the statement
2858 is of some special type.
2859
2860 This must be a member of LEX, not of THD: each stored procedure
2861 needs to remember its unsafeness state between calls and each
2862 stored procedure has its own LEX object (but no own THD object).
2863 */
2864 uint32 binlog_stmt_flags;
2865
2866 /**
2867 Bit field that determines the type of tables that are about to be
2868 be accessed while executing a statement.
2869 */
2870 uint32 stmt_accessed_table_flag;
2871
2872 /**
2873 It will be set true if 'MATCH () AGAINST' is used in the statement.
2874 */
2875 bool using_match;
2876
2877 /**
2878 This flag is set to true if statement is unsafe to be binlogged in STATEMENT
2879 format, when in MIXED mode.
2880 Currently this flag is set to true if stored program used in statement has
2881 CREATE/DROP temporary table operation(s) as sub-statement(s).
2882 */
2883 bool stmt_unsafe_with_mixed_mode{false};
2884 };
2885
2886 /*
2887 st_parsing_options contains the flags for constructions that are
2888 allowed in the current statement.
2889 */
2890
2891 struct st_parsing_options {
2892 bool allows_variable;
2893 bool allows_select_into;
2894
st_parsing_optionsst_parsing_options2895 st_parsing_options() { reset(); }
2896 void reset();
2897 };
2898
2899 /**
2900 The state of the lexical parser, when parsing comments.
2901 */
2902 enum enum_comment_state {
2903 /**
2904 Not parsing comments.
2905 */
2906 NO_COMMENT,
2907
2908 /**
2909 Parsing comments that need to be preserved.
2910 (Copy '/' '*' and '*' '/' sequences to the preprocessed buffer.)
2911 Typically, these are user comments '/' '*' ... '*' '/'.
2912 */
2913 PRESERVE_COMMENT,
2914
2915 /**
2916 Parsing comments that need to be discarded.
2917 (Don't copy '/' '*' '!' and '*' '/' sequences to the preprocessed buffer.)
2918 Typically, these are special comments '/' '*' '!' ... '*' '/',
2919 or '/' '*' '!' 'M' 'M' 'm' 'm' 'm' ... '*' '/', where the comment
2920 markers should not be expanded.
2921 */
2922 DISCARD_COMMENT
2923 };
2924
2925 /**
2926 This class represents the character input stream consumed during lexical
2927 analysis.
2928
2929 In addition to consuming the input stream, this class performs some comment
2930 pre processing, by filtering out out-of-bound special text from the query
2931 input stream.
2932
2933 Two buffers, with pointers inside each, are maintained in parallel. The
2934 'raw' buffer is the original query text, which may contain out-of-bound
2935 comments. The 'cpp' (for comments pre processor) is the pre-processed buffer
2936 that contains only the query text that should be seen once out-of-bound data
2937 is removed.
2938 */
2939
2940 class Lex_input_stream {
2941 public:
2942 /**
2943 Constructor
2944
2945 @param grammar_selector_token_arg See grammar_selector_token.
2946 */
2947
Lex_input_stream(uint grammar_selector_token_arg)2948 explicit Lex_input_stream(uint grammar_selector_token_arg)
2949 : grammar_selector_token(grammar_selector_token_arg) {}
2950
2951 /**
2952 Object initializer. Must be called before usage.
2953
2954 @retval false OK
2955 @retval true Error
2956 */
2957 bool init(THD *thd, const char *buff, size_t length);
2958
2959 void reset(const char *buff, size_t length);
2960
2961 /**
2962 Set the echo mode.
2963
2964 When echo is true, characters parsed from the raw input stream are
2965 preserved. When false, characters parsed are silently ignored.
2966 @param echo the echo mode.
2967 */
set_echo(bool echo)2968 void set_echo(bool echo) { m_echo = echo; }
2969
save_in_comment_state()2970 void save_in_comment_state() {
2971 m_echo_saved = m_echo;
2972 in_comment_saved = in_comment;
2973 }
2974
restore_in_comment_state()2975 void restore_in_comment_state() {
2976 m_echo = m_echo_saved;
2977 in_comment = in_comment_saved;
2978 }
2979
2980 /**
2981 Skip binary from the input stream.
2982 @param n number of bytes to accept.
2983 */
skip_binary(int n)2984 void skip_binary(int n) {
2985 DBUG_ASSERT(m_ptr + n <= m_end_of_query);
2986 if (m_echo) {
2987 memcpy(m_cpp_ptr, m_ptr, n);
2988 m_cpp_ptr += n;
2989 }
2990 m_ptr += n;
2991 }
2992
2993 /**
2994 Get a character, and advance in the stream.
2995 @return the next character to parse.
2996 */
yyGet()2997 unsigned char yyGet() {
2998 DBUG_ASSERT(m_ptr <= m_end_of_query);
2999 char c = *m_ptr++;
3000 if (m_echo) *m_cpp_ptr++ = c;
3001 return c;
3002 }
3003
3004 /**
3005 Get the last character accepted.
3006 @return the last character accepted.
3007 */
yyGetLast()3008 unsigned char yyGetLast() const { return m_ptr[-1]; }
3009
3010 /**
3011 Look at the next character to parse, but do not accept it.
3012 */
yyPeek()3013 unsigned char yyPeek() const {
3014 DBUG_ASSERT(m_ptr <= m_end_of_query);
3015 return m_ptr[0];
3016 }
3017
3018 /**
3019 Look ahead at some character to parse.
3020 @param n offset of the character to look up
3021 */
yyPeekn(int n)3022 unsigned char yyPeekn(int n) const {
3023 DBUG_ASSERT(m_ptr + n <= m_end_of_query);
3024 return m_ptr[n];
3025 }
3026
3027 /**
3028 Cancel the effect of the last yyGet() or yySkip().
3029 Note that the echo mode should not change between calls to yyGet / yySkip
3030 and yyUnget. The caller is responsible for ensuring that.
3031 */
yyUnget()3032 void yyUnget() {
3033 m_ptr--;
3034 if (m_echo) m_cpp_ptr--;
3035 }
3036
3037 /**
3038 Accept a character, by advancing the input stream.
3039 */
yySkip()3040 void yySkip() {
3041 DBUG_ASSERT(m_ptr <= m_end_of_query);
3042 if (m_echo)
3043 *m_cpp_ptr++ = *m_ptr++;
3044 else
3045 m_ptr++;
3046 }
3047
3048 /**
3049 Accept multiple characters at once.
3050 @param n the number of characters to accept.
3051 */
yySkipn(int n)3052 void yySkipn(int n) {
3053 DBUG_ASSERT(m_ptr + n <= m_end_of_query);
3054 if (m_echo) {
3055 memcpy(m_cpp_ptr, m_ptr, n);
3056 m_cpp_ptr += n;
3057 }
3058 m_ptr += n;
3059 }
3060
3061 /**
3062 Puts a character back into the stream, canceling
3063 the effect of the last yyGet() or yySkip().
3064 Note that the echo mode should not change between calls
3065 to unput, get, or skip from the stream.
3066 */
yyUnput(char ch)3067 char *yyUnput(char ch) {
3068 *--m_ptr = ch;
3069 if (m_echo) m_cpp_ptr--;
3070 return m_ptr;
3071 }
3072
3073 /**
3074 Inject a character into the pre-processed stream.
3075
3076 Note, this function is used to inject a space instead of multi-character
3077 C-comment. Thus there is no boundary checks here (basically, we replace
3078 N-chars by 1-char here).
3079 */
cpp_inject(char ch)3080 char *cpp_inject(char ch) {
3081 *m_cpp_ptr = ch;
3082 return ++m_cpp_ptr;
3083 }
3084
3085 /**
3086 End of file indicator for the query text to parse.
3087 @return true if there are no more characters to parse
3088 */
eof()3089 bool eof() const { return (m_ptr >= m_end_of_query); }
3090
3091 /**
3092 End of file indicator for the query text to parse.
3093 @param n number of characters expected
3094 @return true if there are less than n characters to parse
3095 */
eof(int n)3096 bool eof(int n) const { return ((m_ptr + n) >= m_end_of_query); }
3097
3098 /** Get the raw query buffer. */
get_buf()3099 const char *get_buf() const { return m_buf; }
3100
3101 /** Get the pre-processed query buffer. */
get_cpp_buf()3102 const char *get_cpp_buf() const { return m_cpp_buf; }
3103
3104 /** Get the end of the raw query buffer. */
get_end_of_query()3105 const char *get_end_of_query() const { return m_end_of_query; }
3106
3107 /** Mark the stream position as the start of a new token. */
start_token()3108 void start_token() {
3109 m_tok_start = m_ptr;
3110 m_tok_end = m_ptr;
3111
3112 m_cpp_tok_start = m_cpp_ptr;
3113 m_cpp_tok_end = m_cpp_ptr;
3114 }
3115
3116 /**
3117 Adjust the starting position of the current token.
3118 This is used to compensate for starting whitespace.
3119 */
restart_token()3120 void restart_token() {
3121 m_tok_start = m_ptr;
3122 m_cpp_tok_start = m_cpp_ptr;
3123 }
3124
3125 /** Get the token start position, in the raw buffer. */
get_tok_start()3126 const char *get_tok_start() const { return m_tok_start; }
3127
3128 /** Get the token start position, in the pre-processed buffer. */
get_cpp_tok_start()3129 const char *get_cpp_tok_start() const { return m_cpp_tok_start; }
3130
3131 /** Get the token end position, in the raw buffer. */
get_tok_end()3132 const char *get_tok_end() const { return m_tok_end; }
3133
3134 /** Get the token end position, in the pre-processed buffer. */
get_cpp_tok_end()3135 const char *get_cpp_tok_end() const { return m_cpp_tok_end; }
3136
3137 /** Get the current stream pointer, in the raw buffer. */
get_ptr()3138 const char *get_ptr() const { return m_ptr; }
3139
3140 /** Get the current stream pointer, in the pre-processed buffer. */
get_cpp_ptr()3141 const char *get_cpp_ptr() const { return m_cpp_ptr; }
3142
3143 /** Get the length of the current token, in the raw buffer. */
yyLength()3144 uint yyLength() const {
3145 /*
3146 The assumption is that the lexical analyser is always 1 character ahead,
3147 which the -1 account for.
3148 */
3149 DBUG_ASSERT(m_ptr > m_tok_start);
3150 return (uint)((m_ptr - m_tok_start) - 1);
3151 }
3152
3153 /** Get the utf8-body string. */
get_body_utf8_str()3154 const char *get_body_utf8_str() const { return m_body_utf8; }
3155
3156 /** Get the utf8-body length. */
get_body_utf8_length()3157 uint get_body_utf8_length() const {
3158 return (uint)(m_body_utf8_ptr - m_body_utf8);
3159 }
3160
3161 void body_utf8_start(THD *thd, const char *begin_ptr);
3162 void body_utf8_append(const char *ptr);
3163 void body_utf8_append(const char *ptr, const char *end_ptr);
3164 void body_utf8_append_literal(THD *thd, const LEX_STRING *txt,
3165 const CHARSET_INFO *txt_cs,
3166 const char *end_ptr);
3167
3168 uint get_lineno(const char *raw_ptr) const;
3169
3170 /** Current thread. */
3171 THD *m_thd;
3172
3173 /** Current line number. */
3174 uint yylineno;
3175
3176 /** Length of the last token parsed. */
3177 uint yytoklen;
3178
3179 /** Interface with bison, value of the last token parsed. */
3180 Lexer_yystype *yylval;
3181
3182 /**
3183 LALR(2) resolution, look ahead token.
3184 Value of the next token to return, if any,
3185 or -1, if no token was parsed in advance.
3186 Note: 0 is a legal token, and represents YYEOF.
3187 */
3188 int lookahead_token;
3189
3190 /** LALR(2) resolution, value of the look ahead token.*/
3191 Lexer_yystype *lookahead_yylval;
3192
3193 /// Skip adding of the current token's digest since it is already added
3194 ///
3195 /// Usually we calculate a digest token by token at the top-level function
3196 /// of the lexer: MYSQLlex(). However, some complex ("hintable") tokens break
3197 /// that data flow: for example, the `SELECT /*+ HINT(t) */` is the single
3198 /// token from the main parser's point of view, and we add the "SELECT"
3199 /// keyword to the digest buffer right after the lex_one_token() call,
3200 /// but the "/*+ HINT(t) */" is a sequence of separate tokens from the hint
3201 /// parser's point of view, and we add those tokens to the digest buffer
3202 /// *inside* the lex_one_token() call. Thus, the usual data flow adds
3203 /// tokens from the "/*+ HINT(t) */" string first, and only than it appends
3204 /// the "SELECT" keyword token to that stream: "/*+ HINT(t) */ SELECT".
3205 /// This is not acceptable, since we use the digest buffer to restore
3206 /// query strings in their normalized forms, so the order of added tokens is
3207 /// important. Thus, we add tokens of "hintable" keywords to a digest buffer
3208 /// right in the hint parser and skip adding of them at the caller with the
3209 /// help of skip_digest flag.
3210 bool skip_digest;
3211
3212 void add_digest_token(uint token, Lexer_yystype *yylval);
3213
3214 void reduce_digest_token(uint token_left, uint token_right);
3215
3216 /**
3217 True if this scanner tokenizes a partial query (partition expression,
3218 generated column expression etc.)
3219
3220 @return true if parsing a partial query, otherwise false.
3221 */
is_partial_parser()3222 bool is_partial_parser() const { return grammar_selector_token >= 0; }
3223
3224 /**
3225 Outputs warnings on deprecated charsets in complete SQL statements
3226
3227 @param [in] cs The character set/collation to check for a deprecation.
3228 @param [in] alias The name/alias of @p cs.
3229 */
warn_on_deprecated_charset(const CHARSET_INFO * cs,const char * alias)3230 void warn_on_deprecated_charset(const CHARSET_INFO *cs,
3231 const char *alias) const {
3232 if (!is_partial_parser()) {
3233 ::warn_on_deprecated_charset(m_thd, cs, alias);
3234 }
3235 }
3236
3237 /**
3238 Outputs warnings on deprecated collations in complete SQL statements
3239
3240 @param [in] collation The collation to check for a deprecation.
3241 */
warn_on_deprecated_collation(const CHARSET_INFO * collation)3242 void warn_on_deprecated_collation(const CHARSET_INFO *collation) const {
3243 if (!is_partial_parser()) {
3244 ::warn_on_deprecated_collation(m_thd, collation);
3245 }
3246 }
3247
3248 const CHARSET_INFO *query_charset;
3249
3250 private:
3251 /** Pointer to the current position in the raw input stream. */
3252 char *m_ptr;
3253
3254 /** Starting position of the last token parsed, in the raw buffer. */
3255 const char *m_tok_start;
3256
3257 /** Ending position of the previous token parsed, in the raw buffer. */
3258 const char *m_tok_end;
3259
3260 /** End of the query text in the input stream, in the raw buffer. */
3261 const char *m_end_of_query;
3262
3263 /** Begining of the query text in the input stream, in the raw buffer. */
3264 const char *m_buf;
3265
3266 /** Length of the raw buffer. */
3267 size_t m_buf_length;
3268
3269 /** Echo the parsed stream to the pre-processed buffer. */
3270 bool m_echo;
3271 bool m_echo_saved;
3272
3273 /** Pre-processed buffer. */
3274 char *m_cpp_buf;
3275
3276 /** Pointer to the current position in the pre-processed input stream. */
3277 char *m_cpp_ptr;
3278
3279 /**
3280 Starting position of the last token parsed,
3281 in the pre-processed buffer.
3282 */
3283 const char *m_cpp_tok_start;
3284
3285 /**
3286 Ending position of the previous token parsed,
3287 in the pre-processed buffer.
3288 */
3289 const char *m_cpp_tok_end;
3290
3291 /** UTF8-body buffer created during parsing. */
3292 char *m_body_utf8;
3293
3294 /** Pointer to the current position in the UTF8-body buffer. */
3295 char *m_body_utf8_ptr;
3296
3297 /**
3298 Position in the pre-processed buffer. The query from m_cpp_buf to
3299 m_cpp_utf_processed_ptr is converted to UTF8-body.
3300 */
3301 const char *m_cpp_utf8_processed_ptr;
3302
3303 public:
3304 /** Current state of the lexical analyser. */
3305 enum my_lex_states next_state;
3306
3307 /**
3308 Position of ';' in the stream, to delimit multiple queries.
3309 This delimiter is in the raw buffer.
3310 */
3311 const char *found_semicolon;
3312
3313 /** Token character bitmaps, to detect 7bit strings. */
3314 uchar tok_bitmap;
3315
3316 /** SQL_MODE = IGNORE_SPACE. */
3317 bool ignore_space;
3318
3319 /**
3320 true if we're parsing a prepared statement: in this mode
3321 we should allow placeholders.
3322 */
3323 bool stmt_prepare_mode;
3324 /**
3325 true if we should allow multi-statements.
3326 */
3327 bool multi_statements;
3328
3329 /** State of the lexical analyser for comments. */
3330 enum_comment_state in_comment;
3331 enum_comment_state in_comment_saved;
3332
3333 /**
3334 Starting position of the TEXT_STRING or IDENT in the pre-processed
3335 buffer.
3336
3337 NOTE: this member must be used within MYSQLlex() function only.
3338 */
3339 const char *m_cpp_text_start;
3340
3341 /**
3342 Ending position of the TEXT_STRING or IDENT in the pre-processed
3343 buffer.
3344
3345 NOTE: this member must be used within MYSQLlex() function only.
3346 */
3347 const char *m_cpp_text_end;
3348
3349 /**
3350 Character set specified by the character-set-introducer.
3351
3352 NOTE: this member must be used within MYSQLlex() function only.
3353 */
3354 const CHARSET_INFO *m_underscore_cs;
3355
3356 /**
3357 Current statement digest instrumentation.
3358 */
3359 sql_digest_state *m_digest;
3360
3361 /**
3362 The synthetic 1st token to prepend token stream with.
3363
3364 This token value tricks parser to simulate multiple %start-ing points.
3365 Currently the grammar is aware of 4 such synthetic tokens:
3366 1. GRAMMAR_SELECTOR_PART for partitioning stuff from DD,
3367 2. GRAMMAR_SELECTOR_GCOL for generated column stuff from DD,
3368 3. GRAMMAR_SELECTOR_EXPR for generic single expressions from DD/.frm.
3369 4. GRAMMAR_SELECTOR_CTE for generic subquery expressions from CTEs.
3370 5. -1 when parsing with the main grammar (no grammar selector available).
3371
3372 @note yylex() is expected to return the value of type int:
3373 0 is for EOF and everything else for real token numbers.
3374 Bison, in its turn, generates positive token numbers.
3375 So, the negative grammar_selector_token means "not a token".
3376 In other words, -1 is "empty value".
3377 */
3378 const int grammar_selector_token;
3379
text_string_is_7bit()3380 bool text_string_is_7bit() const { return !(tok_bitmap & 0x80); }
3381 };
3382
3383 class LEX_COLUMN {
3384 public:
3385 String column;
3386 uint rights;
LEX_COLUMN(const String & x,const uint & y)3387 LEX_COLUMN(const String &x, const uint &y) : column(x), rights(y) {}
3388 };
3389
3390 enum class role_enum;
3391
3392 /*
3393 This structure holds information about grantor's context
3394 */
3395 class LEX_GRANT_AS {
3396 public:
3397 LEX_GRANT_AS();
3398 void cleanup();
3399
3400 public:
3401 bool grant_as_used;
3402 role_enum role_type;
3403 LEX_USER *user;
3404 List<LEX_USER> *role_list;
3405 };
3406
3407 /* The state of the lex parsing. This is saved in the THD struct */
3408
3409 struct LEX : public Query_tables_list {
3410 friend bool lex_start(THD *thd);
3411
3412 SELECT_LEX_UNIT *unit; ///< Outer-most query expression
3413 /// @todo: select_lex can be replaced with unit->first-select()
3414 SELECT_LEX *select_lex; ///< First query block
3415 SELECT_LEX *all_selects_list; ///< List of all query blocks
3416 private:
3417 /* current SELECT_LEX in parsing */
3418 SELECT_LEX *m_current_select;
3419
3420 public:
current_selectLEX3421 inline SELECT_LEX *current_select() const { return m_current_select; }
3422
3423 /*
3424 We want to keep current_thd out of header files, so the debug assert
3425 is moved to the .cc file.
3426 */
3427 void assert_ok_set_current_select();
set_current_selectLEX3428 inline void set_current_select(SELECT_LEX *select) {
3429 #ifndef DBUG_OFF
3430 assert_ok_set_current_select();
3431 #endif
3432 m_current_select = select;
3433 }
3434 /// @return true if this is an EXPLAIN statement
is_explainLEX3435 bool is_explain() const { return explain_format != nullptr; }
3436 bool is_explain_analyze = false;
3437 LEX_STRING name;
3438 char *help_arg;
3439 char *to_log; /* For PURGE MASTER LOGS TO */
3440 const char *x509_subject, *x509_issuer, *ssl_cipher;
3441 // Widcard from SHOW ... LIKE <wildcard> statements.
3442 String *wild;
3443 Query_result *result;
3444 LEX_STRING binlog_stmt_arg = {
3445 nullptr, 0}; ///< Argument of the BINLOG event statement.
3446 LEX_STRING ident;
3447 LEX_USER *grant_user;
3448 LEX_ALTER alter_password;
3449 enum_alter_user_attribute alter_user_attribute;
3450 LEX_STRING alter_user_comment_text;
3451 LEX_GRANT_AS grant_as;
3452 THD *thd;
3453 Value_generator *gcol_info;
3454
3455 /* Optimizer hints */
3456 Opt_hints_global *opt_hints_global;
3457
3458 /* maintain a list of used plugins for this LEX */
3459 typedef Prealloced_array<plugin_ref, INITIAL_LEX_PLUGIN_LIST_SIZE>
3460 Plugins_array;
3461 Plugins_array plugins;
3462
3463 /// Table being inserted into (may be a view)
3464 TABLE_LIST *insert_table;
3465 /// Leaf table being inserted into (always a base table)
3466 TABLE_LIST *insert_table_leaf;
3467
3468 /** SELECT of CREATE VIEW statement */
3469 LEX_STRING create_view_select;
3470
3471 /* Partition info structure filled in by PARTITION BY parse part */
3472 partition_info *part_info;
3473
3474 /*
3475 The definer of the object being created (view, trigger, stored routine).
3476 I.e. the value of DEFINER clause.
3477 */
3478 LEX_USER *definer;
3479
3480 List<LEX_USER> users_list;
3481 List<LEX_COLUMN> columns;
3482 List<LEX_CSTRING> dynamic_privileges;
3483 List<LEX_USER> *default_roles;
3484
3485 ulonglong bulk_insert_row_cnt;
3486
3487 // PURGE statement-specific fields:
3488 List<Item> purge_value_list;
3489
3490 // KILL statement-specific fields:
3491 List<Item> kill_value_list;
3492
3493 // other stuff:
3494 List<set_var_base> var_list;
3495 List<Item_func_set_user_var> set_var_list; // in-query assignment list
3496 /**
3497 List of placeholders ('?') for parameters of a prepared statement. Because
3498 we append to this list during parsing, it is naturally sorted by
3499 position of the '?' in the query string. The code which fills placeholders
3500 with user-supplied values, and the code which writes a query for
3501 statement-based logging, rely on this order.
3502 This list contains only real placeholders, not the clones which originate
3503 in a re-parsed CTE definition.
3504 */
3505 List<Item_param> param_list;
3506
3507 bool locate_var_assignment(const Name_string &name);
3508
insert_values_mapLEX3509 void insert_values_map(Field *f1, Field *f2) {
3510 if (!insert_update_values_map)
3511 insert_update_values_map = new std::map<Field *, Field *>;
3512 insert_update_values_map->insert(std::make_pair(f1, f2));
3513 }
clear_values_mapLEX3514 void clear_values_map() {
3515 if (insert_update_values_map) {
3516 insert_update_values_map->clear();
3517 delete insert_update_values_map;
3518 insert_update_values_map = nullptr;
3519 }
3520 }
has_values_mapLEX3521 bool has_values_map() const { return insert_update_values_map != nullptr; }
begin_values_mapLEX3522 std::map<Field *, Field *>::iterator begin_values_map() {
3523 return insert_update_values_map->begin();
3524 }
end_values_mapLEX3525 std::map<Field *, Field *>::iterator end_values_map() {
3526 return insert_update_values_map->end();
3527 }
3528
3529 private:
3530 /*
3531 With Visual Studio, an std::map will always allocate two small objects
3532 on the heap. Sometimes we put LEX objects in a MEM_ROOT, and never run
3533 the LEX DTOR. To avoid memory leaks, put this std::map on the heap,
3534 and call clear_values_map() at the end of each statement.
3535 */
3536 std::map<Field *, Field *> *insert_update_values_map;
3537
3538 public:
3539 /*
3540 A stack of name resolution contexts for the query. This stack is used
3541 at parse time to set local name resolution contexts for various parts
3542 of a query. For example, in a JOIN ... ON (some_condition) clause the
3543 Items in 'some_condition' must be resolved only against the operands
3544 of the the join, and not against the whole clause. Similarly, Items in
3545 subqueries should be resolved against the subqueries (and outer queries).
3546 The stack is used in the following way: when the parser detects that
3547 all Items in some clause need a local context, it creates a new context
3548 and pushes it on the stack. All newly created Items always store the
3549 top-most context in the stack. Once the parser leaves the clause that
3550 required a local context, the parser pops the top-most context.
3551 */
3552 List<Name_resolution_context> context_stack;
3553
3554 Item_sum *in_sum_func;
3555 udf_func udf;
3556 HA_CHECK_OPT check_opt; // check/repair options
3557 HA_CREATE_INFO *create_info;
3558 KEY_CREATE_INFO key_create_info;
3559 LEX_MASTER_INFO mi; // used by CHANGE MASTER
3560 LEX_SLAVE_CONNECTION slave_connection;
3561 Server_options server_options;
3562 USER_RESOURCES mqh;
3563 LEX_RESET_SLAVE reset_slave_info;
3564 ulong type;
3565 /**
3566 This field is used as a work field during resolving to validate
3567 the use of aggregate functions. For example in a query
3568 SELECT ... FROM ...WHERE MIN(i) == 1 GROUP BY ... HAVING MIN(i) > 2
3569 MIN(i) in the WHERE clause is not allowed since only non-aggregated data
3570 is present, whereas MIN(i) in the HAVING clause is allowed because HAVING
3571 operates on the output of a grouping operation.
3572 Each query block is assigned a nesting level. This field is a bit field
3573 that contains the value one in the position of that nesting level if
3574 aggregate functions are allowed for that query block.
3575 */
3576 nesting_map allow_sum_func;
3577 /**
3578 Windowing functions are not allowed in HAVING - in contrast to group
3579 aggregates - then we need to be stricter than allow_sum_func.
3580 One bit per query block, as allow_sum_func.
3581 */
3582 nesting_map m_deny_window_func;
3583
3584 Sql_cmd *m_sql_cmd;
3585
3586 /*
3587 Usually `expr` rule of yacc is quite reused but some commands better
3588 not support subqueries which comes standard with this rule, like
3589 KILL, HA_READ, CREATE/ALTER EVENT etc. Set this to `false` to get
3590 syntax error back.
3591 */
3592 bool expr_allows_subselect;
3593 /**
3594 If currently re-parsing a CTE's definition, this is the offset in bytes
3595 of that definition in the original statement which had the WITH
3596 clause. Otherwise this is 0.
3597 */
3598 uint reparse_common_table_expr_at;
3599
3600 enum SSL_type ssl_type; /* defined in violite.h */
3601 enum enum_duplicates duplicates;
3602 enum enum_tx_isolation tx_isolation;
3603 enum enum_var_type option_type;
3604 enum_view_create_mode create_view_mode;
3605
3606 /// QUERY ID for SHOW PROFILE
3607 my_thread_id show_profile_query_id;
3608 uint profile_options;
3609 uint grant, grant_tot_col;
3610 bool grant_privilege;
3611 uint slave_thd_opt, start_transaction_opt;
3612 int select_number; ///< Number of query block (by EXPLAIN)
3613 uint8 create_view_algorithm;
3614 uint8 create_view_check;
3615 /**
3616 @todo ensure that correct CONTEXT_ANALYSIS_ONLY is set for all preparation
3617 code, so we can fully rely on this field.
3618 */
3619 uint8 context_analysis_only;
3620 bool drop_if_exists;
3621 bool drop_temporary;
3622 bool autocommit;
3623 bool verbose, no_write_to_binlog;
3624 // For show commands to show hidden columns and indexes.
3625 bool m_extended_show;
3626
3627 enum enum_yes_no_unknown tx_chain, tx_release;
3628
3629 /**
3630 Whether this query will return the same answer every time, given unchanged
3631 data. Used to be for the query cache, but is now used to find out if an
3632 expression is usable for partitioning.
3633 */
3634 bool safe_to_cache_query;
3635
3636 private:
3637 bool ignore;
3638
3639 public:
is_ignoreLEX3640 bool is_ignore() const { return ignore; }
set_ignoreLEX3641 void set_ignore(bool ignore_param) { ignore = ignore_param; }
3642 st_parsing_options parsing_options;
3643 Alter_info *alter_info;
3644 /* Prepared statements SQL syntax:*/
3645 LEX_CSTRING prepared_stmt_name; /* Statement name (in all queries) */
3646 /*
3647 Prepared statement query text or name of variable that holds the
3648 prepared statement (in PREPARE ... queries)
3649 */
3650 LEX_STRING prepared_stmt_code;
3651 /* If true, prepared_stmt_code is a name of variable that holds the query */
3652 bool prepared_stmt_code_is_varref;
3653 /* Names of user variables holding parameters (in EXECUTE) */
3654 List<LEX_STRING> prepared_stmt_params;
3655 sp_head *sphead;
3656 sp_name *spname;
3657 bool sp_lex_in_use; /* Keep track on lex usage in SPs for error handling */
3658 bool all_privileges;
3659 bool contains_plaintext_password;
3660 enum_keep_diagnostics keep_diagnostics;
3661 uint32 next_binlog_file_nr;
3662
3663 private:
3664 bool m_broken; ///< see mark_broken()
3665 /**
3666 Set to true when execution has started (after parsing, tables opened and
3667 query preparation is complete. Used to track arena state for SPs).
3668 */
3669 bool m_exec_started;
3670 /// Current SP parsing context.
3671 /// @see also sp_head::m_root_parsing_ctx.
3672 sp_pcontext *sp_current_parsing_ctx;
3673
3674 /**
3675 Statement context for SELECT_LEX::make_active_options.
3676 */
3677 ulonglong m_statement_options{0};
3678
3679 public:
3680 /**
3681 Gets the options that have been set for this statement. The options are
3682 propagated to the SELECT_LEX objects and should usually be read with
3683 #SELECT_LEX::active_options().
3684
3685 @return a bit set of options set for this statement
3686 */
statement_optionsLEX3687 ulonglong statement_options() { return m_statement_options; }
3688 /**
3689 Add options to values of m_statement_options. options is an ORed
3690 bit set of options defined in query_options.h
3691
3692 @param options Add this set of options to the set already in
3693 m_statement_options
3694 */
add_statement_optionsLEX3695 void add_statement_options(ulonglong options) {
3696 m_statement_options |= options;
3697 }
is_brokenLEX3698 bool is_broken() const { return m_broken; }
3699 /**
3700 Certain permanent transformations (like in2exists), if they fail, may
3701 leave the LEX in an inconsistent state. They should call the
3702 following function, so that this LEX is not reused by another execution.
3703
3704 @todo If lex_start () were a member function of LEX, the "broken"
3705 argument could always be "true" and thus could be removed.
3706 */
3707 void mark_broken(bool broken = true) {
3708 if (broken) {
3709 /*
3710 "OPEN <cursor>" cannot be re-prepared if the cursor uses no tables
3711 ("SELECT FROM DUAL"). Indeed in that case cursor_query is left empty
3712 in constructions of sp_instr_cpush, and thus
3713 sp_lex_instr::parse_expr() cannot re-prepare. So we mark the statement
3714 as broken only if tables are used.
3715 */
3716 if (is_metadata_used()) m_broken = true;
3717 } else
3718 m_broken = false;
3719 }
3720
is_exec_startedLEX3721 bool is_exec_started() const { return m_exec_started; }
3722
set_exec_startedLEX3723 void set_exec_started() { m_exec_started = true; }
reset_exec_startedLEX3724 void reset_exec_started() { m_exec_started = false; }
get_sp_current_parsing_ctxLEX3725 sp_pcontext *get_sp_current_parsing_ctx() { return sp_current_parsing_ctx; }
3726
set_sp_current_parsing_ctxLEX3727 void set_sp_current_parsing_ctx(sp_pcontext *ctx) {
3728 sp_current_parsing_ctx = ctx;
3729 }
3730
3731 /// Check if the current statement uses meta-data (uses a table or a stored
3732 /// routine).
is_metadata_usedLEX3733 bool is_metadata_used() const {
3734 return query_tables != nullptr ||
3735 (sroutines != nullptr && !sroutines->empty());
3736 }
3737
3738 public:
3739 st_sp_chistics sp_chistics;
3740
3741 Event_parse_data *event_parse_data;
3742
3743 bool only_view; /* used for SHOW CREATE TABLE/VIEW */
3744 /*
3745 view created to be run from definer (standard behaviour)
3746 */
3747 uint8 create_view_suid;
3748
3749 /**
3750 Intended to point to the next word after DEFINER-clause in the
3751 following statements:
3752
3753 - CREATE TRIGGER (points to "TRIGGER");
3754 - CREATE PROCEDURE (points to "PROCEDURE");
3755 - CREATE FUNCTION (points to "FUNCTION" or "AGGREGATE");
3756 - CREATE EVENT (points to "EVENT")
3757
3758 This pointer is required to add possibly omitted DEFINER-clause to the
3759 DDL-statement before dumping it to the binlog.
3760 */
3761 const char *stmt_definition_begin;
3762 const char *stmt_definition_end;
3763
3764 /**
3765 During name resolution search only in the table list given by
3766 Name_resolution_context::first_name_resolution_table and
3767 Name_resolution_context::last_name_resolution_table
3768 (see Item_field::fix_fields()).
3769 */
3770 bool use_only_table_context;
3771
3772 bool is_lex_started; /* If lex_start() did run. For debugging. */
3773 /// Set to true while resolving values in ON DUPLICATE KEY UPDATE clause
3774 bool in_update_value_clause;
3775
3776 class Explain_format *explain_format;
3777
3778 // Maximum execution time for a statement.
3779 ulong max_execution_time;
3780
3781 /*
3782 To flag the current statement as dependent for binary logging
3783 on explicit_defaults_for_timestamp
3784 */
3785 bool binlog_need_explicit_defaults_ts;
3786
3787 /**
3788 Used to inform the parser whether it should contextualize the parse
3789 tree. When we get a pure parser this will not be needed.
3790 */
3791 bool will_contextualize;
3792
3793 LEX();
3794
3795 virtual ~LEX();
3796
3797 /// Reset query context to initial state
3798 void reset();
3799
3800 /// Create an empty query block within this LEX object.
3801 SELECT_LEX *new_empty_query_block();
3802
3803 /// Create query expression object that contains one query block.
3804 SELECT_LEX *new_query(SELECT_LEX *curr_select);
3805
3806 /// Create query block and attach it to the current query expression.
3807 SELECT_LEX *new_union_query(SELECT_LEX *curr_select, bool distinct);
3808
3809 /// Create top-level query expression and query block.
3810 bool new_top_level_query();
3811
3812 /// Create query expression and query block in existing memory objects.
3813 void new_static_query(SELECT_LEX_UNIT *sel_unit, SELECT_LEX *select);
3814
3815 /// Create query expression under current_select and a query block under the
3816 /// new query expression. The new query expression is linked in under
3817 /// current_select. The new query block is linked in under the new
3818 /// query expression.
3819 ///
3820 /// @param thd current session context
3821 /// @param current_select the root under which we create the new expression
3822 /// and block
3823 /// @param where_clause any where clause for the block
3824 /// @param having_clause any having clause for the block
3825 /// @param ctx the parsing context
3826 ///
3827 /// @returns the new query expression, or nullptr on error.
3828 SELECT_LEX_UNIT *create_query_expr_and_block(THD *thd,
3829 SELECT_LEX *current_select,
3830 Item *where_clause,
3831 Item *having_clause,
3832 enum_parsing_context ctx);
3833
is_ps_or_view_context_analysisLEX3834 inline bool is_ps_or_view_context_analysis() {
3835 return (context_analysis_only &
3836 (CONTEXT_ANALYSIS_ONLY_PREPARE | CONTEXT_ANALYSIS_ONLY_VIEW));
3837 }
3838
is_view_context_analysisLEX3839 inline bool is_view_context_analysis() {
3840 return (context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW);
3841 }
3842
3843 /**
3844 Set the current query as uncacheable.
3845
3846 @param curr_select Current select query block
3847 @param cause Why this query is uncacheable.
3848
3849 @details
3850 All query blocks representing subqueries, from the current one up to
3851 the outer-most one, but excluding the main query block, are also set
3852 as uncacheable.
3853 */
set_uncacheableLEX3854 void set_uncacheable(SELECT_LEX *curr_select, uint8 cause) {
3855 safe_to_cache_query = false;
3856
3857 if (m_current_select == nullptr) return;
3858 SELECT_LEX *sl;
3859 SELECT_LEX_UNIT *un;
3860 for (sl = curr_select, un = sl->master_unit(); un != unit;
3861 sl = sl->outer_select(), un = sl->master_unit()) {
3862 sl->uncacheable |= cause;
3863 un->uncacheable |= cause;
3864 }
3865 }
3866 void set_trg_event_type_for_tables();
3867
3868 TABLE_LIST *unlink_first_table(bool *link_to_local);
3869 void link_first_table_back(TABLE_LIST *first, bool link_to_local);
3870 void first_lists_tables_same();
3871
3872 bool can_use_merged();
3873 bool can_not_use_merged();
3874 bool need_correct_ident();
3875 /*
3876 Is this update command where 'WHITH CHECK OPTION' clause is important
3877
3878 SYNOPSIS
3879 LEX::which_check_option_applicable()
3880
3881 RETURN
3882 true have to take 'WHITH CHECK OPTION' clause into account
3883 false 'WHITH CHECK OPTION' clause do not need
3884 */
which_check_option_applicableLEX3885 inline bool which_check_option_applicable() {
3886 switch (sql_command) {
3887 case SQLCOM_UPDATE:
3888 case SQLCOM_UPDATE_MULTI:
3889 case SQLCOM_INSERT:
3890 case SQLCOM_INSERT_SELECT:
3891 case SQLCOM_REPLACE:
3892 case SQLCOM_REPLACE_SELECT:
3893 case SQLCOM_LOAD:
3894 return true;
3895 default:
3896 return false;
3897 }
3898 }
3899
3900 void cleanup_after_one_table_open();
3901
push_contextLEX3902 bool push_context(Name_resolution_context *context) {
3903 return context_stack.push_front(context);
3904 }
3905
pop_contextLEX3906 void pop_context() { context_stack.pop(); }
3907
3908 bool copy_db_to(char const **p_db, size_t *p_db_length) const;
3909
copy_db_toLEX3910 bool copy_db_to(char **p_db, size_t *p_db_length) const {
3911 return copy_db_to(const_cast<const char **>(p_db), p_db_length);
3912 }
3913
current_contextLEX3914 Name_resolution_context *current_context() { return context_stack.head(); }
3915
3916 void reset_n_backup_query_tables_list(Query_tables_list *backup);
3917 void restore_backup_query_tables_list(Query_tables_list *backup);
3918
3919 bool table_or_sp_used();
3920
3921 /**
3922 @brief check if the statement is a single-level join
3923 @return result of the check
3924 @retval true The statement doesn't contain subqueries, unions and
3925 stored procedure calls.
3926 @retval false There are subqueries, UNIONs or stored procedure calls.
3927 */
is_single_level_stmtLEX3928 bool is_single_level_stmt() {
3929 /*
3930 This check exploits the fact that the last added to all_select_list is
3931 on its top. So select_lex (as the first added) will be at the tail
3932 of the list.
3933 */
3934 if (select_lex == all_selects_list &&
3935 (sroutines == nullptr || sroutines->empty())) {
3936 DBUG_ASSERT(!all_selects_list->next_select_in_list());
3937 return true;
3938 }
3939 return false;
3940 }
3941
3942 /**
3943 IS schema queries read some dynamic table statistics from SE.
3944 These statistics are cached, to avoid opening of table more
3945 than once while preparing a single output record buffer.
3946 */
3947 dd::info_schema::Table_statistics m_IS_table_stats;
3948 dd::info_schema::Tablespace_statistics m_IS_tablespace_stats;
3949
3950 bool accept(Select_lex_visitor *visitor);
3951
3952 bool set_wild(LEX_STRING);
3953 void clear_privileges();
3954
3955 bool make_sql_cmd(Parse_tree_root *parse_tree);
3956
3957 private:
3958 /**
3959 Context object used by secondary storage engines to store query
3960 state during optimization and execution.
3961 */
3962 Secondary_engine_execution_context *m_secondary_engine_context{nullptr};
3963
3964 public:
3965 /**
3966 Gets the secondary engine execution context for this statement.
3967 */
secondary_engine_execution_contextLEX3968 Secondary_engine_execution_context *secondary_engine_execution_context()
3969 const {
3970 return m_secondary_engine_context;
3971 }
3972
3973 /**
3974 Sets the secondary engine execution context for this statement.
3975 The old context object is destroyed, if there is one. Can be set
3976 to nullptr to destroy the old context object and clear the
3977 pointer.
3978
3979 The supplied context object should be allocated on the execution
3980 MEM_ROOT, so that its memory doesn't have to be manually freed
3981 after query execution.
3982 */
3983 void set_secondary_engine_execution_context(
3984 Secondary_engine_execution_context *context);
3985 };
3986
3987 /**
3988 The internal state of the syntax parser.
3989 This object is only available during parsing,
3990 and is private to the syntax parser implementation (sql_yacc.yy).
3991 */
3992 class Yacc_state {
3993 public:
Yacc_state()3994 Yacc_state() : yacc_yyss(nullptr), yacc_yyvs(nullptr), yacc_yyls(nullptr) {
3995 reset();
3996 }
3997
reset()3998 void reset() {
3999 if (yacc_yyss != nullptr) {
4000 my_free(yacc_yyss);
4001 yacc_yyss = nullptr;
4002 }
4003 if (yacc_yyvs != nullptr) {
4004 my_free(yacc_yyvs);
4005 yacc_yyvs = nullptr;
4006 }
4007 if (yacc_yyls != nullptr) {
4008 my_free(yacc_yyls);
4009 yacc_yyls = nullptr;
4010 }
4011 m_lock_type = TL_READ_DEFAULT;
4012 m_mdl_type = MDL_SHARED_READ;
4013 }
4014
4015 ~Yacc_state();
4016
4017 /**
4018 Reset part of the state which needs resetting before parsing
4019 substatement.
4020 */
reset_before_substatement()4021 void reset_before_substatement() {
4022 m_lock_type = TL_READ_DEFAULT;
4023 m_mdl_type = MDL_SHARED_READ;
4024 }
4025
4026 /**
4027 Bison internal state stack, yyss, when dynamically allocated using
4028 my_yyoverflow().
4029 */
4030 uchar *yacc_yyss;
4031
4032 /**
4033 Bison internal semantic value stack, yyvs, when dynamically allocated using
4034 my_yyoverflow().
4035 */
4036 uchar *yacc_yyvs;
4037
4038 /**
4039 Bison internal location value stack, yyls, when dynamically allocated using
4040 my_yyoverflow().
4041 */
4042 uchar *yacc_yyls;
4043
4044 /**
4045 Type of lock to be used for tables being added to the statement's
4046 table list in table_factor, table_alias_ref, single_multi and
4047 table_wild_one rules.
4048 Statements which use these rules but require lock type different
4049 from one specified by this member have to override it by using
4050 SELECT_LEX::set_lock_for_tables() method.
4051
4052 The default value of this member is TL_READ_DEFAULT. The only two
4053 cases in which we change it are:
4054 - When parsing SELECT HIGH_PRIORITY.
4055 - Rule for DELETE. In which we use this member to pass information
4056 about type of lock from delete to single_multi part of rule.
4057
4058 We should try to avoid introducing new use cases as we would like
4059 to get rid of this member eventually.
4060 */
4061 thr_lock_type m_lock_type;
4062
4063 /**
4064 The type of requested metadata lock for tables added to
4065 the statement table list.
4066 */
4067 enum_mdl_type m_mdl_type;
4068
4069 /*
4070 TODO: move more attributes from the LEX structure here.
4071 */
4072 };
4073
4074 /**
4075 Input parameters to the parser.
4076 */
4077 struct Parser_input {
4078 bool m_compute_digest;
4079
Parser_inputParser_input4080 Parser_input() : m_compute_digest(false) {}
4081 };
4082
4083 /**
4084 Internal state of the parser.
4085 The complete state consist of:
4086 - input parameters that control the parser behavior
4087 - state data used during lexical parsing,
4088 - state data used during syntactic parsing.
4089 */
4090 class Parser_state {
4091 protected:
4092 /**
4093 Constructor for special parsers of partial SQL clauses (DD)
4094
4095 @param grammar_selector_token See Lex_input_stream::grammar_selector_token
4096 */
Parser_state(int grammar_selector_token)4097 explicit Parser_state(int grammar_selector_token)
4098 : m_input(), m_lip(grammar_selector_token), m_yacc(), m_comment(false) {}
4099
4100 public:
Parser_state()4101 Parser_state() : m_input(), m_lip(-1), m_yacc(), m_comment(false) {}
4102
4103 /**
4104 Object initializer. Must be called before usage.
4105
4106 @retval false OK
4107 @retval true Error
4108 */
init(THD * thd,const char * buff,size_t length)4109 bool init(THD *thd, const char *buff, size_t length) {
4110 return m_lip.init(thd, buff, length);
4111 }
4112
reset(const char * found_semicolon,size_t length)4113 void reset(const char *found_semicolon, size_t length) {
4114 m_lip.reset(found_semicolon, length);
4115 m_yacc.reset();
4116 }
4117
4118 /// Signal that the current query has a comment
add_comment()4119 void add_comment() { m_comment = true; }
4120 /// Check whether the current query has a comment
has_comment()4121 bool has_comment() const { return m_comment; }
4122
4123 public:
4124 Parser_input m_input;
4125 Lex_input_stream m_lip;
4126 Yacc_state m_yacc;
4127 /**
4128 Current performance digest instrumentation.
4129 */
4130 PSI_digest_locker *m_digest_psi;
4131
4132 private:
4133 bool m_comment; ///< True if current query contains comments
4134 };
4135
4136 /**
4137 Parser state for partition expression parser (.frm/DD stuff)
4138 */
4139 class Partition_expr_parser_state : public Parser_state {
4140 public:
4141 Partition_expr_parser_state();
4142
4143 partition_info *result;
4144 };
4145
4146 /**
4147 Parser state for generated column expression parser (.frm/DD stuff)
4148 */
4149 class Gcol_expr_parser_state : public Parser_state {
4150 public:
4151 Gcol_expr_parser_state();
4152
4153 Value_generator *result;
4154 };
4155
4156 /**
4157 Parser state for single expression parser (.frm/DD stuff)
4158 */
4159 class Expression_parser_state : public Parser_state {
4160 public:
4161 Expression_parser_state();
4162
4163 Item *result;
4164 };
4165
4166 /**
4167 Parser state for CTE subquery parser
4168 */
4169 class Common_table_expr_parser_state : public Parser_state {
4170 public:
4171 Common_table_expr_parser_state();
4172
4173 PT_subquery *result;
4174 };
4175
4176 struct st_lex_local : public LEX {
newst_lex_local4177 static void *operator new(size_t size) noexcept {
4178 return (*THR_MALLOC)->Alloc(size);
4179 }
4180 static void *operator new(size_t size, MEM_ROOT *mem_root,
4181 const std::nothrow_t &arg MY_ATTRIBUTE((unused)) =
4182 std::nothrow) noexcept {
4183 return mem_root->Alloc(size);
4184 }
deletest_lex_local4185 static void operator delete(void *ptr MY_ATTRIBUTE((unused)),
4186 size_t size MY_ATTRIBUTE((unused))) {
4187 TRASH(ptr, size);
4188 }
deletest_lex_local4189 static void operator delete(
4190 void *, MEM_ROOT *, const std::nothrow_t &) noexcept { /* Never called */
4191 }
4192 };
4193
4194 extern bool lex_init(void);
4195 extern void lex_free(void);
4196 extern bool lex_start(THD *thd);
4197 extern void lex_end(LEX *lex);
4198 extern int MYSQLlex(union YYSTYPE *, struct YYLTYPE *, class THD *);
4199
4200 extern void trim_whitespace(const CHARSET_INFO *cs, LEX_STRING *str);
4201
4202 extern bool is_lex_native_function(const LEX_STRING *name);
4203
4204 bool is_keyword(const char *name, size_t len);
4205 bool db_is_default_db(const char *db, size_t db_len, const THD *thd);
4206
4207 bool check_select_for_locking_clause(THD *);
4208
4209 void print_derived_column_names(const THD *thd, String *str,
4210 const Create_col_name_list *column_names);
4211
4212 /**
4213 @} (End of group GROUP_PARSER)
4214 */
4215
4216 /**
4217 Check if the given string is invalid using the system charset.
4218
4219 @param string_val Reference to the string.
4220 @param charset_info Pointer to charset info.
4221
4222 @return true if the string has an invalid encoding using
4223 the system charset else false.
4224 */
4225
is_invalid_string(const LEX_CSTRING & string_val,const CHARSET_INFO * charset_info)4226 inline bool is_invalid_string(const LEX_CSTRING &string_val,
4227 const CHARSET_INFO *charset_info) {
4228 size_t valid_len;
4229 bool len_error;
4230
4231 if (validate_string(charset_info, string_val.str, string_val.length,
4232 &valid_len, &len_error)) {
4233 char hexbuf[7];
4234 octet2hex(
4235 hexbuf, string_val.str + valid_len,
4236 static_cast<uint>(std::min<size_t>(string_val.length - valid_len, 3)));
4237 my_error(ER_INVALID_CHARACTER_STRING, MYF(0), charset_info->csname, hexbuf);
4238 return true;
4239 }
4240 return false;
4241 }
4242
4243 bool walk_item(Item *item, Select_lex_visitor *visitor);
4244 bool accept_for_order(SQL_I_List<ORDER> orders, Select_lex_visitor *visitor);
4245 bool accept_table(TABLE_LIST *t, Select_lex_visitor *visitor);
4246 bool accept_for_join(mem_root_deque<TABLE_LIST *> *tables,
4247 Select_lex_visitor *visitor);
4248 #endif /* SQL_LEX_INCLUDED */
4249