MySQL 9.0.0
Source Code Documentation
sql_lex.h
Go to the documentation of this file.
1/* Copyright (c) 2000, 2024, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/**
25 @defgroup GROUP_PARSER Parser
26 @{
27*/
28
29#ifndef SQL_LEX_INCLUDED
30#define SQL_LEX_INCLUDED
31
32#include <string.h>
33#include <sys/types.h> // TODO: replace with cstdint
34
35#include <algorithm>
36#include <cstdint>
37#include <cstring>
38#include <functional>
39#include <map>
40#include <memory>
41#include <new>
42#include <string>
43#include <utility>
44
45#include "lex_string.h"
46#include "map_helpers.h"
47#include "mem_root_deque.h"
48#include "memory_debugging.h"
49#include "my_alloc.h" // Destroy_only
50#include "my_base.h"
51#include "my_compiler.h"
52#include "my_dbug.h"
53#include "my_inttypes.h" // TODO: replace with cstdint
54#include "my_sqlcommand.h"
55#include "my_sys.h"
56#include "my_table_map.h"
57#include "my_thread_local.h"
59#include "mysql/service_mysql_alloc.h" // my_free
61#include "mysql_com.h"
62#include "mysqld_error.h"
63#include "prealloced_array.h" // Prealloced_array
64#include "sql/dd/info_schema/table_stats.h" // dd::info_schema::Table_stati...
65#include "sql/dd/info_schema/tablespace_stats.h" // dd::info_schema::Tablesp...
66#include "sql/enum_query_type.h"
67#include "sql/handler.h"
68#include "sql/item.h" // Name_resolution_context
69#include "sql/item_subselect.h" // Subquery_strategy
72#include "sql/key_spec.h" // KEY_CREATE_INFO
73#include "sql/mdl.h"
74#include "sql/mem_root_array.h" // Mem_root_array
75#include "sql/parse_location.h"
76#include "sql/parse_tree_node_base.h" // enum_parsing_context
77#include "sql/parser_yystype.h"
78#include "sql/query_options.h" // OPTION_NO_CONST_TABLES
79#include "sql/query_term.h"
80#include "sql/set_var.h"
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_error.h" // warn_on_deprecated_charset
86#include "sql/sql_list.h"
87#include "sql/sql_plugin_ref.h"
88#include "sql/sql_servers.h" // Server_options
89#include "sql/sql_udf.h" // Item_udftype
90#include "sql/table.h" // Table_ref
91#include "sql/thr_malloc.h"
92#include "sql/trigger_def.h" // enum_trigger_action_time_type
93#include "sql/visible_fields.h"
94#include "sql_string.h"
95#include "string_with_len.h"
96#include "strings/sql_chars.h"
97#include "thr_lock.h" // thr_lock_type
98#include "violite.h" // SSL_type
99
100class Alter_info;
101class Event_parse_data;
102class Field;
103class Item_cond;
105class Item_func_match;
109class Item_sum;
110class JOIN;
111class Opt_hints_global;
112class Opt_hints_qb;
113class PT_subquery;
114class PT_with_clause;
115class Parse_tree_root;
116class Protocol;
117class Query_result;
120class Query_block;
121class Query_expression;
123class Sql_cmd;
124class THD;
125class Value_generator;
126class Window;
127class partition_info;
128class sp_head;
129class sp_name;
130class sp_pcontext;
131struct LEX;
132struct NESTED_JOIN;
133struct PSI_digest_locker;
134struct sql_digest_state;
135union Lexer_yystype;
137
139constexpr const int MAX_SELECT_NESTING{sizeof(nesting_map) * 8 - 1};
140
141/*
142 There are 8 different type of table access so there is no more than
143 combinations 2^8 = 256:
144
145 . STMT_READS_TRANS_TABLE
146
147 . STMT_READS_NON_TRANS_TABLE
148
149 . STMT_READS_TEMP_TRANS_TABLE
150
151 . STMT_READS_TEMP_NON_TRANS_TABLE
152
153 . STMT_WRITES_TRANS_TABLE
154
155 . STMT_WRITES_NON_TRANS_TABLE
156
157 . STMT_WRITES_TEMP_TRANS_TABLE
158
159 . STMT_WRITES_TEMP_NON_TRANS_TABLE
160
161 The unsafe conditions for each combination is represented within a byte
162 and stores the status of the option --binlog-direct-non-trans-updates,
163 whether the trx-cache is empty or not, and whether the isolation level
164 is lower than ISO_REPEATABLE_READ:
165
166 . option (OFF/ON)
167 . trx-cache (empty/not empty)
168 . isolation (>= ISO_REPEATABLE_READ / < ISO_REPEATABLE_READ)
169
170 bits 0 : . OFF, . empty, . >= ISO_REPEATABLE_READ
171 bits 1 : . OFF, . empty, . < ISO_REPEATABLE_READ
172 bits 2 : . OFF, . not empty, . >= ISO_REPEATABLE_READ
173 bits 3 : . OFF, . not empty, . < ISO_REPEATABLE_READ
174 bits 4 : . ON, . empty, . >= ISO_REPEATABLE_READ
175 bits 5 : . ON, . empty, . < ISO_REPEATABLE_READ
176 bits 6 : . ON, . not empty, . >= ISO_REPEATABLE_READ
177 bits 7 : . ON, . not empty, . < ISO_REPEATABLE_READ
178*/
179extern uint binlog_unsafe_map[256];
180/*
181 Initializes the array with unsafe combinations and its respective
182 conditions.
183*/
185
186/*
187 If we encounter a diagnostics statement (GET DIAGNOSTICS, or e.g.
188 the old SHOW WARNINGS|ERRORS, or "diagnostics variables" such as
189 @@warning_count | @@error_count, we'll set some hints so this
190 information is not lost. DA_KEEP_UNSPECIFIED is used in LEX constructor to
191 avoid leaving variables uninitialized.
192 */
194 DA_KEEP_NOTHING = 0, /**< keep nothing */
195 DA_KEEP_DIAGNOSTICS, /**< keep the diagnostics area */
196 DA_KEEP_COUNTS, /**< keep \@warning_count / \@error_count */
197 DA_KEEP_PARSE_ERROR, /**< keep diagnostics area after parse error */
198 DA_KEEP_UNSPECIFIED /**< keep semantics is unspecified */
200
206
214
215/**
216 enum_sp_type defines type codes of stored programs.
217
218 @note these codes are used when dealing with the mysql.routines system table,
219 so they must not be changed.
220
221 @note the following macros were used previously for the same purpose. Now they
222 are used for ACL only.
223*/
224enum class enum_sp_type {
225 FUNCTION = 1,
226 PROCEDURE,
227 TRIGGER,
228 EVENT,
229 /*
230 Must always be the last one.
231 Denotes an error condition.
232 */
234};
235
237 if (val >= static_cast<longlong>(enum_sp_type::FUNCTION) &&
238 val < static_cast<longlong>(enum_sp_type::INVALID_SP_TYPE))
239 return static_cast<enum_sp_type>(val);
240 else
242}
243
245 return static_cast<longlong>(val);
246}
247
248inline uint to_uint(enum_sp_type val) { return static_cast<uint>(val); }
249
250/*
251 Values for the type enum. This reflects the order of the enum declaration
252 in the CREATE TABLE command. These values are used to enumerate object types
253 for the ACL statements.
254
255 These values were also used for enumerating stored program types. However, now
256 enum_sp_type should be used for that instead of them.
257*/
258#define TYPE_ENUM_FUNCTION 1
259#define TYPE_ENUM_PROCEDURE 2
260#define TYPE_ENUM_TRIGGER 3
261#define TYPE_ENUM_PROXY 4
262
263enum class Acl_type {
264 TABLE = 0,
267};
268
270 {STRING_WITH_LEN("")},
271 {STRING_WITH_LEN("CONTAINS SQL")},
272 {STRING_WITH_LEN("NO SQL")},
273 {STRING_WITH_LEN("READS SQL DATA")},
274 {STRING_WITH_LEN("MODIFIES SQL DATA")}};
275
277 VIEW_CREATE_NEW, // check that there are not such VIEW/table
278 VIEW_ALTER, // check that VIEW with such name exists
279 VIEW_CREATE_OR_REPLACE // check only that there are not such table
280};
281
283 ALTER_USER_COMMENT_NOT_USED, // No user metadata ALTER in the AST
284 ALTER_USER_COMMENT, // A text comment is expected
285 ALTER_USER_ATTRIBUTE // A JSON object is expected
286};
287
288/* Options to add_table_to_list() */
289#define TL_OPTION_UPDATING 0x01
290#define TL_OPTION_IGNORE_LEAVES 0x02
291#define TL_OPTION_ALIAS 0x04
292
293/* Structure for db & table in sql_yacc */
294class Table_function;
295
297 public:
302
303 Table_ident(Protocol *protocol, const LEX_CSTRING &db_arg,
304 const LEX_CSTRING &table_arg, bool force);
305 Table_ident(const LEX_CSTRING &db_arg, const LEX_CSTRING &table_arg)
306 : db(db_arg), table(table_arg), sel(nullptr), table_function(nullptr) {}
307 Table_ident(const LEX_CSTRING &table_arg)
308 : table(table_arg), sel(nullptr), table_function(nullptr) {
309 db = NULL_CSTR;
310 }
311 /**
312 This constructor is used only for the case when we create a derived
313 table. A derived table has no name and doesn't belong to any database.
314 Later, if there was an alias specified for the table, it will be set
315 by add_table_to_list.
316 */
318 db = EMPTY_CSTR; /* a subject to casedn_str */
320 }
321 /*
322 This constructor is used only for the case when we create a table function.
323 It has no name and doesn't belong to any database as it exists only
324 during query execution. Later, if there was an alias specified for the
325 table, it will be set by add_table_to_list.
326 */
327 Table_ident(LEX_CSTRING &table_arg, Table_function *table_func_arg)
328 : table(table_arg), sel(nullptr), table_function(table_func_arg) {
329 /* We must have a table name here as this is used with add_table_to_list */
330 db = EMPTY_CSTR; /* a subject to casedn_str */
331 }
332 // True if we can tell from syntax that this is a table function.
333 bool is_table_function() const { return (table_function != nullptr); }
334 // True if we can tell from syntax that this is an unnamed derived table.
335 bool is_derived_table() const { return sel; }
336 void change_db(const char *db_name) {
337 db.str = db_name;
338 db.length = strlen(db_name);
339 }
340};
341
344
345/**
346 Structure to hold parameters for CHANGE REPLICATION SOURCE, START REPLICA, and
347 STOP REPLICA.
348
349 Remark: this should not be confused with Master_info (and perhaps
350 would better be renamed to st_lex_replication_info). Some fields,
351 e.g., delay, are saved in Relay_log_info, not in Master_info.
352*/
354 /*
355 The array of IGNORE_SERVER_IDS has a preallocation, and is not expected
356 to grow to any significant size, so no instrumentation.
357 */
359 initialize();
360 }
367 char *gtid;
368 char *view_id;
369 const char *channel; // identifier similar to database name
370 enum {
377
378 /*
379 Enum is used for making it possible to detect if the user
380 changed variable or if it should be left at old value
381 */
382 enum {
392 /*
393 Ciphersuites used for TLS 1.3 communication with the master server.
394 */
399 };
408 /**
409 Flag that is set to `true` whenever `PRIVILEGE_CHECKS_USER` is set to `NULL`
410 as a part of a `CHANGE REPLICATION SOURCE TO` statement.
411 */
413 /**
414 Username and hostname parts of the `PRIVILEGE_CHECKS_USER`, when it's set to
415 a user.
416 */
418 /**
419 Flag indicating if row format should be enforced for this channel event
420 stream.
421 */
423
424 /**
425 Identifies what is the slave policy on primary keys in tables.
426 If set to STREAM it just replicates the value of sql_require_primary_key.
427 If set to ON it fails when the source tries to replicate a table creation
428 or alter operation that does not have a primary key.
429 If set to OFF it does not enforce any policies on the channel for primary
430 keys.
431 */
432 enum {
439
440 enum {
446
448
449 /// Initializes everything to zero/NULL/empty.
450 void initialize();
451 /// Sets all fields to their "unspecified" value.
452 void set_unspecified();
453
454 private:
455 // Not copyable or assignable.
458};
459
461 bool all;
462};
463
469
470/*
471 String names used to print a statement with index hints.
472 Keep in sync with index_hint_type.
473*/
474extern const char *index_hint_type_name[];
476
477/*
478 Bits in index_clause_map : one for each possible FOR clause in
479 USE/FORCE/IGNORE INDEX index hint specification
480*/
481#define INDEX_HINT_MASK_JOIN (1)
482#define INDEX_HINT_MASK_GROUP (1 << 1)
483#define INDEX_HINT_MASK_ORDER (1 << 2)
484
485#define INDEX_HINT_MASK_ALL \
486 (INDEX_HINT_MASK_JOIN | INDEX_HINT_MASK_GROUP | INDEX_HINT_MASK_ORDER)
487
488/* Single element of an USE/FORCE/IGNORE INDEX list specified as a SQL hint */
490 public:
491 /* The type of the hint : USE/FORCE/IGNORE */
493 /* Where the hit applies to. A bitmask of INDEX_HINT_MASK_<place> values */
495 /*
496 The index name. Empty (str=NULL) name represents an empty list
497 USE INDEX () clause
498 */
500
501 Index_hint(const char *str, uint length) {
502 key_name.str = str;
504 }
505
506 void print(const THD *thd, String *str);
507};
508
509/*
510 Class Query_expression represents a query expression.
511 Class Query_block represents a query block.
512
513 In addition to what is explained below, the query block(s) of a query
514 expression is contained in a tree expressing the nesting of set operations,
515 cf. query_term.h
516
517 A query expression contains one or more query blocks (more than one means
518 that the query expression contains one or more set operations - UNION,
519 INTERSECT or EXCEPT - unless the query blocks are used to describe
520 subqueries). These classes are connected as follows: both classes have a
521 master, a slave, a next and a prev field. For class Query_block, master and
522 slave connect to objects of type Query_expression, whereas for class
523 Query_expression, they connect to Query_block. master is pointer to outer
524 node. slave is pointer to the first inner node.
525
526 neighbors are two Query_block or Query_expression objects on
527 the same level.
528
529 The structures are linked with the following pointers:
530 - list of neighbors (next/prev) (prev of first element point to slave
531 pointer of outer structure)
532 - For Query_block, this is a list of query blocks.
533 - For Query_expression, this is a list of subqueries.
534
535 - pointer to outer node (master), which is
536 If this is Query_expression
537 - pointer to outer query_block.
538 If this is Query_block
539 - pointer to outer Query_expression.
540
541 - pointer to inner objects (slave), which is either:
542 If this is an Query_expression:
543 - first query block that belong to this query expression.
544 If this is an Query_block
545 - first query expression that belong to this query block (subqueries).
546
547 - list of all Query_block objects (link_next/link_prev)
548 This is to be used for things like derived tables creation, where we
549 go through this list and create the derived tables.
550
551 In addition to the above mentioned link, the query's tree structure is
552 represented by the member m_query_term, see query_term.h
553 For example for following query:
554
555 select *
556 from table1
557 where table1.field IN (select * from table1_1_1 union
558 select * from table1_1_2)
559 union
560 select *
561 from table2
562 where table2.field=(select (select f1 from table2_1_1_1_1
563 where table2_1_1_1_1.f2=table2_1_1.f3)
564 from table2_1_1
565 where table2_1_1.f1=table2.f2)
566 union
567 select * from table3;
568
569 we will have following structure:
570
571 select1: (select * from table1 ...)
572 select2: (select * from table2 ...)
573 select3: (select * from table3)
574 select1.1.1: (select * from table1_1_1)
575 ...
576
577 main unit
578 select1 select2 select3
579 |^^ |^
580 s||| ||master
581 l||| |+---------------------------------+
582 a||| +---------------------------------+|
583 v|||master slave ||
584 e||+-------------------------+ ||
585 V| neighbor | V|
586 unit1.1<+==================>unit1.2 unit2.1
587 select1.1.1 select 1.1.2 select1.2.1 select2.1.1
588 |^
589 ||
590 V|
591 unit2.1.1.1
592 select2.1.1.1.1
593
594
595 relation in main unit will be following:
596 (bigger picture for:
597 main unit
598 select1 select2 select3
599 in the above picture)
600
601 main unit
602 |^^^
603 ||||
604 ||||
605 |||+------------------------------+
606 ||+--------------+ |
607 slave||master | |
608 V| neighbor | neighbor |
609 select1<========>select2<========>select3
610
611 list of all query_block will be following (as it will be constructed by
612 parser):
613
614 select1->select2->select3->select2.1.1->select 2.1.2->select2.1.1.1.1-+
615 |
616 +---------------------------------------------------------------------+
617 |
618 +->select1.1.1->select1.1.2
619
620*/
621
622/**
623 This class represents a query expression (one query block or
624 several query blocks combined with UNION).
625*/
627 /**
628 Intrusive double-linked list of all query expressions
629 immediately contained within the same query block.
630 */
633
634 /**
635 The query block wherein this query expression is contained,
636 NULL if the query block is the outer-most one.
637 */
639 /// The first query block in this query expression.
641
642 // The query set operation structure, see doc for Query_term.
644
645 public:
646 /// Getter for m_query_term, q.v.
647 Query_term *query_term() const { return m_query_term; }
648 /// Setter for m_query_term, q.v.
650 /// Convenience method to avoid down casting, i.e. interpret m_query_term
651 /// as a Query_term_set_op.
652 /// @retval a non-null node iff !is_simple
653 /// @retval nullptr if is_simple() holds.
655 return is_simple() ? nullptr : down_cast<Query_term_set_op *>(m_query_term);
656 }
657 /// Return the query block iff !is_simple() holds
659 if (is_simple())
660 return nullptr;
661 else
662 return m_query_term->query_block();
663 }
664 bool is_leaf_block(Query_block *qb);
666 for (auto qt : query_terms<>()) {
667 if (qt->query_block() == qb) return qt;
668 }
669 return nullptr;
670 }
671
672 /**
673 Return iterator object over query terms rooted in m_query_term,
674 using either post order visiting (default) or pre order,
675 optionally skipping leaf nodes (query blocks corresponding to SELECTs or
676 table constructors). By default, we visit all nodes.
677 Usage: for (auto qt : query_terms<..>() { ... }
678 E.g.
679 for (auto qt : query_terms<>()) { } Visit all nodes, post order
680 for (auto qt : query_terms<QTC_PRE_ORDER, false>()) { }
681 Skip leaves, pre order
682 @tparam order == QTC_POST_ORDER if post order traversal is desired;default
683 == QTC_PRE_ORDER pre-order traversal
684 @tparam visit_leaves == VL_VISIT_LEAVES: if we want the traversal to include
685 leaf nodes i.e. the SELECTs or table constructors
686 == VL_SKIP_LEAVES: leaves will be skipped
687 @returns iterator object
688 */
689 template <Visit_order order = QTC_POST_ORDER,
690 Visit_leaves visit_leaves = VL_VISIT_LEAVES>
693 }
694
695 /**
696 Return the Query_block of the last query term in a n-ary set
697 operation that is the right side of the last DISTINCT set operation in that
698 n_ary set operation:
699 E.e. for
700 A UNION B UNION ALL C,
701 B's block will be returned. If no DISTINCT is present or not a set
702 operation, return nullptr.
703
704 @returns query block of last distinct right operand
705 */
707 auto const setop = down_cast<Query_term_set_op *>(m_query_term);
708 if (setop->m_last_distinct > 0)
709 return setop->m_children[setop->m_last_distinct]->query_block();
710 else
711 return nullptr;
712 }
713
715 if (is_simple()) return false;
716 return down_cast<Query_term_set_op *>(m_query_term)->m_last_distinct > 0;
717 }
718
719 private:
720 /**
721 Marker for subqueries in WHERE, HAVING, ORDER BY, GROUP BY and
722 SELECT item lists.
723 Must be read/written when holding LOCK_query_plan.
724
725 See Item_subselect::explain_subquery_checker
726 */
728
729 bool prepared; ///< All query blocks in query expression are prepared
730 bool optimized; ///< All query blocks in query expression are optimized
731 bool executed; ///< Query expression has been executed
732
733 /// Object to which the result for this query expression is sent.
734 /// Not used if we materialize directly into a parent query expression's
735 /// result table (see optimize()).
737
738 /**
739 An iterator you can read from to get all records for this query.
740
741 May be nullptr even after create_access_paths(), or in the case of an
742 unfinished materialization (see optimize()).
743 */
746
747 /**
748 If there is an unfinished materialization (see optimize()),
749 contains one element for each operand (query block) in this query
750 expression.
751 */
753
754 private:
755 /**
756 Convert the executor structures to a set of access paths, storing the result
757 in m_root_access_path.
758 */
759 void create_access_paths(THD *thd);
760
761 public:
762 /**
763 result of this query can't be cached, bit field, can be :
764 UNCACHEABLE_DEPENDENT
765 UNCACHEABLE_RAND
766 UNCACHEABLE_SIDEEFFECT
767 */
769
770 explicit Query_expression(enum_parsing_context parsing_context);
771
772 /// @return true for a query expression without UNION/INTERSECT/EXCEPT or
773 /// multi-level ORDER, i.e. we have a "simple table".
774 bool is_simple() const { return m_query_term->term_type() == QT_QUERY_BLOCK; }
775
776 /// Values for Query_expression::cleaned
778 UC_DIRTY, ///< Unit isn't cleaned
779 UC_PART_CLEAN, ///< Unit were cleaned, except JOIN and JOIN_TABs were
780 ///< kept for possible EXPLAIN
781 UC_CLEAN ///< Unit completely cleaned, all underlying JOINs were
782 ///< freed
783 };
784 enum_clean_state cleaned; ///< cleanliness state
785
786 private:
787 /*
788 list of types of items inside union (used for union & derived tables)
789
790 Item_type_holders from which this list consist may have pointers to Field,
791 pointers is valid only after preparing SELECTS of this unit and before
792 any SELECT of this unit execution
793
794 All hidden items are stripped away from this list.
795 */
797
798 public:
799 /**
800 Return the query block holding the top level ORDER BY, LIMIT and OFFSET.
801
802 If the query is not a set operation (UNION, INTERSECT or EXCEPT, and the
803 query expression has no multi-level ORDER BY/LIMIT, this represents the
804 single query block of the query itself, cf. documentation for class
805 Query_term.
806
807 @return query block containing the global parameters
808 */
810 return query_term()->query_block();
811 }
812
813 /* LIMIT clause runtime counters */
815
816 /* For IN/EXISTS predicates, we may not push down LIMIT 1 safely if true*/
818
819 /// Points to subquery if this query expression is used in one, otherwise NULL
821 /**
822 The WITH clause which is the first part of this query expression. NULL if
823 none.
824 */
826 /**
827 If this query expression is underlying of a derived table, the derived
828 table. NULL if none.
829 */
831 /**
832 First query block (in this UNION) which references the CTE.
833 NULL if not the query expression of a recursive CTE.
834 */
836
837 /**
838 If 'this' is body of lateral derived table:
839 map of tables in the same FROM clause as this derived table, and to which
840 the derived table's body makes references.
841 In pre-resolution stages, this is OUTER_REF_TABLE_BIT, just to indicate
842 that this has LATERAL; after resolution, which has found references in the
843 body, this is the proper map (with no PSEUDO_TABLE_BITS anymore).
844 */
846
847 /**
848 This query expression represents a scalar subquery and we need a run-time
849 check that the cardinality doesn't exceed 1.
850 */
852
853 /// @return true if query expression can be merged into an outer query
854 bool is_mergeable() const;
855
856 /// @return true if query expression is recommended to be merged
857 bool merge_heuristic(const LEX *lex) const;
858
859 /// @return the query block this query expression belongs to as subquery
861
862 /// @return the first query block inside this query expression
864
865 /// @return the next query expression within same query block (next subquery)
867
868 /// @return the query result object in use for this query expression
870
871 RowIterator *root_iterator() const { return m_root_iterator.get(); }
873 return std::move(m_root_iterator);
874 }
876
877 // Asks each query block to switch to an access path with in2exists
878 // conditions removed (if they were ever added).
879 // See JOIN::change_to_access_path_without_in2exists().
881
883 m_root_access_path = nullptr;
884 m_root_iterator.reset();
885 }
886
887 /**
888 Ensures that there are iterators created for the access paths created
889 by optimize(), even if it is not a top-level Query_expression.
890 If there are already iterators, it is a no-op. optimize() must have
891 been called earlier.
892
893 The use case for this is if we have a query block that's not top-level,
894 but we figure out after the fact that we wanted to run it anyway.
895 The typical case would be that we notice that the query block can return
896 at most one row (a so-called const table), and want to run it during
897 optimization.
898 */
899 bool force_create_iterators(THD *thd);
900
901 /**
902 Creates iterators for the access paths created by optimize(). Usually called
903 on a top-level Query_expression, but can also be called on non-top level
904 expressions from force_create_iterators(). See force_create_iterators() for
905 details.
906 */
907 bool create_iterators(THD *thd);
908
909 /// See optimize().
910 bool unfinished_materialization() const { return !m_operands.empty(); }
911
912 /// See optimize().
915 return std::move(m_operands);
916 }
917
918 /// Set new query result object for this query expression
920
921 /**
922 Whether there is a chance that optimize() is capable of materializing
923 directly into a result table if given one. Note that even if this function
924 returns true, optimize() can choose later not to do so, since it depends
925 on information (in particular, whether the query blocks can run under
926 the iterator executor or not) that is not available before optimize time.
927
928 TODO(sgunders): Now that all query blocks can run under the iterator
929 executor, the above may no longer be true. This needs investigation.
930 */
932
933 bool prepare(THD *thd, Query_result *result,
934 mem_root_deque<Item *> *insert_field_list,
935 ulonglong added_options, ulonglong removed_options);
936
937 /**
938 If and only if materialize_destination is non-nullptr, it means that the
939 caller intends to materialize our result into the given table. If it is
940 advantageous (in particular, if this query expression is a UNION DISTINCT),
941 optimize() will not create an iterator by itself, but rather do an
942 unfinished materialize. This means that it will collect iterators for
943 all the query blocks and prepare them for materializing into the given
944 table, but not actually create a root iterator for this query expression;
945 the caller is responsible for calling release_query_blocks_to_materialize()
946 and creating the iterator itself.
947
948 Even if materialize_destination is non-nullptr, this function may choose
949 to make a regular iterator. The caller is responsible for checking
950 unfinished_materialization() if it has given a non-nullptr table.
951
952 @param thd Thread handle.
953
954 @param materialize_destination What table to try to materialize into,
955 or nullptr if the caller does not intend to materialize the result.
956
957 @param finalize_access_paths Relevant for the hypergraph optimizer only.
958 If false, the given access paths will _not_ be finalized, so you cannot
959 create iterators from it before finalize() is called (see
960 FinalizePlanForQueryBlock()), and create_iterators must also be false.
961 This is relevant only if you are potentially optimizing multiple times
962 (see change_to_access_path_without_in2exists()), since you are only
963 allowed to finalize a query block once. "Fake" query blocks (see
964 query_term.h) are always finalized.
965 */
966 bool optimize(THD *thd, TABLE *materialize_destination,
967 bool finalize_access_paths);
968
969 /**
970 For any non-finalized query block, finalize it so that we are allowed to
971 create iterators. Must be called after the final access path is chosen
972 (ie., after any calls to change_to_access_path_without_in2exists()).
973 */
974 bool finalize(THD *thd);
975
976#ifndef NDEBUG
977 void DebugPrintQueryPlan(THD *thd, const char *keyword) const;
978#endif
979
980 /**
981 Do everything that would be needed before running Init() on the root
982 iterator. In particular, clear out data from previous execution iterations,
983 if needed.
984 */
985 bool ClearForExecution();
986
987 bool ExecuteIteratorQuery(THD *thd);
988 bool execute(THD *thd);
989 bool explain(THD *explain_thd, const THD *query_thd);
990 bool explain_query_term(THD *explain_thd, const THD *query_thd,
991 Query_term *qt);
992 void cleanup(bool full);
993 /**
994 Destroy contained objects, in particular temporary tables which may
995 have their own mem_roots.
996 */
997 void destroy();
998
999 void print(const THD *thd, String *str, enum_query_type query_type);
1000 bool accept(Select_lex_visitor *visitor);
1001
1002 /**
1003 Create a block to be used for ORDERING and LIMIT/OFFSET processing of a
1004 query term, which isn't itself a query specification or table value
1005 constructor. Such blocks are not included in the list starting in
1006 Query_Expression::first_query_block, and Query_block::next_query_block().
1007 They blocks are accessed via Query_term::query_block().
1008
1009 @param term the term on behalf of which we are making a post processing
1010 block
1011 @returns a query block
1012 */
1014
1015 bool prepare_query_term(THD *thd, Query_term *qts,
1016 Query_result *common_result, ulonglong added_options,
1017 ulonglong create_options, int level,
1018 Mem_root_array<bool> &nullable);
1020 assert(!is_prepared());
1021 prepared = true;
1022 }
1024 assert(is_prepared() && !is_optimized());
1025 optimized = true;
1026 }
1028 // assert(is_prepared() && is_optimized() && !is_executed());
1029 assert(is_prepared() && is_optimized());
1030 executed = true;
1031 }
1032 /// Reset this query expression for repeated evaluation within same execution
1034 assert(is_prepared() && is_optimized());
1035 executed = false;
1036 }
1037 /// Clear execution state, needed before new execution of prepared statement
1039 // Cannot be enforced when called from Prepared_statement::execute():
1040 // assert(is_prepared());
1041 optimized = false;
1042 executed = false;
1043 cleaned = UC_DIRTY;
1044 }
1045 /// Check state of preparation of the contained query expression.
1046 bool is_prepared() const { return prepared; }
1047 /// Check state of optimization of the contained query expression.
1048 bool is_optimized() const { return optimized; }
1049 /**
1050 Check state of execution of the contained query expression.
1051 Should not be used to check the state of a complete statement, use
1052 LEX::is_exec_completed() instead.
1053 */
1054 bool is_executed() const { return executed; }
1056 Query_result_interceptor *old_result);
1057 bool set_limit(THD *thd, Query_block *provider);
1058 bool has_any_limit() const;
1059
1060 inline bool is_union() const;
1061 inline bool is_set_operation() const;
1062
1063 /// Include a query expression below a query block.
1064 void include_down(LEX *lex, Query_block *outer);
1065
1066 /// Exclude this unit and immediately contained query_block objects
1067 void exclude_level();
1068
1069 /// Exclude subtree of current unit from tree of SELECTs
1070 void exclude_tree();
1071
1072 /// Renumber query blocks of a query expression according to supplied LEX
1073 void renumber_selects(LEX *lex);
1074
1076 bool save_cmd_properties(THD *thd);
1077
1078 friend class Query_block;
1079
1082 size_t num_visible_fields() const;
1083
1084 // If we are doing a query with global LIMIT, we need somewhere to store the
1085 // record count for FOUND_ROWS(). It can't be in any of the JOINs, since
1086 // they may have their own LimitOffsetIterators, which will write to
1087 // join->send_records whenever there is an OFFSET. Thus, we'll keep it here
1088 // instead.
1090
1093 void set_explain_marker_from(THD *thd, const Query_expression *u);
1094
1095#ifndef NDEBUG
1096 /**
1097 Asserts that none of {this unit and its children units} is fully cleaned
1098 up.
1099 */
1101#else
1102 void assert_not_fully_clean() {}
1103#endif
1104 void invalidate();
1105
1106 bool is_recursive() const { return first_recursive != nullptr; }
1107
1109
1111
1112 void fix_after_pullout(Query_block *parent_query_block,
1113 Query_block *removed_query_block);
1114
1115 /**
1116 If unit is a subquery, which forms an object of the upper level (an
1117 Item_subselect, a derived Table_ref), adds to this object a map
1118 of tables of the upper level which the unit references.
1119 */
1121
1122 /**
1123 If unit is a subquery, which forms an object of the upper level (an
1124 Item_subselect, a derived Table_ref), returns the place of this object
1125 in the upper level query block.
1126 */
1128
1129 bool walk(Item_processor processor, enum_walk walk, uchar *arg);
1130
1131 /**
1132 Replace all targeted items using transformer provided and info in
1133 arg.
1134 */
1135 bool replace_items(Item_transformer t, uchar *arg);
1136
1137 /*
1138 An exception: this is the only function that needs to adjust
1139 explain_marker.
1140 */
1141 friend bool parse_view_definition(THD *thd, Table_ref *view_ref);
1142};
1143
1146
1147/**
1148 Query_block type enum
1149*/
1151 EXPLAIN_NONE = 0,
1164 // Total:
1165 EXPLAIN_total ///< fake type, total number of all valid types
1166
1167 // Don't insert new types below this line!
1168};
1169
1170/**
1171 This class represents a query block, aka a query specification, which is
1172 a query consisting of a SELECT keyword, followed by a table list,
1173 optionally followed by a WHERE clause, a GROUP BY, etc.
1174*/
1175class Query_block : public Query_term {
1176 public:
1177 /**
1178 @note the group_by and order_by lists below will probably be added to the
1179 constructor when the parser is converted into a true bottom-up design.
1180
1181 //SQL_I_LIST<ORDER> *group_by, SQL_I_LIST<ORDER> order_by
1182 */
1184
1185 /// Query_term methods overridden
1186 void debugPrint(int level, std::ostringstream &buf) const override;
1187 /// Minion of debugPrint
1188 void qbPrint(int level, std::ostringstream &buf) const;
1189 Query_term_type term_type() const override { return QT_QUERY_BLOCK; }
1190 const char *operator_string() const override { return "query_block"; }
1191 Query_block *query_block() const override {
1192 return const_cast<Query_block *>(this);
1193 }
1194 void label_children() override {}
1195 void destroy_tree() override { m_parent = nullptr; }
1196
1197 bool open_result_tables(THD *, int) override;
1198 /// end of overridden methods from Query_term
1199 bool absorb_limit_of(Query_block *block);
1200
1201 Item *where_cond() const { return m_where_cond; }
1203 void set_where_cond(Item *cond) { m_where_cond = cond; }
1204 Item *having_cond() const { return m_having_cond; }
1206 void set_having_cond(Item *cond) { m_having_cond = cond; }
1207 Item *qualify_cond() const { return m_qualify_cond; }
1209 void set_qualify_cond(Item *cond) { m_qualify_cond = cond; }
1212 bool change_query_result(THD *thd, Query_result_interceptor *new_result,
1213 Query_result_interceptor *old_result);
1214
1215 /// Set base options for a query block (and active options too)
1216 void set_base_options(ulonglong options_arg) {
1217 DBUG_EXECUTE_IF("no_const_tables", options_arg |= OPTION_NO_CONST_TABLES;);
1218
1219 // Make sure we do not overwrite options by accident
1220 assert(m_base_options == 0 && m_active_options == 0);
1221 m_base_options = options_arg;
1222 m_active_options = options_arg;
1223 }
1224
1225 /// Add base options to a query block, also update active options
1227 assert(first_execution);
1230 }
1231
1232 /**
1233 Remove base options from a query block.
1234 Active options are also updated, and we assume here that "extra" options
1235 cannot override removed base options.
1236 */
1238 assert(first_execution);
1241 }
1242
1243 /// Make active options from base options, supplied options and environment:
1244 void make_active_options(ulonglong added_options, ulonglong removed_options);
1245
1246 /// Adjust the active option set
1248
1249 /// @return the active query options
1251
1252 /**
1253 Set associated tables as read_only, ie. they cannot be inserted into,
1254 updated or deleted from during this statement.
1255 Commonly used for query blocks that are part of derived tables or
1256 views that are materialized.
1257 */
1259 // Set all referenced base tables as read only.
1260 for (Table_ref *tr = leaf_tables; tr != nullptr; tr = tr->next_leaf)
1261 tr->set_readonly();
1262 }
1263
1264 /// @returns a map of all tables references in the query block
1265 table_map all_tables_map() const { return (1ULL << leaf_table_count) - 1; }
1266
1267 bool remove_aggregates(THD *thd, Query_block *select);
1268
1272 Query_block *next_query_block() const { return next; }
1273
1275
1277
1278 void mark_as_dependent(Query_block *last, bool aggregate);
1279
1280 /// @returns true if query block references any tables
1281 bool has_tables() const { return m_table_list.elements != 0; }
1282
1283 /// @return true if query block is explicitly grouped (non-empty GROUP BY)
1284 bool is_explicitly_grouped() const { return group_list.elements != 0; }
1285
1286 /**
1287 @return true if this query block is implicitly grouped, ie it is not
1288 explicitly grouped but contains references to set functions.
1289 The query will return max. 1 row (@see also is_single_grouped()).
1290 */
1292 return m_agg_func_used && group_list.elements == 0;
1293 }
1294
1295 /**
1296 @return true if this query block has GROUP BY modifier.
1297 */
1299 return (olap != UNSPECIFIED_OLAP_TYPE);
1300 }
1301
1302 /**
1303 @return true if this query block is explicitly or implicitly grouped.
1304 @note a query with DISTINCT is not considered to be aggregated.
1305 @note in standard SQL, a query with HAVING is defined as grouped, however
1306 MySQL allows HAVING without any aggregation to be the same as WHERE.
1307 */
1308 bool is_grouped() const { return group_list.elements > 0 || m_agg_func_used; }
1309
1310 /// @return true if this query block contains DISTINCT at start of select list
1311 bool is_distinct() const { return active_options() & SELECT_DISTINCT; }
1312
1313 /**
1314 @return true if this query block contains an ORDER BY clause.
1315
1316 @note returns false if ORDER BY has been eliminated, e.g if the query
1317 can return max. 1 row.
1318 */
1319 bool is_ordered() const { return order_list.elements > 0; }
1320
1321 /**
1322 Based on the structure of the query at resolution time, it is possible to
1323 conclude that DISTINCT is useless and remove it.
1324 This is the case if:
1325 - all GROUP BY expressions are in SELECT list, so resulting group rows are
1326 distinct,
1327 - and ROLLUP is not specified, so it adds no row for NULLs.
1328
1329 @returns true if we can remove DISTINCT.
1330
1331 @todo could refine this to if ROLLUP were specified and all GROUP
1332 expressions were non-nullable, because ROLLUP then adds only NULL values.
1333 Currently, ROLLUP+DISTINCT is rejected because executor cannot handle
1334 it in all cases.
1335 */
1336 bool can_skip_distinct() const {
1337 return is_grouped() && hidden_group_field_count == 0 &&
1339 }
1340
1341 /// @return true if this query block has a LIMIT clause
1342 bool has_limit() const { return select_limit != nullptr; }
1343
1344 /// @return true if query block references full-text functions
1345 bool has_ft_funcs() const { return ftfunc_list->elements > 0; }
1346
1347 /// @returns true if query block is a recursive member of a recursive unit
1348 bool is_recursive() const { return recursive_reference != nullptr; }
1349
1350 /**
1351 Finds a group expression matching the given item, or nullptr if
1352 none. When there are multiple candidates, ones that match in name are
1353 given priority (such that “a AS c GROUP BY a,b,c” resolves to c, not a);
1354 if there is still a tie, the leftmost is given priority.
1355
1356 @param item The item to search for.
1357 @param [out] rollup_level If not nullptr, will be set to the group
1358 expression's index (0-based).
1359 */
1360 ORDER *find_in_group_list(Item *item, int *rollup_level) const;
1361 int group_list_size() const;
1362
1363 /// @returns true if query block contains window functions
1364 bool has_windows() const { return m_windows.elements > 0; }
1365
1366 void invalidate();
1367
1368 uint get_in_sum_expr() const { return in_sum_expr; }
1369
1370 bool add_item_to_list(Item *item);
1372 Table_ref *add_table_to_list(THD *thd, Table_ident *table, const char *alias,
1373 ulong table_options,
1375 enum_mdl_type mdl_type = MDL_SHARED_READ,
1376 List<Index_hint> *hints = nullptr,
1377 List<String> *partition_names = nullptr,
1378 LEX_STRING *option = nullptr,
1379 Parse_context *pc = nullptr);
1380
1381 /**
1382 Add item to the hidden part of select list
1383
1384 @param item item to add
1385
1386 @return Pointer to reference of the added item
1387 */
1388 Item **add_hidden_item(Item *item);
1389
1390 /// Remove hidden items from select list
1391 void remove_hidden_items();
1392
1393 Table_ref *get_table_list() const { return m_table_list.first; }
1394 bool init_nested_join(THD *thd);
1396 Table_ref *nest_last_join(THD *thd, size_t table_cnt = 2);
1399
1400 /// Wrappers over fields / get_fields_list() that hide items where
1401 /// item->hidden, meant for range-based for loops. See sql/visible_fields.h.
1403 auto visible_fields() const { return VisibleFields(fields); }
1404
1405 /// Check privileges for views that are merged into query block
1406 bool check_view_privileges(THD *thd, Access_bitmask want_privilege_first,
1407 Access_bitmask want_privilege_next);
1408 /// Check privileges for all columns referenced from query block
1409 bool check_column_privileges(THD *thd);
1410
1411 /// Check privileges for column references in subqueries of a query block
1413
1414 /// Resolve and prepare information about tables for one query block
1415 bool setup_tables(THD *thd, Table_ref *tables, bool select_insert);
1416
1417 /// Resolve OFFSET and LIMIT clauses
1418 bool resolve_limits(THD *thd);
1419
1420 /// Resolve derived table, view, table function information for a query block
1421 bool resolve_placeholder_tables(THD *thd, bool apply_semijoin);
1422
1423 /// Propagate exclusion from table uniqueness test into subqueries
1425
1426 /// Merge name resolution context objects of a subquery into its parent
1427 void merge_contexts(Query_block *inner);
1428
1429 /// Merge derived table into query block
1430 bool merge_derived(THD *thd, Table_ref *derived_table);
1431
1432 bool flatten_subqueries(THD *thd);
1433
1434 /**
1435 Update available semijoin strategies for semijoin nests.
1436
1437 Available semijoin strategies needs to be updated on every execution since
1438 optimizer_switch setting may have changed.
1439
1440 @param thd Pointer to THD object for session.
1441 Used to access optimizer_switch
1442 */
1444
1445 /**
1446 Returns which subquery execution strategies can be used for this query
1447 block.
1448
1449 @param thd Pointer to THD object for session.
1450 Used to access optimizer_switch
1451
1452 @retval SUBQ_MATERIALIZATION Subquery Materialization should be used
1453 @retval SUBQ_EXISTS In-to-exists execution should be used
1454 @retval CANDIDATE_FOR_IN2EXISTS_OR_MAT A cost-based decision should be made
1455 */
1456 Subquery_strategy subquery_strategy(const THD *thd) const;
1457
1458 /**
1459 Returns whether semi-join is enabled for this query block
1460
1461 @see @c Opt_hints_qb::semijoin_enabled for details on how hints
1462 affect this decision. If there are no hints for this query block,
1463 optimizer_switch setting determines whether semi-join is used.
1464
1465 @param thd Pointer to THD object for session.
1466 Used to access optimizer_switch
1467
1468 @return true if semijoin is enabled,
1469 false otherwise
1470 */
1471 bool semijoin_enabled(const THD *thd) const;
1472
1474 sj_candidates = sj_cand;
1475 }
1477 sj_candidates->push_back(predicate);
1478 }
1479 bool has_sj_candidates() const {
1480 return sj_candidates != nullptr && !sj_candidates->empty();
1481 }
1482
1483 bool has_subquery_transforms() const { return sj_candidates != nullptr; }
1484
1485 /// Add full-text function elements from a list into this query block
1487
1488 void set_lock_for_table(const Lock_descriptor &descriptor, Table_ref *table);
1489
1490 void set_lock_for_tables(thr_lock_type lock_type);
1491
1492 inline void init_order() {
1493 assert(order_list.elements == 0);
1494 order_list.elements = 0;
1495 order_list.first = nullptr;
1496 order_list.next = &order_list.first;
1497 }
1498 /*
1499 This method created for reiniting LEX in mysql_admin_table() and can be
1500 used only if you are going remove all Query_block & units except belonger
1501 to LEX (LEX::unit & LEX::select, for other purposes use
1502 Query_expression::exclude_level()
1503 */
1504 void cut_subtree() { slave = nullptr; }
1505 bool test_limit();
1506 /**
1507 Get offset for LIMIT.
1508
1509 Evaluate offset item if necessary.
1510
1511 @return Number of rows to skip.
1512
1513 @todo Integrate better with Query_expression::set_limit()
1514 */
1515 ha_rows get_offset(const THD *thd) const;
1516 /**
1517 Get limit.
1518
1519 Evaluate limit item if necessary.
1520
1521 @return Limit of rows in result.
1522
1523 @todo Integrate better with Query_expression::set_limit()
1524 */
1525 ha_rows get_limit(const THD *thd) const;
1526
1527 /// Assign a default name resolution object for this query block.
1528 bool set_context(Name_resolution_context *outer_context);
1529
1530 /// Setup the array containing references to base items
1531 bool setup_base_ref_items(THD *thd);
1532 void print(const THD *thd, String *str, enum_query_type query_type);
1533
1534 /**
1535 Print detail of the Query_block object.
1536
1537 @param thd Thread handler
1538 @param query_type Options to print out string output
1539 @param[out] str String of output.
1540 */
1541 void print_query_block(const THD *thd, String *str,
1542 enum_query_type query_type);
1543
1544 /**
1545 Print detail of the UPDATE statement.
1546
1547 @param thd Thread handler
1548 @param[out] str String of output
1549 @param query_type Options to print out string output
1550 */
1551 void print_update(const THD *thd, String *str, enum_query_type query_type);
1552
1553 /**
1554 Print detail of the DELETE statement.
1555
1556 @param thd Thread handler
1557 @param[out] str String of output
1558 @param query_type Options to print out string output
1559 */
1560 void print_delete(const THD *thd, String *str, enum_query_type query_type);
1561
1562 /**
1563 Print detail of the INSERT statement.
1564
1565 @param thd Thread handler
1566 @param[out] str String of output
1567 @param query_type Options to print out string output
1568 */
1569 void print_insert(const THD *thd, String *str, enum_query_type query_type);
1570
1571 /**
1572 Print detail of Hints.
1573
1574 @param thd Thread handler
1575 @param[out] str String of output
1576 @param query_type Options to print out string output
1577 */
1578 void print_hints(const THD *thd, String *str, enum_query_type query_type);
1579
1580 /**
1581 Print error.
1582
1583 @param thd Thread handler
1584 @param[out] str String of output
1585
1586 @retval false If there is no error
1587 @retval true else
1588 */
1589 bool print_error(const THD *thd, String *str);
1590
1591 /**
1592 Print select options.
1593
1594 @param[out] str String of output
1595 */
1597
1598 /**
1599 Print UPDATE options.
1600
1601 @param[out] str String of output
1602 */
1604
1605 /**
1606 Print DELETE options.
1607
1608 @param[out] str String of output
1609 */
1611
1612 /**
1613 Print INSERT options.
1614
1615 @param[out] str String of output
1616 */
1618
1619 /**
1620 Print list of tables.
1621
1622 @param thd Thread handler
1623 @param[out] str String of output
1624 @param table_list Table_ref object
1625 @param query_type Options to print out string output
1626 */
1627 void print_table_references(const THD *thd, String *str,
1628 Table_ref *table_list,
1629 enum_query_type query_type);
1630
1631 /**
1632 Print list of items in Query_block object.
1633
1634 @param thd Thread handle
1635 @param[out] str String of output
1636 @param query_type Options to print out string output
1637 */
1638 void print_item_list(const THD *thd, String *str, enum_query_type query_type);
1639
1640 /**
1641 Print assignments list. Used in UPDATE and
1642 INSERT ... ON DUPLICATE KEY UPDATE ...
1643
1644 @param thd Thread handle
1645 @param[out] str String of output
1646 @param query_type Options to print out string output
1647 @param fields List columns to be assigned.
1648 @param values List of values.
1649 */
1650 void print_update_list(const THD *thd, String *str,
1651 enum_query_type query_type,
1653 const mem_root_deque<Item *> &values);
1654
1655 /**
1656 Print column list to be inserted into. Used in INSERT.
1657
1658 @param thd Thread handle
1659 @param[out] str String of output
1660 @param query_type Options to print out string output
1661 */
1662 void print_insert_fields(const THD *thd, String *str,
1663 enum_query_type query_type);
1664
1665 /**
1666 Print list of values, used in INSERT and for general VALUES clause.
1667
1668 @param thd Thread handle
1669 @param[out] str String of output
1670 @param query_type Options to print out string output
1671 @param values List of values
1672 @param prefix Prefix to print before each row in value list
1673 = nullptr: No prefix wanted
1674 */
1675 void print_values(const THD *thd, String *str, enum_query_type query_type,
1676 const mem_root_deque<mem_root_deque<Item *> *> &values,
1677 const char *prefix);
1678
1679 /**
1680 Print list of tables in FROM clause.
1681
1682 @param thd Thread handler
1683 @param[out] str String of output
1684 @param query_type Options to print out string output
1685 */
1686 void print_from_clause(const THD *thd, String *str,
1687 enum_query_type query_type);
1688
1689 /**
1690 Print list of conditions in WHERE clause.
1691
1692 @param thd Thread handle
1693 @param[out] str String of output
1694 @param query_type Options to print out string output
1695 */
1696 void print_where_cond(const THD *thd, String *str,
1697 enum_query_type query_type);
1698
1699 /**
1700 Print list of items in GROUP BY clause.
1701
1702 @param thd Thread handle
1703 @param[out] str String of output
1704 @param query_type Options to print out string output
1705 */
1706 void print_group_by(const THD *thd, String *str, enum_query_type query_type);
1707
1708 /**
1709 Print list of items in HAVING clause.
1710
1711 @param thd Thread handle
1712 @param[out] str String of output
1713 @param query_type Options to print out string output
1714 */
1715 void print_having(const THD *thd, String *str, enum_query_type query_type);
1716
1717 /**
1718 Print list of items in QUALIFY clause.
1719
1720 @param thd Thread handle
1721 @param[out] str String of output
1722 @param query_type Options to print out string output
1723 */
1724 void print_qualify(const THD *thd, String *str,
1725 enum_query_type query_type) const;
1726
1727 /**
1728 Print details of Windowing functions.
1729
1730 @param thd Thread handler
1731 @param[out] str String of output
1732 @param query_type Options to print out string output
1733 */
1734 void print_windows(const THD *thd, String *str, enum_query_type query_type);
1735
1736 /**
1737 Print list of items in ORDER BY clause.
1738
1739 @param thd Thread handle
1740 @param[out] str String of output
1741 @param query_type Options to print out string output
1742 */
1743 void print_order_by(const THD *thd, String *str,
1744 enum_query_type query_type) const;
1745
1746 void print_limit(const THD *thd, String *str,
1747 enum_query_type query_type) const;
1748 bool save_properties(THD *thd);
1749
1750 /**
1751 Accept function for SELECT and DELETE.
1752
1753 @param visitor Select_lex_visitor Object
1754 */
1755 bool accept(Select_lex_visitor *visitor);
1756
1757 /**
1758 Cleanup this subtree (this Query_block and all nested Query_blockes and
1759 Query_expressions).
1760 @param full if false only partial cleanup is done, JOINs and JOIN_TABs are
1761 kept to provide info for EXPLAIN CONNECTION; if true, complete cleanup is
1762 done, all JOINs are freed.
1763 */
1764 void cleanup(bool full) override;
1765 /*
1766 Recursively cleanup the join of this select lex and of all nested
1767 select lexes. This is not a full cleanup.
1768 */
1769 void cleanup_all_joins();
1770 /**
1771 Destroy contained objects, in particular temporary tables which may
1772 have their own mem_roots.
1773 */
1774 void destroy();
1775
1776 /// @return true when query block is not part of a set operation and is not a
1777 /// parenthesized query expression.
1780 }
1781
1782 /**
1783 @return true if query block is found during preparation to produce no data.
1784 Notice that if query is implicitly grouped, an aggregation row will
1785 still be returned.
1786 */
1787 bool is_empty_query() const { return m_empty_query; }
1788
1789 /// Set query block as returning no data
1790 /// @todo This may also be set when we have an always false WHERE clause
1792 assert(join == nullptr);
1793 m_empty_query = true;
1794 }
1795 /*
1796 For MODE_ONLY_FULL_GROUP_BY we need to know if
1797 this query block is the aggregation query of at least one aggregate
1798 function.
1799 */
1800 bool agg_func_used() const { return m_agg_func_used; }
1802
1803 void set_agg_func_used(bool val) { m_agg_func_used = val; }
1804
1806
1807 bool right_joins() const { return m_right_joins; }
1809
1810 /// Lookup for Query_block type
1811 enum_explain_type type() const;
1812
1813 /// Lookup for a type string
1814 const char *get_type_str() { return type_str[static_cast<int>(type())]; }
1816 return type_str[static_cast<int>(type)];
1817 }
1818
1820 bool is_cacheable() const { return !uncacheable; }
1821
1822 /// @returns true if this query block outputs at most one row.
1824 return (m_table_list.size() == 0 &&
1825 (!is_table_value_constructor || row_value_list->size() == 1));
1826 }
1827
1828 /// Include query block inside a query expression.
1829 void include_down(LEX *lex, Query_expression *outer);
1830
1831 /// Include a query block next to another query block.
1832 void include_neighbour(LEX *lex, Query_block *before);
1833
1834 /// Include query block inside a query expression, but do not link.
1836
1837 /// Include query block into global list.
1838 void include_in_global(Query_block **plink);
1839
1840 /// Include chain of query blocks into global list.
1842
1843 /// Renumber query blocks of contained query expressions
1844 void renumber(LEX *lex);
1845
1846 /**
1847 Does permanent transformations which are local to a query block (which do
1848 not merge it to another block).
1849 */
1850 bool apply_local_transforms(THD *thd, bool prune);
1851
1852 /// Pushes parts of the WHERE condition of this query block to materialized
1853 /// derived tables.
1855
1856 bool get_optimizable_conditions(THD *thd, Item **new_where,
1857 Item **new_having);
1858
1859 bool validate_outermost_option(LEX *lex, const char *wrong_option) const;
1860 bool validate_base_options(LEX *lex, ulonglong options) const;
1861
1862 bool walk(Item_processor processor, enum_walk walk, uchar *arg);
1863
1864 bool add_tables(THD *thd, const Mem_root_array<Table_ident *> *tables,
1865 ulong table_options, thr_lock_type lock_type,
1866 enum_mdl_type mdl_type);
1867
1868 bool resolve_rollup_wfs(THD *thd);
1869
1870 bool setup_conds(THD *thd);
1871 bool prepare(THD *thd, mem_root_deque<Item *> *insert_field_list);
1872 bool optimize(THD *thd, bool finalize_access_paths);
1873 void reset_nj_counters(mem_root_deque<Table_ref *> *join_list = nullptr);
1874
1875 // If the query block has exactly one single visible field, returns it.
1876 // If not, returns nullptr.
1877 Item *single_visible_field() const;
1878 size_t num_visible_fields() const;
1879
1880 // Whether the SELECT list is empty (hidden fields are ignored).
1881 // Typically used to distinguish INSERT INTO ... SELECT queries
1882 // from INSERT INTO ... VALUES queries.
1883 bool field_list_is_empty() const;
1884
1885 /// Creates a clone for the given expression by re-parsing the
1886 /// expression. Used in condition pushdown to derived tables.
1887 Item *clone_expression(THD *thd, Item *item, Table_ref *derived_table);
1888 /// Returns an expression from the select list of the query block
1889 /// using the field's index in a derived table.
1890 Item *get_derived_expr(uint expr_index);
1891
1893 AccessPath *child_path, TABLE *dst_table) const;
1894
1895 // ************************************************
1896 // * Members (most of these should not be public) *
1897 // ************************************************
1898
1900 /**
1901 All expressions needed after join and filtering, ie., select list,
1902 group by list, having clause, window clause, order by clause,
1903 including hidden fields.
1904 Does not include join conditions nor where clause.
1905
1906 This should ideally be changed into Mem_root_array<Item *>, but
1907 find_order_in_list() depends on pointer stability (it stores a pointer
1908 to an element in referenced_by[]). Similarly, there are some instances
1909 of thd->change_item_tree() that store pointers to elements in this list.
1910
1911 Because of this, adding or removing elements in the middle is not allowed;
1912 std::deque guarantees pointer stability only in the face of adding
1913 or removing elements from either end, ie., {push,pop}_{front_back}.
1914
1915 Currently, all hidden items must be before all visible items.
1916 This is primarily due to the requirement for pointer stability
1917 but also because change_to_use_tmp_fields() depends on it when mapping
1918 items to ref_item_array indexes. It would be good to get rid of this
1919 requirement in the future.
1920 */
1922
1923 /**
1924 All windows defined on the select, both named and inlined
1925 */
1927
1928 /**
1929 A pointer to ftfunc_list_alloc, list of full text search functions.
1930 */
1933
1934 /// The VALUES items of a table value constructor.
1936
1937 /// List of semi-join nests generated for this query block
1939
1940 /// List of tables in FROM clause - use Table_ref::next_local to traverse
1942
1943 /**
1944 ORDER BY clause.
1945 This list may be mutated during optimization (by remove_const() in the old
1946 optimizer or by RemoveRedundantOrderElements() in the hypergraph optimizer),
1947 so for prepared statements, we keep a copy of the ORDER.next pointers in
1948 order_list_ptrs, and re-establish the original list before each execution.
1949 */
1952
1953 /**
1954 GROUP BY clause. This list may be mutated during optimization (by
1955 \c remove_const() in the old optimizer or by
1956 RemoveRedundantOrderElements() in the hypergraph optimizer), so for prepared
1957 statements, we keep a copy of the ORDER.next pointers in \c group_list_ptrs,
1958 and re-establish the original list before each execution. The list can also
1959 be temporarily pruned and restored by \c Group_check (if transform done,
1960 cf. \c Query_block::m_gl_size_orig).
1961 */
1964 /**
1965 For an explicitly grouped, correlated, scalar subquery which is transformed
1966 to join with derived tables: the number of added non-column expressions.
1967 Used for better functional dependency analysis since this is checked during
1968 prepare *after* transformations. Transforms will append inner expressions
1969 to the group by list, rendering the check too optimistic. To remedy this,
1970 we temporarily remove the added compound (i.e. not simple column)
1971 expressions when doing the full group by check. This is bit too
1972 pessimistic: we can get occasionally false positives (full group by check
1973 error). The underlying problem is that we do not perform full group by
1974 checking before transformations. See also \c Group_check's ctor and dtor.
1975 */
1977
1978 // Used so that AggregateIterator knows which items to signal when the rollup
1979 // level changes. Obviously only used in the presence of rollup.
1984
1985 /// Query-block-level hints, for this query block
1987
1988 char *db{nullptr};
1989
1990 /**
1991 If this query block is a recursive member of a recursive unit: the
1992 Table_ref, in this recursive member, referencing the query
1993 name.
1994 */
1996
1997 /// Reference to LEX that this query block belongs to
1998 LEX *parent_lex{nullptr};
1999
2000 /**
2001 The set of those tables whose fields are referenced in the select list of
2002 this select level.
2003 */
2005 table_map outer_join{0}; ///< Bitmap of all inner tables from outer joins
2006
2007 /**
2008 Context for name resolution for all column references except columns
2009 from joined tables.
2010 */
2012
2013 /**
2014 Pointer to first object in list of Name res context objects that have
2015 this query block as the base query block.
2016 Includes field "context" which is embedded in this query block.
2017 */
2019
2020 /**
2021 After optimization it is pointer to corresponding JOIN. This member
2022 should be changed only when THD::LOCK_query_plan mutex is taken.
2023 */
2024 JOIN *join{nullptr};
2025 /// Set of table references contained in outer-most join nest
2027 /// Pointer to the set of table references in the currently active join
2029 /// table embedding the above list
2031 /**
2032 Points to first leaf table of query block. After setup_tables() is done,
2033 this is a list of base tables and derived tables. After derived tables
2034 processing is done, this is a list of base tables only.
2035 Use Table_ref::next_leaf to traverse the list.
2036 */
2038 /// Last table for LATERAL join, used by table functions
2040
2041 /// LIMIT clause, NULL if no limit is given
2043 /// LIMIT ... OFFSET clause, NULL if no offset is given
2045 /// Whether we have LIMIT 1 and no OFFSET.
2046 bool m_limit_1{false};
2047 /**
2048 Circular linked list of aggregate functions in nested query blocks.
2049 This is needed if said aggregate functions depend on outer values
2050 from this query block; if so, we want to add them as hidden items
2051 in our own field list, to be able to evaluate them.
2052 @see Item_sum::check_sum_func
2053 */
2055
2056 /**
2057 Array of pointers to "base" items; one each for every selected expression
2058 and referenced item in the query block. All references to fields are to
2059 buffers associated with the primary input tables.
2060 */
2062
2063 uint select_number{0}; ///< Query block number (used for EXPLAIN)
2064
2065 /**
2066 Saved values of the WHERE and HAVING clauses. Allowed values are:
2067 - COND_UNDEF if the condition was not specified in the query or if it
2068 has not been optimized yet
2069 - COND_TRUE if the condition is always true
2070 - COND_FALSE if the condition is impossible
2071 - COND_OK otherwise
2072 */
2075
2076 /// Parse context: indicates where the current expression is being parsed
2078 /// Parse context: is inside a set function if this is positive
2080 /// Parse context: is inside a window function if this is positive
2082
2083 /**
2084 Three fields used by semi-join transformations to know when semi-join is
2085 possible, and in which condition tree the subquery predicate is located.
2086 */
2096 RESOLVE_NONE}; ///< Indicates part of query being resolved
2097
2098 /**
2099 Number of fields used in select list or where clause of current select
2100 and all inner subselects.
2101 */
2103 /**
2104 Number of items in the select list, HAVING clause, QUALIFY clause and ORDER
2105 BY clause. It is used to reserve space in the base_ref_items array so that
2106 it is big enough to hold hidden items for any of the expressions or
2107 sub-expressions in those clauses.
2108 */
2110 /// Number of arguments of and/or/xor in where/having/on
2112 /// Number of predicates after preparation
2113 uint cond_count{0};
2114 /// Number of between predicates in where/having/on
2116 /// Maximal number of elements in multiple equalities
2118
2119 /**
2120 Number of Item_sum-derived objects in this SELECT. Keeps count of
2121 aggregate functions and window functions(to allocate items in ref array).
2122 See Query_block::setup_base_ref_items.
2123 */
2125 /// Number of Item_sum-derived objects in children and descendant SELECTs
2127
2128 /// Keep track for allocation of base_ref_items: scalar subqueries may be
2129 /// replaced by a field during scalar_to_derived transformation
2131
2132 /// Number of materialized derived tables and views in this query block.
2134 /// Number of partitioned tables
2136
2137 /**
2138 Number of wildcards used in the SELECT list. For example,
2139 SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
2140 has 3 wildcards.
2141 */
2142 uint with_wild{0};
2143
2144 /// Number of leaf tables in this query block.
2146 /// Number of derived tables and views in this query block.
2148 /// Number of table functions in this query block
2150
2151 /**
2152 Nesting level of query block, outer-most query block has level 0,
2153 its subqueries have level 1, etc. @see also sql/item_sum.h.
2154 */
2156
2157 /**
2158 Indicates whether this query block contains non-primitive grouping (such as
2159 ROLLUP).
2160 */
2162
2163 /// @see enum_condition_context
2165
2166 /// If set, the query block is of the form VALUES row_list.
2168
2169 /// Describes context of this query block (e.g if it is a derived table).
2171
2172 /**
2173 result of this query can't be cached, bit field, can be :
2174 UNCACHEABLE_DEPENDENT
2175 UNCACHEABLE_RAND
2176 UNCACHEABLE_SIDEEFFECT
2177 */
2179
2180 void update_used_tables();
2182 bool save_cmd_properties(THD *thd);
2183
2184 /**
2185 This variable is required to ensure proper work of subqueries and
2186 stored procedures. Generally, one should use the states of
2187 Query_arena to determine if it's a statement prepare or first
2188 execution of a stored procedure. However, in case when there was an
2189 error during the first execution of a stored procedure, the SP body
2190 is not expelled from the SP cache. Therefore, a deeply nested
2191 subquery might be left unoptimized. So we need this per-subquery
2192 variable to inidicate the optimization/execution state of every
2193 subquery. Prepared statements work OK in that regard, as in
2194 case of an error during prepare the PS is not created.
2195 */
2197
2198 /// True when semi-join pull-out processing is complete
2199 bool sj_pullout_done{false};
2200
2201 /// Used by nested scalar_to_derived transformations
2203
2204 /// True: skip local transformations during prepare() call (used by INSERT)
2206
2208
2209 /// true when having fix field called in processing of this query block
2210 bool having_fix_field{false};
2211 /// true when GROUP BY fix field called in processing of this query block
2212 bool group_fix_field{false};
2213
2214 /**
2215 True if contains or aggregates set functions.
2216 @note this is wrong when a locally found set function is aggregated
2217 in an outer query block.
2218 */
2219 bool with_sum_func{false};
2220
2221 /**
2222 HAVING clause contains subquery => we can't close tables before
2223 query processing end even if we use temporary table
2224 */
2226
2227 /**
2228 If true, use select_limit to limit number of rows selected.
2229 Applicable when no explicit limit is supplied, and only for the
2230 outermost query block of a SELECT statement.
2231 */
2233
2234 /// If true, limit object is added internally
2235 bool m_internal_limit{false};
2236
2237 /// exclude this query block from unique_table() check
2239
2240 bool no_table_names_allowed{false}; ///< used for global order by
2241
2242 /// Hidden items added during optimization
2243 /// @note that using this means we modify resolved data during optimization
2245
2247
2248 private:
2249 friend class Query_expression;
2250 friend class Condition_context;
2251
2252 /// Helper for save_properties()
2254 Group_list_ptrs **list_ptrs);
2255
2257 bool simplify_joins(THD *thd, mem_root_deque<Table_ref *> *join_list,
2258 bool top, bool in_sj, Item **new_conds,
2259 uint *changelog = nullptr);
2260 /// Remove semijoin condition for this query block
2261 void clear_sj_expressions(NESTED_JOIN *nested_join);
2262 /// Build semijoin condition for th query block
2263 bool build_sj_cond(THD *thd, NESTED_JOIN *nested_join,
2264 Query_block *subq_query_block, table_map outer_tables_map,
2265 Item **sj_cond, bool *simple_const);
2267 Table_ref *join_nest);
2268
2271 Item *join_cond, bool left_outer,
2272 bool use_inner_join);
2273 bool transform_subquery_to_derived(THD *thd, Table_ref **out_tl,
2274 Query_expression *subs_query_expression,
2275 Item_subselect *subq, bool use_inner_join,
2276 bool reject_multiple_rows,
2277 Item::Css_info *subquery,
2278 Item *lifted_where_cond);
2280 THD *thd, Item_exists_subselect *subq_pred);
2282 THD *thd, Table_ref *derived, Lifted_expressions_map *lifted_expressions,
2283 mem_root_deque<Item *> &exprs_added_to_group_by, uint hidden_fields);
2285 Lifted_expressions_map *lifted_exprs,
2286 Item *selected_field_or_ref,
2287 const uint first_non_hidden);
2289 THD *thd, Lifted_expressions_map *lifted_exprs);
2291 THD *thd, List_iterator<Item> &inner_exprs, Item *selected_item,
2292 bool *selected_expr_added_to_group_by,
2293 mem_root_deque<Item *> *exprs_added_to_group_by);
2295 THD *thd, Table_ref *derived, Item::Css_info *subquery,
2296 Item *lifted_where, Lifted_expressions_map *lifted_where_expressions,
2297 bool *added_card_check, size_t *added_window_card_checks);
2299 THD *thd, Table_ref *derived, Lifted_expressions_map *lifted_exprs,
2300 bool added_card_check, size_t added_window_card_checks);
2301 /// Replace the first visible item in the select list with a wrapping
2302 /// MIN or MAX aggregate function.
2303 bool replace_first_item_with_min_max(THD *thd, int item_no, bool use_min);
2304 void replace_referenced_item(Item *const old_item, Item *const new_item);
2305 void remap_tables(THD *thd);
2307 Item *resolve_rollup_item(THD *thd, Item *item);
2308 bool resolve_rollup(THD *thd);
2309
2310 bool setup_wild(THD *thd);
2311 bool setup_order_final(THD *thd);
2312 bool setup_group(THD *thd);
2313 void fix_after_pullout(Query_block *parent_query_block,
2314 Query_block *removed_query_block);
2317 bool empty_order_list(Query_block *sl);
2319 bool in_update);
2320 bool find_common_table_expr(THD *thd, Table_ident *table_id, Table_ref *tl,
2321 Parse_context *pc, bool *found);
2322 /**
2323 Transform eligible scalar subqueries in the SELECT list, WHERE condition,
2324 HAVING condition or JOIN conditions of a query block[*] to an equivalent
2325 derived table of a LEFT OUTER join, e.g. as shown in this uncorrelated
2326 subquery:
2327
2328 [*] a.k.a "transformed query block" throughout this method and its minions.
2329
2330 <pre>
2331 SELECT * FROM t1
2332 WHERE t1.a > (SELECT COUNT(a) AS cnt FROM t2); ->
2333
2334 SELECT t1.* FROM t1 LEFT OUTER JOIN
2335 (SELECT COUNT(a) AS cnt FROM t2) AS derived
2336 ON TRUE WHERE t1.a > derived.cnt;
2337 </pre>
2338
2339 Grouping in the transformed query block may necessitate the grouping to be
2340 moved down to another derived table, cf. transform_grouped_to_derived.
2341
2342 Limitations:
2343 - only implicitly grouped subqueries (guaranteed to have cardinality one)
2344 are identified as scalar subqueries.
2345 _ Correlated subqueries are not handled
2346
2347 @param[in,out] thd the session context
2348 @returns true on error
2349 */
2352 Item **lifted_where);
2353 bool replace_item_in_expression(Item **expr, bool was_hidden,
2355 Item_transformer transformer);
2356 bool transform_grouped_to_derived(THD *thd, bool *break_off);
2357 bool replace_subquery_in_expr(THD *thd, Item::Css_info *subquery,
2358 Table_ref *tr, Item **expr);
2359 bool nest_derived(THD *thd, Item *join_cond,
2360 mem_root_deque<Table_ref *> *join_list,
2361 Table_ref *new_derived_table);
2362
2364
2365 // Delete unused columns from merged derived tables
2367
2368 bool prepare_values(THD *thd);
2369 bool check_only_full_group_by(THD *thd);
2370 /**
2371 Copies all non-aggregated calls to the full-text search MATCH function from
2372 the HAVING clause to the SELECT list (as hidden items), so that we can
2373 materialize their result and not only their input. This is needed when the
2374 result will be accessed after aggregation, as the result from MATCH cannot
2375 be recalculated from its input alone. It also needs the underlying scan to
2376 be positioned on the correct row. Storing the value before aggregation
2377 removes the need for evaluating MATCH again after materialization.
2378 */
2380
2381 //
2382 // Members:
2383 //
2384
2385 /**
2386 Pointer to collection of subqueries candidate for semi/antijoin
2387 conversion.
2388 Template parameter is "true": no need to run DTORs on pointers.
2389 */
2391
2392 /// How many expressions are part of the order by but not select list.
2394
2395 /**
2396 Intrusive linked list of all query blocks within the same
2397 query expression.
2398 */
2400
2401 /// The query expression containing this query block.
2403 /// The first query expression contained within this query block.
2405
2406 /// Intrusive double-linked global list of query blocks.
2409
2410 /// Result of this query block
2412
2413 /**
2414 Options assigned from parsing and throughout resolving,
2415 should not be modified after resolving is done.
2416 */
2418 /**
2419 Active options. Derived from base options, modifiers added during
2420 resolving and values from session variable option_bits. Since the latter
2421 may change, active options are refreshed per execution of a statement.
2422 */
2424
2425 /**
2426 If the query block includes non-primitive grouping, then these modifiers are
2427 represented as grouping sets. The variable 'm_num_grouping_sets' holds the
2428 count of grouping sets.
2429 */
2431
2432 public:
2434 nullptr}; ///< Used when resolving outer join condition
2435
2436 /**
2437 Initializes the grouping set if the query block includes GROUP BY
2438 modifiers.
2439 */
2440 bool allocate_grouping_sets(THD *thd);
2441
2442 /**
2443 Populates the grouping sets if the query block includes non-primitive
2444 grouping.
2445 */
2446 bool populate_grouping_sets(THD *thd);
2448
2449 private:
2450 /**
2451 Condition to be evaluated after all tables in a query block are joined.
2452 After all permanent transformations have been conducted by
2453 Query_block::prepare(), this condition is "frozen", any subsequent changes
2454 to it must be done with change_item_tree(), unless they only modify AND/OR
2455 items and use a copy created by Query_block::get_optimizable_conditions().
2456 Same is true for 'having_cond'.
2457 */
2459
2460 /// Condition to be evaluated on grouped rows after grouping.
2462
2463 /// Condition to be evaluated after window functions.
2465
2466 /// Number of GROUP BY expressions added to all_fields
2468
2469 /// A backup of the items in base_ref_items at the end of preparation, so that
2470 /// base_ref_items can be restored between executions of prepared statements.
2471 /// Empty if it's a regular statement.
2473
2474 /**
2475 True if query block has semi-join nests merged into it. Notice that this
2476 is updated earlier than sj_nests, so check this if info is needed
2477 before the full resolver process is complete.
2478 */
2479 bool has_sj_nests{false};
2480 bool has_aj_nests{false}; ///< @see has_sj_nests; counts antijoin nests.
2481 bool m_right_joins{false}; ///< True if query block has right joins
2482
2483 /// Allow merge of immediate unnamed derived tables
2485
2486 bool m_agg_func_used{false};
2488
2489 /**
2490 True if query block does not generate any rows before aggregation,
2491 determined during preparation (not optimization).
2492 */
2493 bool m_empty_query{false};
2494
2495 static const char
2497};
2498
2499inline bool Query_expression::is_union() const {
2500 Query_term *qt = query_term();
2501 while (qt->term_type() == QT_UNARY)
2502 qt = down_cast<Query_term_unary *>(qt)->m_children[0];
2503 return qt->term_type() == QT_UNION;
2504}
2505
2507 Query_term *qt = query_term();
2508 while (qt->term_type() == QT_UNARY)
2509 qt = down_cast<Query_term_unary *>(qt)->m_children[0];
2510 const Query_term_type type = qt->term_type();
2511 return type == QT_UNION || type == QT_INTERSECT || type == QT_EXCEPT;
2512}
2513
2514/// Utility RAII class to save/modify/restore the condition_context information
2515/// of a query block. @see enum_condition_context.
2517 public:
2519 Query_block *select_ptr,
2521 : select(nullptr), saved_value() {
2522 if (select_ptr) {
2523 select = select_ptr;
2525 // More restrictive wins over less restrictive:
2526 if (new_type == enum_condition_context::NEITHER ||
2527 (new_type == enum_condition_context::ANDS_ORS &&
2529 select->condition_context = new_type;
2530 }
2531 }
2534 }
2535
2536 private:
2539};
2540
2542 std::function<bool(Table_ref *)> action);
2543
2544/**
2545 Base class for secondary engine execution context objects. Secondary
2546 storage engines may create classes derived from this one which
2547 contain state they need to preserve between optimization and
2548 execution of statements. The context objects should be allocated on
2549 the execution MEM_ROOT.
2550*/
2552 public:
2553 /**
2554 Destructs the secondary engine execution context object. It is
2555 called after the query execution has completed. Secondary engines
2556 may override the destructor in subclasses and add code that
2557 performs cleanup tasks that are needed after query execution.
2558 */
2560};
2561
2563 char *user;
2567
2568 void reset();
2570
2576 LEX_CSTRING language; ///< CREATE|ALTER ... LANGUAGE <language>
2577};
2578
2579extern const LEX_STRING null_lex_str;
2580
2584
2585 /**
2586 FOLLOWS or PRECEDES as specified in the CREATE TRIGGER statement.
2587 */
2589
2590 /**
2591 Trigger name referenced in the FOLLOWS/PRECEDES clause of the CREATE TRIGGER
2592 statement.
2593 */
2595};
2596
2598
2599/*
2600 Class representing list of all tables used by statement and other
2601 information which is necessary for opening and locking its tables,
2602 like SQL command for this statement.
2603
2604 Also contains information about stored functions used by statement
2605 since during its execution we may have to add all tables used by its
2606 stored functions/triggers to this list in order to pre-open and lock
2607 them.
2608
2609 Also used by LEX::reset_n_backup/restore_backup_query_tables_list()
2610 methods to save and restore this information.
2611*/
2612
2614 public:
2616
2617 /**
2618 SQL command for this statement. Part of this class since the
2619 process of opening and locking tables for the statement needs
2620 this information to determine correct type of lock for some of
2621 the tables.
2622 */
2624 /* Global list of all tables used by this statement */
2626 /* Pointer to next_global member of last element in the previous list. */
2628 /*
2629 If non-0 then indicates that query requires prelocking and points to
2630 next_global member of last own element in query table list (i.e. last
2631 table which was not added to it as part of preparation to prelocking).
2632 0 - indicates that this query does not need prelocking.
2633 */
2635 /*
2636 Set of stored routines called by statement.
2637 (Note that we use lazy-initialization for this hash).
2638
2639 See Sroutine_hash_entry for explanation why this hash uses binary
2640 key comparison.
2641 */
2643 std::unique_ptr<malloc_unordered_map<std::string, Sroutine_hash_entry *>>
2645 /*
2646 List linking elements of 'sroutines' set. Allows you to add new elements
2647 to this set as you iterate through the list of existing elements.
2648 'sroutines_list_own_last' is pointer to ::next member of last element of
2649 this list which represents routine which is explicitly used by query.
2650 'sroutines_list_own_elements' number of explicitly used routines.
2651 We use these two members for restoring of 'sroutines_list' to the state
2652 in which it was right after query parsing.
2653 */
2657
2658 /**
2659 Locking state of tables in this particular statement.
2660
2661 If we under LOCK TABLES or in prelocked mode we consider tables
2662 for the statement to be "locked" if there was a call to lock_tables()
2663 (which called handler::start_stmt()) for tables of this statement
2664 and there was no matching close_thread_tables() call.
2665
2666 As result this state may differ significantly from one represented
2667 by Open_tables_state::lock/locked_tables_mode more, which are always
2668 "on" under LOCK TABLES or in prelocked mode.
2669 */
2673 return (lock_tables_state == LTS_LOCKED);
2674 }
2675
2676 /**
2677 Number of tables which were open by open_tables() and to be locked
2678 by lock_tables().
2679 Note that we set this member only in some cases, when this value
2680 needs to be passed from open_tables() to lock_tables() which are
2681 separated by some amount of code.
2682 */
2684
2685 /*
2686 These constructor and destructor serve for creation/destruction
2687 of Query_tables_list instances which are used as backup storage.
2688 */
2691
2692 /* Initializes (or resets) Query_tables_list object for "real" use. */
2693 void reset_query_tables_list(bool init);
2696 *this = std::move(*state);
2697 }
2698
2699 /*
2700 Direct addition to the list of query tables.
2701 If you are using this function, you must ensure that the table
2702 object, in particular table->db member, is initialized.
2703 */
2705 *(table->prev_global = query_tables_last) = table;
2706 query_tables_last = &table->next_global;
2707 }
2709 void mark_as_requiring_prelocking(Table_ref **tables_own_last) {
2710 query_tables_own_last = tables_own_last;
2711 }
2712 /* Return pointer to first not-own table in query-tables or 0 */
2714 return (query_tables_own_last ? *query_tables_own_last : nullptr);
2715 }
2718 *query_tables_own_last = nullptr;
2720 query_tables_own_last = nullptr;
2721 }
2722 }
2723
2724 /**
2725 All types of unsafe statements.
2726
2727 @note The int values of the enum elements are used to point to
2728 bits in two bitmaps in two different places:
2729
2730 - Query_tables_list::binlog_stmt_flags
2731 - THD::binlog_unsafe_warning_flags
2732
2733 Hence in practice this is not an enum at all, but a map from
2734 symbols to bit indexes.
2735
2736 The ordering of elements in this enum must correspond to the order of
2737 elements in the array binlog_stmt_unsafe_errcode.
2738 */
2740 /**
2741 SELECT..LIMIT is unsafe because the set of rows returned cannot
2742 be predicted.
2743 */
2745 /**
2746 Access to log tables is unsafe because slave and master probably
2747 log different things.
2748 */
2750 /**
2751 Inserting into an autoincrement column in a stored routine is unsafe.
2752 Even with just one autoincrement column, if the routine is invoked more
2753 than once slave is not guaranteed to execute the statement graph same way
2754 as the master. And since it's impossible to estimate how many times a
2755 routine can be invoked at the query pre-execution phase (see lock_tables),
2756 the statement is marked pessimistically unsafe.
2757 */
2759 /**
2760 Using a UDF (user-defined function) is unsafe.
2761 */
2763 /**
2764 Using most system variables is unsafe, because slave may run
2765 with different options than master.
2766 */
2768 /**
2769 Using some functions is unsafe (e.g., UUID).
2770 */
2772
2773 /**
2774 Mixing transactional and non-transactional statements are unsafe if
2775 non-transactional reads or writes are occur after transactional
2776 reads or writes inside a transaction.
2777 */
2779
2780 /**
2781 Mixing self-logging and non-self-logging engines in a statement
2782 is unsafe.
2783 */
2785
2786 /**
2787 Statements that read from both transactional and non-transactional
2788 tables and write to any of them are unsafe.
2789 */
2791
2792 /**
2793 INSERT...IGNORE SELECT is unsafe because which rows are ignored depends
2794 on the order that rows are retrieved by SELECT. This order cannot be
2795 predicted and may differ on master and the slave.
2796 */
2798
2799 /**
2800 INSERT...SELECT...UPDATE is unsafe because which rows are updated depends
2801 on the order that rows are retrieved by SELECT. This order cannot be
2802 predicted and may differ on master and the slave.
2803 */
2805
2806 /**
2807 Query that writes to a table with auto_inc column after selecting from
2808 other tables are unsafe as the order in which the rows are retrieved by
2809 select may differ on master and slave.
2810 */
2812
2813 /**
2814 INSERT...REPLACE SELECT is unsafe because which rows are replaced depends
2815 on the order that rows are retrieved by SELECT. This order cannot be
2816 predicted and may differ on master and the slave.
2817 */
2819
2820 /**
2821 CREATE TABLE... IGNORE... SELECT is unsafe because which rows are ignored
2822 depends on the order that rows are retrieved by SELECT. This order cannot
2823 be predicted and may differ on master and the slave.
2824 */
2826
2827 /**
2828 CREATE TABLE...REPLACE... SELECT is unsafe because which rows are replaced
2829 depends on the order that rows are retrieved from SELECT. This order
2830 cannot be predicted and may differ on master and the slave
2831 */
2833
2834 /**
2835 CREATE TABLE...SELECT on a table with auto-increment column is unsafe
2836 because which rows are replaced depends on the order that rows are
2837 retrieved from SELECT. This order cannot be predicted and may differ on
2838 master and the slave
2839 */
2841
2842 /**
2843 UPDATE...IGNORE is unsafe because which rows are ignored depends on the
2844 order that rows are updated. This order cannot be predicted and may differ
2845 on master and the slave.
2846 */
2848
2849 /**
2850 INSERT... ON DUPLICATE KEY UPDATE on a table with more than one
2851 UNIQUE KEYS is unsafe.
2852 */
2854
2855 /**
2856 INSERT into auto-inc field which is not the first part in composed
2857 primary key.
2858 */
2860
2861 /**
2862 Using a plugin is unsafe.
2863 */
2867
2868 /**
2869 XA transactions and statements.
2870 */
2872
2873 /**
2874 If a substatement inserts into or updates a table that has a column with
2875 an unsafe DEFAULT expression, it may not have the same effect on the
2876 slave.
2877 */
2879
2880 /**
2881 DML or DDL statement that reads a ACL table is unsafe, because the row
2882 are read without acquiring SE row locks. This would allow ACL tables to
2883 be updated by concurrent thread. It would not have the same effect on the
2884 slave.
2885 */
2887
2888 /**
2889 Generating invisible primary key for a table created using CREATE TABLE...
2890 SELECT... is unsafe because order in which rows are retrieved by the
2891 SELECT determines which (if any) rows are inserted. This order cannot be
2892 predicted and values for generated invisible primary key column may
2893 differ on source and replica when @@session.binlog_format=STATEMENT.
2894 */
2896
2897 /* the last element of this enumeration type. */
2900 /**
2901 This has all flags from 0 (inclusive) to BINLOG_STMT_FLAG_COUNT
2902 (exclusive) set.
2903 */
2905 ((1 << BINLOG_STMT_UNSAFE_COUNT) - 1);
2906
2907 /**
2908 Maps elements of enum_binlog_stmt_unsafe to error codes.
2909 */
2911
2912 /**
2913 Determine if this statement is marked as unsafe.
2914
2915 @retval 0 if the statement is not marked as unsafe.
2916 @retval nonzero if the statement is marked as unsafe.
2917 */
2918 inline bool is_stmt_unsafe() const { return get_stmt_unsafe_flags() != 0; }
2919
2921 return binlog_stmt_flags & (1 << unsafe);
2922 }
2923
2924 /**
2925 Flag the current (top-level) statement as unsafe.
2926 The flag will be reset after the statement has finished.
2927
2928 @param unsafe_type The type of unsafety: one of the @c
2929 BINLOG_STMT_FLAG_UNSAFE_* flags in @c enum_binlog_stmt_flag.
2930 */
2931 inline void set_stmt_unsafe(enum_binlog_stmt_unsafe unsafe_type) {
2932 DBUG_TRACE;
2933 assert(unsafe_type >= 0 && unsafe_type < BINLOG_STMT_UNSAFE_COUNT);
2934 binlog_stmt_flags |= (1U << unsafe_type);
2935 return;
2936 }
2937
2938 /**
2939 Set the bits of binlog_stmt_flags determining the type of
2940 unsafeness of the current statement. No existing bits will be
2941 cleared, but new bits may be set.
2942
2943 @param flags A binary combination of zero or more bits, (1<<flag)
2944 where flag is a member of enum_binlog_stmt_unsafe.
2945 */
2947 DBUG_TRACE;
2948 assert((flags & ~BINLOG_STMT_UNSAFE_ALL_FLAGS) == 0);
2950 return;
2951 }
2952
2953 /**
2954 Return a binary combination of all unsafe warnings for the
2955 statement. If the statement has been marked as unsafe by the
2956 'flag' member of enum_binlog_stmt_unsafe, then the return value
2957 from this function has bit (1<<flag) set to 1.
2958 */
2960 DBUG_TRACE;
2962 }
2963
2964 /**
2965 Determine if this statement is a row injection.
2966
2967 @retval 0 if the statement is not a row injection
2968 @retval nonzero if the statement is a row injection
2969 */
2970 inline bool is_stmt_row_injection() const {
2971 constexpr uint32_t shift =
2972 static_cast<uint32_t>(BINLOG_STMT_UNSAFE_COUNT) +
2973 static_cast<uint32_t>(BINLOG_STMT_TYPE_ROW_INJECTION);
2974 return binlog_stmt_flags & (1U << shift);
2975 }
2976
2977 /**
2978 Flag the statement as a row injection. A row injection is either
2979 a BINLOG statement, or a row event in the relay log executed by
2980 the slave SQL thread.
2981 */
2983 constexpr uint32_t shift =
2984 static_cast<uint32_t>(BINLOG_STMT_UNSAFE_COUNT) +
2985 static_cast<uint32_t>(BINLOG_STMT_TYPE_ROW_INJECTION);
2986 DBUG_TRACE;
2987 binlog_stmt_flags |= (1U << shift);
2988 }
2989
2991 /*
2992 If a transactional table is about to be read. Note that
2993 a write implies a read.
2994 */
2996 /*
2997 If a non-transactional table is about to be read. Note that
2998 a write implies a read.
2999 */
3001 /*
3002 If a temporary transactional table is about to be read. Note
3003 that a write implies a read.
3004 */
3006 /*
3007 If a temporary non-transactional table is about to be read. Note
3008 that a write implies a read.
3009 */
3011 /*
3012 If a transactional table is about to be updated.
3013 */
3015 /*
3016 If a non-transactional table is about to be updated.
3017 */
3019 /*
3020 If a temporary transactional table is about to be updated.
3021 */
3023 /*
3024 If a temporary non-transactional table is about to be updated.
3025 */
3027 /*
3028 The last element of the enumeration. Please, if necessary add
3029 anything before this.
3030 */
3033
3034#ifndef NDEBUG
3035 static inline const char *stmt_accessed_table_string(
3036 enum_stmt_accessed_table accessed_table) {
3037 switch (accessed_table) {
3039 return "STMT_READS_TRANS_TABLE";
3040 break;
3042 return "STMT_READS_NON_TRANS_TABLE";
3043 break;
3045 return "STMT_READS_TEMP_TRANS_TABLE";
3046 break;
3048 return "STMT_READS_TEMP_NON_TRANS_TABLE";
3049 break;
3051 return "STMT_WRITES_TRANS_TABLE";
3052 break;
3054 return "STMT_WRITES_NON_TRANS_TABLE";
3055 break;
3057 return "STMT_WRITES_TEMP_TRANS_TABLE";
3058 break;
3060 return "STMT_WRITES_TEMP_NON_TRANS_TABLE";
3061 break;
3063 default:
3064 assert(0);
3065 break;
3066 }
3068 return "";
3069 }
3070#endif /* DBUG */
3071
3072#define BINLOG_DIRECT_ON \
3073 0xF0 /* unsafe when \
3074 --binlog-direct-non-trans-updates \
3075 is ON */
3076
3077#define BINLOG_DIRECT_OFF \
3078 0xF /* unsafe when \
3079 --binlog-direct-non-trans-updates \
3080 is OFF */
3081
3082#define TRX_CACHE_EMPTY 0x33 /* unsafe when trx-cache is empty */
3083
3084#define TRX_CACHE_NOT_EMPTY 0xCC /* unsafe when trx-cache is not empty */
3085
3086#define IL_LT_REPEATABLE 0xAA /* unsafe when < ISO_REPEATABLE_READ */
3087
3088#define IL_GTE_REPEATABLE 0x55 /* unsafe when >= ISO_REPEATABLE_READ */
3089
3090 /**
3091 Sets the type of table that is about to be accessed while executing a
3092 statement.
3094 @param accessed_table Enumeration type that defines the type of table,
3095 e.g. temporary, transactional, non-transactional.
3096 */
3097 inline void set_stmt_accessed_table(enum_stmt_accessed_table accessed_table) {
3098 DBUG_TRACE;
3099
3100 assert(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
3101 stmt_accessed_table_flag |= (1U << accessed_table);
3102
3103 return;
3104 }
3105
3106 /**
3107 Checks if a type of table is about to be accessed while executing a
3108 statement.
3109
3110 @param accessed_table Enumeration type that defines the type of table,
3111 e.g. temporary, transactional, non-transactional.
3113 @retval true if the type of the table is about to be accessed
3114 @retval false otherwise
3115 */
3116 inline bool stmt_accessed_table(enum_stmt_accessed_table accessed_table) {
3117 DBUG_TRACE;
3118
3119 assert(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
3120
3121 return (stmt_accessed_table_flag & (1U << accessed_table)) != 0;
3122 }
3123
3124 /*
3125 Checks if a mixed statement is unsafe.
3126
3127
3128 @param in_multi_stmt_transaction_mode defines if there is an on-going
3129 multi-transactional statement.
3130 @param binlog_direct defines if --binlog-direct-non-trans-updates is
3131 active.
3132 @param trx_cache_is_not_empty defines if the trx-cache is empty or not.
3133 @param trx_isolation defines the isolation level.
3134
3135 @return
3136 @retval true if the mixed statement is unsafe
3137 @retval false otherwise
3138 */
3139 inline bool is_mixed_stmt_unsafe(bool in_multi_stmt_transaction_mode,
3140 bool binlog_direct,
3141 bool trx_cache_is_not_empty,
3142 uint tx_isolation) {
3143 bool unsafe = false;
3144
3145 if (in_multi_stmt_transaction_mode) {
3146 const uint condition =
3147 (binlog_direct ? BINLOG_DIRECT_ON : BINLOG_DIRECT_OFF) &
3148 (trx_cache_is_not_empty ? TRX_CACHE_NOT_EMPTY : TRX_CACHE_EMPTY) &
3149 (tx_isolation >= ISO_REPEATABLE_READ ? IL_GTE_REPEATABLE
3151
3152 unsafe = (binlog_unsafe_map[stmt_accessed_table_flag] & condition);
3153
3154#if !defined(NDEBUG)
3155 DBUG_PRINT("LEX::is_mixed_stmt_unsafe",
3156 ("RESULT %02X %02X %02X\n", condition,
3159
3160 int type_in = 0;
3161 for (; type_in < STMT_ACCESS_TABLE_COUNT; type_in++) {
3163 DBUG_PRINT("LEX::is_mixed_stmt_unsafe",
3164 ("ACCESSED %s ", stmt_accessed_table_string(
3165 (enum_stmt_accessed_table)type_in)));
3166 }
3167#endif
3168 }
3169
3172 tx_isolation < ISO_REPEATABLE_READ)
3173 unsafe = true;
3176 tx_isolation < ISO_REPEATABLE_READ)
3177 unsafe = true;
3178
3179 return (unsafe);
3180 }
3181
3182 /**
3183 true if the parsed tree contains references to stored procedures
3184 or functions, false otherwise
3186 bool uses_stored_routines() const { return sroutines_list.elements != 0; }
3188 void set_using_match() { using_match = true; }
3189 bool get_using_match() { return using_match; }
3190
3192 bool is_stmt_unsafe_with_mixed_mode() const {
3194 }
3195
3196 private:
3197 /**
3198 Enumeration listing special types of statements.
3199
3200 Currently, the only possible type is ROW_INJECTION.
3201 */
3203 /**
3204 The statement is a row injection (i.e., either a BINLOG
3205 statement or a row event executed by the slave SQL thread).
3206 */
3208
3209 /** The last element of this enumeration type. */
3211 };
3212
3213 /**
3214 Bit field indicating the type of statement.
3215
3216 There are two groups of bits:
3217
3218 - The low BINLOG_STMT_UNSAFE_COUNT bits indicate the types of
3219 unsafeness that the current statement has.
3220
3221 - The next BINLOG_STMT_TYPE_COUNT bits indicate if the statement
3222 is of some special type.
3223
3224 This must be a member of LEX, not of THD: each stored procedure
3225 needs to remember its unsafeness state between calls and each
3226 stored procedure has its own LEX object (but no own THD object).
3227 */
3229
3230 /**
3231 Bit field that determines the type of tables that are about to be
3232 be accessed while executing a statement.
3233 */
3236 /**
3237 It will be set true if 'MATCH () AGAINST' is used in the statement.
3238 */
3239 bool using_match;
3240
3241 /**
3242 This flag is set to true if statement is unsafe to be binlogged in STATEMENT
3243 format, when in MIXED mode.
3244 Currently this flag is set to true if stored program used in statement has
3245 CREATE/DROP temporary table operation(s) as sub-statement(s).
3246 */
3247 bool stmt_unsafe_with_mixed_mode{false};
3248};
3249
3250/*
3251 st_parsing_options contains the flags for constructions that are
3252 allowed in the current statement.
3254
3256 bool allows_variable;
3257 bool allows_select_into;
3258
3259 st_parsing_options() { reset(); }
3260 void reset();
3261};
3263/**
3264 The state of the lexical parser, when parsing comments.
3265*/
3267 /**
3268 Not parsing comments.
3269 */
3270 NO_COMMENT,
3271
3272 /**
3273 Parsing comments that need to be preserved.
3274 (Copy '/' '*' and '*' '/' sequences to the preprocessed buffer.)
3275 Typically, these are user comments '/' '*' ... '*' '/'.
3276 */
3278
3279 /**
3280 Parsing comments that need to be discarded.
3281 (Don't copy '/' '*' '!' and '*' '/' sequences to the preprocessed buffer.)
3282 Typically, these are special comments '/' '*' '!' ... '*' '/',
3283 or '/' '*' '!' 'M' 'M' 'm' 'm' 'm' ... '*' '/', where the comment
3284 markers should not be expanded.
3285 */
3287};
3288
3289/**
3290 This class represents the character input stream consumed during lexical
3291 analysis.
3292
3293 In addition to consuming the input stream, this class performs some comment
3294 pre processing, by filtering out out-of-bound special text from the query
3295 input stream.
3296
3297 Two buffers, with pointers inside each, are maintained in parallel. The
3298 'raw' buffer is the original query text, which may contain out-of-bound
3299 comments. The 'cpp' (for comments pre processor) is the pre-processed buffer
3300 that contains only the query text that should be seen once out-of-bound data
3301 is removed.
3302*/
3303
3304class Lex_input_stream {
3305 public:
3306 /**
3307 Constructor
3309 @param grammar_selector_token_arg See grammar_selector_token.
3310 */
3311
3312 explicit Lex_input_stream(uint grammar_selector_token_arg)
3313 : grammar_selector_token(grammar_selector_token_arg) {}
3314
3315 /**
3316 Object initializer. Must be called before usage.
3318 @retval false OK
3319 @retval true Error
3320 */
3321 bool init(THD *thd, const char *buff, size_t length);
3322
3323 void reset(const char *buff, size_t length);
3324
3325 /**
3326 Set the echo mode.
3327
3328 When echo is true, characters parsed from the raw input stream are
3329 preserved. When false, characters parsed are silently ignored.
3330 @param echo the echo mode.
3331 */
3332 void set_echo(bool echo) { m_echo = echo; }
3333
3334 void save_in_comment_state() {
3337 }
3338
3342 }
3343
3344 /**
3345 Skip binary from the input stream.
3346 @param n number of bytes to accept.
3347 */
3348 void skip_binary(int n) {
3349 assert(m_ptr + n <= m_end_of_query);
3350 if (m_echo) {
3351 memcpy(m_cpp_ptr, m_ptr, n);
3352 m_cpp_ptr += n;
3353 }
3354 m_ptr += n;
3355 }
3356
3357 /**
3358 Get a character, and advance in the stream.
3359 @return the next character to parse.
3360 */
3361 unsigned char yyGet() {
3362 assert(m_ptr <= m_end_of_query);
3363 const char c = *m_ptr++;
3364 if (m_echo) *m_cpp_ptr++ = c;
3365 return c;
3366 }
3367
3368 /**
3369 Get the last character accepted.
3370 @return the last character accepted.
3371 */
3372 unsigned char yyGetLast() const { return m_ptr[-1]; }
3374 /**
3375 Look at the next character to parse, but do not accept it.
3376 */
3377 unsigned char yyPeek() const {
3378 assert(m_ptr <= m_end_of_query);
3379 return m_ptr[0];
3380 }
3381
3382 /**
3383 Look ahead at some character to parse.
3384 @param n offset of the character to look up
3385 */
3386 unsigned char yyPeekn(int n) const {
3387 assert(m_ptr + n <= m_end_of_query);
3388 return m_ptr[n];
3389 }
3390
3391 /**
3392 Cancel the effect of the last yyGet() or yySkip().
3393 Note that the echo mode should not change between calls to yyGet / yySkip
3394 and yyUnget. The caller is responsible for ensuring that.
3395 */
3396 void yyUnget() {
3397 m_ptr--;
3398 if (m_echo) m_cpp_ptr--;
3399 }
3401 /**
3402 Accept a character, by advancing the input stream.
3403 */
3404 void yySkip() {
3405 assert(m_ptr <= m_end_of_query);
3406 if (m_echo)
3407 *m_cpp_ptr++ = *m_ptr++;
3408 else
3409 m_ptr++;
3410 }
3411
3412 /**
3413 Accept multiple characters at once.
3414 @param n the number of characters to accept.
3415 */
3416 void yySkipn(int n) {
3417 assert(m_ptr + n <= m_end_of_query);
3418 if (m_echo) {
3419 memcpy(m_cpp_ptr, m_ptr, n);
3420 m_cpp_ptr += n;
3421 }
3422 m_ptr += n;
3423 }
3424
3425 /**
3426 Puts a character back into the stream, canceling
3427 the effect of the last yyGet() or yySkip().
3428 Note that the echo mode should not change between calls
3429 to unput, get, or skip from the stream.
3430 */
3431 char *yyUnput(char ch) {
3432 *--m_ptr = ch;
3433 if (m_echo) m_cpp_ptr--;
3434 return m_ptr;
3435 }
3436
3437 /**
3438 Inject a character into the pre-processed stream.
3439
3440 Note, this function is used to inject a space instead of multi-character
3441 C-comment. Thus there is no boundary checks here (basically, we replace
3442 N-chars by 1-char here).
3443 */
3444 char *cpp_inject(char ch) {
3445 *m_cpp_ptr = ch;
3446 return ++m_cpp_ptr;
3447 }
3448
3449 /**
3450 End of file indicator for the query text to parse.
3451 @return true if there are no more characters to parse
3452 */
3453 bool eof() const { return (m_ptr >= m_end_of_query); }
3454
3455 /**
3456 End of file indicator for the query text to parse.
3457 @param n number of characters expected
3458 @return true if there are less than n characters to parse
3460 bool eof(int n) const { return ((m_ptr + n) >= m_end_of_query); }
3461
3462 /** Get the raw query buffer. */
3463 const char *get_buf() const { return m_buf; }
3464
3465 /** Get the pre-processed query buffer. */
3466 const char *get_cpp_buf() const { return m_cpp_buf; }
3467
3468 /** Get the end of the raw query buffer. */
3469 const char *get_end_of_query() const { return m_end_of_query; }
3470
3471 /** Mark the stream position as the start of a new token. */
3472 void start_token() {
3474 m_tok_end = m_ptr;
3475
3478 }
3479
3480 /**
3481 Adjust the starting position of the current token.
3482 This is used to compensate for starting whitespace.
3483 */
3484 void restart_token() {
3487 }
3488
3489 /** Get the token start position, in the raw buffer. */
3490 const char *get_tok_start() const { return m_tok_start; }
3491
3492 /** Get the token start position, in the pre-processed buffer. */
3493 const char *get_cpp_tok_start() const { return m_cpp_tok_start; }
3494
3495 /** Get the token end position, in the raw buffer. */
3496 const char *get_tok_end() const { return m_tok_end; }
3497
3498 /** Get the token end position, in the pre-processed buffer. */
3499 const char *get_cpp_tok_end() const { return m_cpp_tok_end; }
3500
3501 /** Get the current stream pointer, in the raw buffer. */
3502 const char *get_ptr() const { return m_ptr; }
3503
3504 /** Get the current stream pointer, in the pre-processed buffer. */
3505 const char *get_cpp_ptr() const { return m_cpp_ptr; }
3506
3507 /** Get the length of the current token, in the raw buffer. */
3508 uint yyLength() const {
3509 /*
3510 The assumption is that the lexical analyser is always 1 character ahead,
3511 which the -1 account for.
3512 */
3513 assert(m_ptr > m_tok_start);
3514 return (uint)((m_ptr - m_tok_start) - 1);
3515 }
3516
3517 /** Get the utf8-body string. */
3518 const char *get_body_utf8_str() const { return m_body_utf8; }
3519
3520 /** Get the utf8-body length. */
3525 void body_utf8_start(THD *thd, const char *begin_ptr);
3526 void body_utf8_append(const char *ptr);
3527 void body_utf8_append(const char *ptr, const char *end_ptr);
3529 const CHARSET_INFO *txt_cs,
3530 const char *end_ptr);
3531
3532 uint get_lineno(const char *raw_ptr) const;
3533
3534 /** Current thread. */
3535 THD *m_thd;
3536
3537 /** Current line number. */
3538 uint yylineno;
3539
3540 /** Length of the last token parsed. */
3541 uint yytoklen;
3542
3543 /** Interface with bison, value of the last token parsed. */
3545
3546 /**
3547 LALR(2) resolution, look ahead token.
3548 Value of the next token to return, if any,
3549 or -1, if no token was parsed in advance.
3550 Note: 0 is a legal token, and represents YYEOF.
3551 */
3552 int lookahead_token;
3553
3554 /** LALR(2) resolution, value of the look ahead token.*/
3556
3557 /// Skip adding of the current token's digest since it is already added
3558 ///
3559 /// Usually we calculate a digest token by token at the top-level function
3560 /// of the lexer: MYSQLlex(). However, some complex ("hintable") tokens break
3561 /// that data flow: for example, the `SELECT /*+ HINT(t) */` is the single
3562 /// token from the main parser's point of view, and we add the "SELECT"
3563 /// keyword to the digest buffer right after the lex_one_token() call,
3564 /// but the "/*+ HINT(t) */" is a sequence of separate tokens from the hint
3565 /// parser's point of view, and we add those tokens to the digest buffer
3566 /// *inside* the lex_one_token() call. Thus, the usual data flow adds
3567 /// tokens from the "/*+ HINT(t) */" string first, and only than it appends
3568 /// the "SELECT" keyword token to that stream: "/*+ HINT(t) */ SELECT".
3569 /// This is not acceptable, since we use the digest buffer to restore
3570 /// query strings in their normalized forms, so the order of added tokens is
3571 /// important. Thus, we add tokens of "hintable" keywords to a digest buffer
3572 /// right in the hint parser and skip adding of them at the caller with the
3573 /// help of skip_digest flag.
3575
3576 void add_digest_token(uint token, Lexer_yystype *yylval);
3577
3578 void reduce_digest_token(uint token_left, uint token_right);
3579
3580 /**
3581 True if this scanner tokenizes a partial query (partition expression,
3582 generated column expression etc.)
3583
3584 @return true if parsing a partial query, otherwise false.
3585 */
3586 bool is_partial_parser() const { return grammar_selector_token >= 0; }
3587
3588 /**
3589 Outputs warnings on deprecated charsets in complete SQL statements
3591 @param [in] cs The character set/collation to check for a deprecation.
3592 @param [in] alias The name/alias of @p cs.
3593 */
3595 const char *alias) const {
3596 if (!is_partial_parser()) {
3598 }
3599 }
3600
3601 /**
3602 Outputs warnings on deprecated collations in complete SQL statements
3603
3604 @param [in] collation The collation to check for a deprecation.
3605 */
3607 if (!is_partial_parser()) {
3609 }
3610 }
3611
3613
3614 private:
3615 /** Pointer to the current position in the raw input stream. */
3616 char *m_ptr;
3617
3618 /** Starting position of the last token parsed, in the raw buffer. */
3619 const char *m_tok_start;
3620
3621 /** Ending position of the previous token parsed, in the raw buffer. */
3622 const char *m_tok_end;
3623
3624 /** End of the query text in the input stream, in the raw buffer. */
3625 const char *m_end_of_query;
3626
3627 /** Beginning of the query text in the input stream, in the raw buffer. */
3628 const char *m_buf;
3629
3630 /** Length of the raw buffer. */
3631 size_t m_buf_length;
3632
3633 /** Echo the parsed stream to the pre-processed buffer. */
3634 bool m_echo;
3635 bool m_echo_saved;
3636
3637 /** Pre-processed buffer. */
3638 char *m_cpp_buf;
3639
3640 /** Pointer to the current position in the pre-processed input stream. */
3641 char *m_cpp_ptr;
3642
3643 /**
3644 Starting position of the last token parsed,
3645 in the pre-processed buffer.
3646 */
3647 const char *m_cpp_tok_start;
3648
3649 /**
3650 Ending position of the previous token parsed,
3651 in the pre-processed buffer.
3652 */
3653 const char *m_cpp_tok_end;
3654
3655 /** UTF8-body buffer created during parsing. */
3656 char *m_body_utf8;
3657
3658 /** Pointer to the current position in the UTF8-body buffer. */
3659 char *m_body_utf8_ptr;
3660
3661 /**
3662 Position in the pre-processed buffer. The query from m_cpp_buf to
3663 m_cpp_utf_processed_ptr is converted to UTF8-body.
3664 */
3665 const char *m_cpp_utf8_processed_ptr;
3666
3667 public:
3668 /** Current state of the lexical analyser. */
3670
3671 /**
3672 Position of ';' in the stream, to delimit multiple queries.
3673 This delimiter is in the raw buffer.
3674 */
3675 const char *found_semicolon;
3676
3677 /** Token character bitmaps, to detect 7bit strings. */
3679
3680 /** SQL_MODE = IGNORE_SPACE. */
3681 bool ignore_space;
3682
3683 /**
3684 true if we're parsing a prepared statement: in this mode
3685 we should allow placeholders.
3686 */
3687 bool stmt_prepare_mode;
3688 /**
3689 true if we should allow multi-statements.
3690 */
3691 bool multi_statements;
3692
3693 /** State of the lexical analyser for comments. */
3696
3697 /**
3698 Starting position of the TEXT_STRING or IDENT in the pre-processed
3699 buffer.
3700
3701 NOTE: this member must be used within MYSQLlex() function only.
3702 */
3703 const char *m_cpp_text_start;
3704
3705 /**
3706 Ending position of the TEXT_STRING or IDENT in the pre-processed
3707 buffer.
3708
3709 NOTE: this member must be used within MYSQLlex() function only.
3710 */
3711 const char *m_cpp_text_end;
3712
3713 /**
3714 Character set specified by the character-set-introducer.
3715
3716 NOTE: this member must be used within MYSQLlex() function only.
3717 */
3719
3720 /**
3721 Current statement digest instrumentation.
3722 */
3724
3725 /**
3726 The synthetic 1st token to prepend token stream with.
3727
3728 This token value tricks parser to simulate multiple %start-ing points.
3729 Currently the grammar is aware of 4 such synthetic tokens:
3730 1. GRAMMAR_SELECTOR_PART for partitioning stuff from DD,
3731 2. GRAMMAR_SELECTOR_GCOL for generated column stuff from DD,
3732 3. GRAMMAR_SELECTOR_EXPR for generic single expressions from DD/.frm.
3733 4. GRAMMAR_SELECTOR_CTE for generic subquery expressions from CTEs.
3734 5. -1 when parsing with the main grammar (no grammar selector available).
3735
3736 @note yylex() is expected to return the value of type int:
3737 0 is for EOF and everything else for real token numbers.
3738 Bison, in its turn, generates positive token numbers.
3739 So, the negative grammar_selector_token means "not a token".
3740 In other words, -1 is "empty value".
3741 */
3742 const int grammar_selector_token;
3744 bool text_string_is_7bit() const { return !(tok_bitmap & 0x80); }
3748 public:
3749 String column;
3751 LEX_COLUMN(const String &x, const Access_bitmask &y) : column(x), rights(y) {}
3752};
3753
3754enum class role_enum;
3756/*
3757 This structure holds information about grantor's context
3758*/
3759class LEX_GRANT_AS {
3760 public:
3762 void cleanup();
3764 public:
3765 bool grant_as_used;
3767 LEX_USER *user;
3769};
3770
3771/*
3772 Some queries can be executed only using the secondary engine. The enum
3773 "execute_only_in_secondary_reasons" retains the explanations for queries that
3774 cannot be executed using the primary engine.
3775*/
3778 CUBE,
3780};
3781
3783 Some queries can be executed only in using the hypergraph optimizer. The enum
3784 "execute_only_in_hypergraph_reasons" retains the explanations for the same.
3789};
3790
3791/**
3792 The LEX object currently serves three different purposes:
3793
3794 - It contains some universal properties of an SQL command, such as
3795 sql_command, presence of IGNORE in data change statement syntax, and list
3796 of tables (query_tables).
3797
3798 - It contains some execution state variables, like m_exec_started
3799 (set to true when execution is started), plugins (list of plugins used
3800 by statement), insert_update_values_map (a map of objects used by certain
3801 INSERT statements), etc.
3802
3803 - It contains a number of members that should be local to subclasses of
3804 Sql_cmd, like purge_value_list (for the PURGE command), kill_value_list
3805 (for the KILL command).
3806
3807 The LEX object is strictly a part of class Sql_cmd, for those SQL commands
3808 that are represented by an Sql_cmd class. For the remaining SQL commands,
3809 it is a standalone object linked to the current THD.
3810
3811 The lifecycle of a LEX object is as follows:
3812
3813 - The LEX object is constructed either on the execution mem_root
3814 (for regular statements), on a Prepared_statement mem_root (for
3815 prepared statements), on an SP mem_root (for stored procedure instructions),
3816 or created on the current mem_root for short-lived uses.
3817
3818 - Call lex_start() to initialize a LEX object before use.
3819 This initializes the execution state part of the object.
3820 It also calls LEX::reset() to ensure that all members are properly inited.
3821
3822 - Parse and resolve the statement, using the LEX as a work area.
3823
3824 - Execute an SQL command: call set_exec_started() when starting to execute
3825 (actually when starting to optimize).
3826 Typically call is_exec_started() to distinguish between preparation
3827 and optimization/execution stages of SQL command execution.
3828
3829 - Call clear_execution() when execution is finished. This will clear all
3830 execution state associated with the SQL command, it also includes calling
3831 LEX::reset_exec_started().
3832
3833 @todo - Create subclasses of Sql_cmd to contain data that are local
3834 to specific commands.
3835
3836 @todo - Create a Statement context object that will hold the execution state
3837 part of struct LEX.
3838
3839 @todo - Ensure that a LEX struct is never reused, thus making e.g
3840 LEX::reset() redundant.
3841*/
3843struct LEX : public Query_tables_list {
3844 friend bool lex_start(THD *thd);
3846 Query_expression *unit; ///< Outer-most query expression
3847 /// @todo: query_block can be replaced with unit->first-select()
3848 Query_block *query_block; ///< First query block
3849 Query_block *all_query_blocks_list; ///< List of all query blocks
3850 private:
3851 /* current Query_block in parsing */
3853
3855 Some queries can only be executed on a secondary engine, for example,
3856 queries with non-primitive grouping like CUBE.
3857 */
3859
3862
3864 Some queries can only be executed in hypergraph optimizer, for example,
3865 queries with QUALIFY clause.
3866 */
3870
3871 public:
3872 inline Query_block *current_query_block() const {
3873 return m_current_query_block;
3874 }
3875
3876 /*
3877 We want to keep current_thd out of header files, so the debug assert
3878 is moved to the .cc file.
3879 */
3881 inline void set_current_query_block(Query_block *select) {
3882#ifndef NDEBUG
3884#endif
3886 }
3887 /// @return true if this is an EXPLAIN statement
3888 bool is_explain() const { return explain_format != nullptr; }
3889 bool is_explain_analyze = false;
3890
3891 /**
3892 Whether the currently-running statement should be prepared and executed
3893 with the hypergraph optimizer. This will not change after the statement is
3894 prepared, so you can use it in any optimization phase to e.g. figure out
3895 whether to inhibit some transformation that the hypergraph optimizer
3896 does not properly understand yet. If a different optimizer is requested,
3897 the statement must be re-prepared with the proper optimizer settings.
3898 */
3901 }
3902
3903 void set_using_hypergraph_optimizer(bool use_hypergraph) {
3905 }
3906
3907 private:
3910 public:
3913 char *to_log; /* For PURGE BINARY LOGS TO */
3915 // Widcard from SHOW ... LIKE <wildcard> statements.
3919 nullptr, 0}; ///< Argument of the BINLOG event statement.
3926 THD *thd;
3927
3928 /* Optimizer hints */
3931 /* maintain a list of used plugins for this LEX */
3936 /// Table being inserted into (may be a view)
3938 /// Leaf table being inserted into (always a base table)
3940
3941 /** SELECT of CREATE VIEW statement */
3943
3944 /* Partition info structure filled in by PARTITION BY parse part */
3946
3948 The definer of the object being created (view, trigger, stored routine).
3949 I.e. the value of DEFINER clause.
3959
3960 // PURGE statement-specific fields:
3962
3963 // KILL statement-specific fields:
3965
3966 // other stuff:
3968 List<Item_func_set_user_var> set_var_list; // in-query assignment list
3969 /**
3970 List of placeholders ('?') for parameters of a prepared statement. Because
3971 we append to this list during parsing, it is naturally sorted by
3972 position of the '?' in the query string. The code which fills placeholders
3973 with user-supplied values, and the code which writes a query for
3974 statement-based logging, rely on this order.
3975 This list contains only real placeholders, not the clones which originate
3976 in a re-parsed CTE definition.
3977 */
3979
3981
3982 void insert_values_map(Item_field *f1, Field *f2) {
3984 insert_update_values_map = new std::map<Item_field *, Field *>;
3985 insert_update_values_map->insert(std::make_pair(f1, f2));
3986 }
3987 void destroy_values_map() {
3989 insert_update_values_map->clear();
3991 insert_update_values_map = nullptr;
3992 }
3993 }
3994 void clear_values_map() {
3997 }
3998 }
3999 bool has_values_map() const { return insert_update_values_map != nullptr; }
4000 std::map<Item_field *, Field *>::iterator begin_values_map() {
4001 return insert_update_values_map->begin();
4003 std::map<Item_field *, Field *>::iterator end_values_map() {
4004 return insert_update_values_map->end();
4008 }
4010 const bool execute_only_in_secondary_engine_param,
4013 execute_only_in_secondary_engine_param;
4016 reason == SUPPORTED_IN_PRIMARY);
4017 }
4018
4020 const {
4022 }
4023
4027 case CUBE:
4028 return "CUBE";
4029 case TABLESAMPLE:
4030 return "TABLESAMPLE";
4031 default:
4032 return "UNDEFINED";
4033 }
4037 }
4039 bool execute_in_hypergraph_optimizer_param,
4042 execute_in_hypergraph_optimizer_param;
4044 }
4045
4049 ? "QUALIFY clause"
4050 : "UNDEFINED";
4051 }
4052
4054 const {
4056 }
4057
4058 private:
4059 /*
4060 With Visual Studio, an std::map will always allocate two small objects
4061 on the heap. Sometimes we put LEX objects in a MEM_ROOT, and never run
4062 the LEX DTOR. To avoid memory leaks, put this std::map on the heap,
4063 and call clear_values_map() at the end of each statement.
4064 */
4065 std::map<Item_field *, Field *> *insert_update_values_map;
4066
4067 public:
4068 /*
4069 A stack of name resolution contexts for the query. This stack is used
4070 at parse time to set local name resolution contexts for various parts
4071 of a query. For example, in a JOIN ... ON (some_condition) clause the
4072 Items in 'some_condition' must be resolved only against the operands
4073 of the the join, and not against the whole clause. Similarly, Items in
4074 subqueries should be resolved against the subqueries (and outer queries).
4075 The stack is used in the following way: when the parser detects that
4076 all Items in some clause need a local context, it creates a new context
4077 and pushes it on the stack. All newly created Items always store the
4078 top-most context in the stack. Once the parser leaves the clause that
4079 required a local context, the parser pops the top-most context.
4085 HA_CHECK_OPT check_opt; // check/repair options
4088 LEX_SOURCE_INFO mi; // used by CHANGE REPLICATION SOURCE
4093 ulong type;
4094 /**
4095 This field is used as a work field during resolving to validate
4096 the use of aggregate functions. For example in a query
4097 SELECT ... FROM ...WHERE MIN(i) == 1 GROUP BY ... HAVING MIN(i) > 2
4098 MIN(i) in the WHERE clause is not allowed since only non-aggregated data
4099 is present, whereas MIN(i) in the HAVING clause is allowed because HAVING
4100 operates on the output of a grouping operation.
4101 Each query block is assigned a nesting level. This field is a bit field
4102 that contains the value one in the position of that nesting level if
4103 aggregate functions are allowed for that query block.
4104 */
4106 /**
4107 Windowing functions are not allowed in HAVING - in contrast to grouped
4108 aggregate functions, since windowing in SQL logically follows after all
4109 grouping operations. Nor are they allowed inside grouped aggregate
4110 function arguments. One bit per query block, as also \c allow_sum_func. For
4111 ORDER BY and QUALIFY predicates, window functions \em are allowed unless
4112 they are contained in arguments of a grouped aggregate function. Nor are
4113 references to outer window functions (via alias) allowed in subqueries, but
4114 that is checked separately.
4115 */
4118 /// If true: during prepare, we did a subquery transformation (IN-to-EXISTS,
4119 /// SOME/ANY) that doesn't currently work for subquery to a derived table
4120 /// transformation.
4122
4124
4125 /*
4126 Usually `expr` rule of yacc is quite reused but some commands better
4127 not support subqueries which comes standard with this rule, like
4128 KILL, HA_READ, CREATE/ALTER EVENT etc. Set this to `false` to get
4129 syntax error back.
4130 */
4131 bool expr_allows_subquery{true};
4132 /**
4133 If currently re-parsing a CTE's definition, this is the offset in bytes
4134 of that definition in the original statement which had the WITH
4135 clause. Otherwise this is 0.
4136 */
4138 /**
4139 If currently re-parsing a condition which is pushed down to a derived
4140 table, this will be set to true.
4141 */
4143 /**
4144 If currently re-parsing a condition that is being pushed down to a
4145 derived table, this has the positions of all the parameters that are
4146 part of that condition in the original statement. Otherwise it is empty.
4150 enum SSL_type ssl_type; /* defined in violite.h */
4156 /// QUERY ID for SHOW PROFILE
4158 uint profile_options;
4159 uint grant, grant_tot_col;
4160 /**
4161 Set to true when GRANT ... GRANT OPTION ... TO ...
4162 is used (vs. GRANT ... WITH GRANT OPTION).
4163 The flag is used by @ref mysql_grant to grant GRANT OPTION (@ref GRANT_ACL)
4164 to all dynamic privileges.
4168 int select_number; ///< Number of query block (by EXPLAIN)
4171 /**
4172 @todo ensure that correct CONTEXT_ANALYSIS_ONLY is set for all preparation
4173 code, so we can fully rely on this field.
4174 */
4176 bool drop_if_exists;
4177 /**
4178 refers to optional IF EXISTS clause in REVOKE sql. This flag when set to
4179 true will report warnings in case privilege being granted is not granted to
4180 given user/role. When set to false error is reported.
4181 */
4182 bool grant_if_exists;
4183 /**
4184 refers to optional IGNORE UNKNOWN USER clause in REVOKE sql. This flag when
4185 set to true will report warnings in case target user/role for which
4186 privilege being granted does not exists. When set to false error is
4187 reported.
4191 bool autocommit;
4193 // For show commands to show hidden columns and indexes.
4194 bool m_extended_show;
4195
4196 enum enum_yes_no_unknown tx_chain, tx_release;
4197
4198 /**
4199 Whether this query will return the same answer every time, given unchanged
4200 data. Used to be for the query cache, but is now used to find out if an
4201 expression is usable for partitioning.
4202 */
4205 private:
4206 /// True if statement references UDF functions
4207 bool m_has_udf{false};
4210 public:
4211 bool is_ignore() const { return ignore; }
4212 void set_ignore(bool ignore_param) { ignore = ignore_param; }
4213 void set_has_udf() { m_has_udf = true; }
4214 bool has_udf() const { return m_has_udf; }
4217 /* Prepared statements SQL syntax:*/
4218 LEX_CSTRING prepared_stmt_name; /* Statement name (in all queries) */
4220 Prepared statement query text or name of variable that holds the
4221 prepared statement (in PREPARE ... queries)
4222 */
4224 /* If true, prepared_stmt_code is a name of variable that holds the query */
4226 /* Names of user variables holding parameters (in EXECUTE) */
4230 bool sp_lex_in_use; /* Keep track on lex usage in SPs for error handling */
4231 bool all_privileges;
4235
4236 private:
4237 bool m_broken; ///< see mark_broken()
4238 /**
4239 Set to true when execution has started (after parsing, tables opened and
4240 query preparation is complete. Used to track arena state for SPs).
4241 */
4242 bool m_exec_started;
4243 /**
4244 Set to true when execution is completed, ie optimization has been done
4245 and execution is successful or ended in error.
4246 */
4247 bool m_exec_completed;
4248 /**
4249 Current SP parsing context.
4250 @see also sp_head::m_root_parsing_ctx.
4251 */
4254 /**
4255 Statement context for Query_block::make_active_options.
4256 */
4258
4259 public:
4260 /**
4261 Gets the options that have been set for this statement. The options are
4262 propagated to the Query_block objects and should usually be read with
4263 #Query_block::active_options().
4264
4265 @return a bit set of options set for this statement
4266 */
4268 /**
4269 Add options to values of m_statement_options. options is an ORed
4270 bit set of options defined in query_options.h
4272 @param options Add this set of options to the set already in
4273 m_statement_options
4277 }
4278 bool is_broken() const { return m_broken; }
4279 /**
4280 Certain permanent transformations (like in2exists), if they fail, may
4281 leave the LEX in an inconsistent state. They should call the
4282 following function, so that this LEX is not reused by another execution.
4284 @todo If lex_start () were a member function of LEX, the "broken"
4285 argument could always be "true" and thus could be removed.
4286 */
4287 void mark_broken(bool broken = true) {
4288 if (broken) {
4289 /*
4290 "OPEN <cursor>" cannot be re-prepared if the cursor uses no tables
4291 ("SELECT FROM DUAL"). Indeed in that case cursor_query is left empty
4292 in constructions of sp_instr_cpush, and thus
4293 sp_lex_instr::parse_expr() cannot re-prepare. So we mark the statement
4294 as broken only if tables are used.
4295 */
4296 if (is_metadata_used()) m_broken = true;
4297 } else
4298 m_broken = false;
4300
4302
4303 void cleanup(bool full) {
4304 unit->cleanup(full);
4305 if (full) {
4310
4311 bool is_exec_started() const { return m_exec_started; }
4312 void set_exec_started() { m_exec_started = true; }
4313 void reset_exec_started() {
4314 m_exec_started = false;
4315 m_exec_completed = false;
4316 }
4317 /**
4318 Check whether the statement has been executed (regardless of completion -
4319 successful or in error).
4320 Check this instead of Query_expression::is_executed() to determine
4321 the state of a complete statement.
4322 */
4323 bool is_exec_completed() const { return m_exec_completed; }
4324 void set_exec_completed() { m_exec_completed = true; }
4326
4330
4331 /// Check if the current statement uses meta-data (uses a table or a stored
4332 /// routine).
4333 bool is_metadata_used() const {
4334 return query_tables != nullptr || has_udf() ||
4335 (sroutines != nullptr && !sroutines->empty());
4336 }
4337
4338 /// We have detected the presence of an alias of a window function with a
4339 /// window on query block qb. Check if the reference is illegal at this point
4340 /// during resolution.
4341 /// @param qb The query block of the window function
4342 /// @return true if window function is referenced from another query block
4343 /// than its window, or if window functions are disallowed at the current
4344 /// point during prepare, cf. also documentation of \c m_deny_window_func.
4345 bool deny_window_function(Query_block *qb) const {
4346 return qb != current_query_block() ||
4347 ((~allow_sum_func | m_deny_window_func) >>
4349 0x1;
4350 }
4352 public:
4354
4355 bool only_view; /* used for SHOW CREATE TABLE/VIEW */
4356 /*
4357 view created to be run from definer (standard behaviour)
4358 */
4360
4361 /**
4362 Intended to point to the next word after DEFINER-clause in the
4363 following statements:
4364
4365 - CREATE TRIGGER (points to "TRIGGER");
4366 - CREATE PROCEDURE (points to "PROCEDURE");
4367 - CREATE FUNCTION (points to "FUNCTION" or "AGGREGATE");
4368 - CREATE EVENT (points to "EVENT")
4370 This pointer is required to add possibly omitted DEFINER-clause to the
4371 DDL-statement before dumping it to the binlog.
4372 */
4373 const char *stmt_definition_begin;
4374 const char *stmt_definition_end;
4375
4376 /**
4377 During name resolution search only in the table list given by
4378 Name_resolution_context::first_name_resolution_table and
4379 Name_resolution_context::last_name_resolution_table
4380 (see Item_field::fix_fields()).
4381 */
4383
4384 bool is_lex_started; /* If lex_start() did run. For debugging. */
4385 /// Set to true while resolving values in ON DUPLICATE KEY UPDATE clause
4388 class Explain_format *explain_format{nullptr};
4389
4390 // Maximum execution time for a statement.
4391 ulong max_execution_time;
4392
4394 To flag the current statement as dependent for binary logging
4395 on explicit_defaults_for_timestamp
4396 */
4398
4399 /**
4400 Used to inform the parser whether it should contextualize the parse
4401 tree. When we get a pure parser this will not be needed.
4402 */
4403 bool will_contextualize;
4404
4405 LEX();
4407 virtual ~LEX();
4408
4409 /// Destroy contained objects, but not the LEX object itself.
4410 void destroy() {
4411 if (unit == nullptr) return;
4412 unit->destroy();
4413 unit = nullptr;
4414 query_block = nullptr;
4415 all_query_blocks_list = nullptr;
4416 m_current_query_block = nullptr;
4417 explain_format = nullptr;
4419 }
4420
4421 /// Reset query context to initial state
4422 void reset();
4423
4424 /// Create an empty query block within this LEX object.
4426
4427 /// Create query expression object that contains one query block.
4428 Query_block *new_query(Query_block *curr_query_block);
4429
4430 /// Create query block and attach it to the current query expression.
4432
4433 /// Create top-level query expression and query block.
4434 bool new_top_level_query();
4435
4436 /// Create query expression and query block in existing memory objects.
4437 void new_static_query(Query_expression *sel_query_expression,
4438 Query_block *select);
4439
4440 /// Create query expression under current_query_block and a query block under
4441 /// the new query expression. The new query expression is linked in under
4442 /// current_query_block. The new query block is linked in under the new
4443 /// query expression.
4444 ///
4445 /// @param thd current session context
4446 /// @param current_query_block the root under which we create the new
4447 /// expression
4448 /// and block
4449 /// @param where_clause any where clause for the block
4450 /// @param having_clause any having clause for the block
4451 /// @param ctx the parsing context
4452 ///
4453 /// @returns the new query expression, or nullptr on error.
4455 THD *thd, Query_block *current_query_block, Item *where_clause,
4456 Item *having_clause, enum_parsing_context ctx);
4457
4458 inline bool is_ps_or_view_context_analysis() {
4461 }
4462
4463 inline bool is_view_context_analysis() {
4465 }
4466
4467 void clear_execution();
4468
4469 /**
4470 Set the current query as uncacheable.
4471
4472 @param curr_query_block Current select query block
4473 @param cause Why this query is uncacheable.
4474
4475 @details
4476 All query blocks representing subqueries, from the current one up to
4477 the outer-most one, but excluding the main query block, are also set
4478 as uncacheable.
4479 */
4480 void set_uncacheable(Query_block *curr_query_block, uint8 cause) {
4481 safe_to_cache_query = false;
4482
4483 if (m_current_query_block == nullptr) return;
4484 Query_block *sl;
4485 Query_expression *un;
4486 for (sl = curr_query_block, un = sl->master_query_expression(); un != unit;
4487 sl = sl->outer_query_block(), un = sl->master_query_expression()) {
4488 sl->uncacheable |= cause;
4489 un->uncacheable |= cause;
4490 }
4491 }
4493
4494 Table_ref *unlink_first_table(bool *link_to_local);
4495 void link_first_table_back(Table_ref *first, bool link_to_local);
4497
4499
4501 for (Table_ref *tr = insert_table->first_leaf_table(); tr != nullptr;
4502 tr = tr->next_leaf)
4503 tr->restore_properties();
4504 }
4505
4507
4508 bool can_use_merged();
4509 bool can_not_use_merged();
4510 bool need_correct_ident();
4511 /*
4512 Is this update command where 'WHITH CHECK OPTION' clause is important
4513
4514 SYNOPSIS
4515 LEX::which_check_option_applicable()
4516
4517 RETURN
4518 true have to take 'WHITH CHECK OPTION' clause into account
4519 false 'WHITH CHECK OPTION' clause do not need
4520 */
4521 inline bool which_check_option_applicable() {
4522 switch (sql_command) {
4523 case SQLCOM_UPDATE:
4525 case SQLCOM_INSERT:
4527 case SQLCOM_REPLACE:
4529 case SQLCOM_LOAD:
4530 return true;
4531 default:
4532 return false;
4533 }
4535
4537
4539 return context_stack.push_front(context);
4540 }
4541
4542 void pop_context() { context_stack.pop(); }
4543
4544 bool copy_db_to(char const **p_db, size_t *p_db_length) const;
4545
4546 bool copy_db_to(char **p_db, size_t *p_db_length) const {
4547 return copy_db_to(const_cast<const char **>(p_db), p_db_length);
4548 }
4549
4551
4554
4555 bool table_or_sp_used();
4556
4557 /**
4558 @brief check if the statement is a single-level join
4559 @return result of the check
4560 @retval true The statement doesn't contain subqueries, unions and
4561 stored procedure calls.
4562 @retval false There are subqueries, UNIONs or stored procedure calls.
4563 */
4564 bool is_single_level_stmt() {
4565 /*
4566 This check exploits the fact that the last added to all_select_list is
4567 on its top. So query_block (as the first added) will be at the tail
4568 of the list.
4569 */
4571 (sroutines == nullptr || sroutines->empty())) {
4573 return true;
4574 }
4575 return false;
4576 }
4577
4578 void release_plugins();
4579
4580 /**
4581 IS schema queries read some dynamic table statistics from SE.
4582 These statistics are cached, to avoid opening of table more
4583 than once while preparing a single output record buffer.
4584 */
4587
4588 bool accept(Select_lex_visitor *visitor);
4589
4590 bool set_wild(LEX_STRING);
4591 void clear_privileges();
4592
4593 bool make_sql_cmd(Parse_tree_root *parse_tree);
4594
4595 private:
4596 /**
4597 Context object used by secondary storage engines to store query
4598 state during optimization and execution.
4599 */
4601
4602 public:
4603 /**
4604 Gets the secondary engine execution context for this statement.
4605 */
4607 const {
4609 }
4610
4611 /**
4612 Sets the secondary engine execution context for this statement.
4613 The old context object is destroyed, if there is one. Can be set
4614 to nullptr to destroy the old context object and clear the
4615 pointer.
4616
4617 The supplied context object should be allocated on the execution
4618 MEM_ROOT, so that its memory doesn't have to be manually freed
4619 after query execution.
4620 */
4623
4624 /**
4625 Validates if a query can run with the old optimizer.
4626 @return True if the query cannot be run with old optimizer, false otherwise.
4629
4630 private:
4632
4633 public:
4636 }
4637
4640 }
4643
4644 private:
4645 bool rewrite_required{false};
4647 public:
4648 void set_rewrite_required() { rewrite_required = true; }
4649 void reset_rewrite_required() { rewrite_required = false; }
4650 bool is_rewrite_required() { return rewrite_required; }
4651};
4652
4654 RAII class to ease the call of LEX::mark_broken() if error.
4655 Used during preparation and optimization of DML queries.
4656*/
4658 public:
4659 Prepare_error_tracker(THD *thd_arg) : thd(thd_arg) {}
4661
4662 private:
4663 THD *const thd;
4664};
4665
4666/**
4667 The internal state of the syntax parser.
4668 This object is only available during parsing,
4669 and is private to the syntax parser implementation (sql_yacc.yy).
4670*/
4671class Yacc_state {
4672 public:
4674 reset();
4675 }
4676
4677 void reset() {
4678 if (yacc_yyss != nullptr) {
4680 yacc_yyss = nullptr;
4681 }
4682 if (yacc_yyvs != nullptr) {
4684 yacc_yyvs = nullptr;
4685 }
4686 if (yacc_yyls != nullptr) {
4688 yacc_yyls = nullptr;
4689 }
4692 }
4693
4694 ~Yacc_state();
4695
4696 /**
4697 Reset part of the state which needs resetting before parsing
4698 substatement.
4699 */
4703 }
4704
4705 /**
4706 Bison internal state stack, yyss, when dynamically allocated using
4707 my_yyoverflow().
4708 */
4710
4711 /**
4712 Bison internal semantic value stack, yyvs, when dynamically allocated using
4713 my_yyoverflow().
4714 */
4716
4717 /**
4718 Bison internal location value stack, yyls, when dynamically allocated using
4719 my_yyoverflow().
4720 */
4722
4723 /**
4724 Type of lock to be used for tables being added to the statement's
4725 table list in table_factor, table_alias_ref, single_multi and
4726 table_wild_one rules.
4727 Statements which use these rules but require lock type different
4728 from one specified by this member have to override it by using
4729 Query_block::set_lock_for_tables() method.
4730
4731 The default value of this member is TL_READ_DEFAULT. The only two
4732 cases in which we change it are:
4733 - When parsing SELECT HIGH_PRIORITY.
4734 - Rule for DELETE. In which we use this member to pass information
4735 about type of lock from delete to single_multi part of rule.
4736
4737 We should try to avoid introducing new use cases as we would like
4738 to get rid of this member eventually.
4739 */
4741
4742 /**
4743 The type of requested metadata lock for tables added to
4744 the statement table list.
4745 */
4747
4748 /*
4749 TODO: move more attributes from the LEX structure here.
4750 */
4751};
4752
4753/**
4754 Input parameters to the parser.
4755*/
4756struct Parser_input {
4757 /**
4758 True if the text parsed corresponds to an actual query,
4759 and not another text artifact.
4760 This flag is used to disable digest parsing of nested:
4761 - view definitions
4762 - table trigger definitions
4763 - table partition definitions
4764 - event scheduler event definitions
4765 */
4766 bool m_has_digest;
4767 /**
4768 True if the caller needs to compute a digest.
4769 This flag is used to request explicitly a digest computation,
4770 independently of the performance schema configuration.
4771 */
4772 bool m_compute_digest;
4773
4774 Parser_input() : m_has_digest(false), m_compute_digest(false) {}
4775};
4776
4777/**
4778 Internal state of the parser.
4779 The complete state consist of:
4780 - input parameters that control the parser behavior
4781 - state data used during lexical parsing,
4782 - state data used during syntactic parsing.
4783*/
4784class Parser_state {
4785 protected:
4786 /**
4787 Constructor for special parsers of partial SQL clauses (DD)
4788
4789 @param grammar_selector_token See Lex_input_stream::grammar_selector_token
4790 */
4791 explicit Parser_state(int grammar_selector_token)
4792 : m_input(), m_lip(grammar_selector_token), m_yacc(), m_comment(false) {}
4793
4794 public:
4795 Parser_state() : m_input(), m_lip(~0U), m_yacc(), m_comment(false) {}
4796
4797 /**
4798 Object initializer. Must be called before usage.
4800 @retval false OK
4801 @retval true Error
4802 */
4803 bool init(THD *thd, const char *buff, size_t length) {
4804 return m_lip.init(thd, buff, length);
4805 }
4806
4807 void reset(const char *found_semicolon, size_t length) {
4808 m_lip.reset(found_semicolon, length);
4810 }
4812 /// Signal that the current query has a comment
4813 void add_comment() { m_comment = true; }
4814 /// Check whether the current query has a comment
4815 bool has_comment() const { return m_comment; }
4816
4817 public:
4821 /**
4822 Current performance digest instrumentation.
4823 */
4825
4826 private:
4827 bool m_comment; ///< True if current query contains comments
4828};
4830/**
4831 Parser state for partition expression parser (.frm/DD stuff)
4832*/
4834 public:
4836
4838};
4840/**
4841 Parser state for generated column expression parser (.frm/DD stuff)
4842*/
4844 public:
4846
4847