MySQL 9.2.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
73#include "sql/key_spec.h" // KEY_CREATE_INFO
74#include "sql/mdl.h"
75#include "sql/mem_root_array.h" // Mem_root_array
76#include "sql/parse_location.h"
77#include "sql/parse_tree_node_base.h" // enum_parsing_context
78#include "sql/parser_yystype.h"
79#include "sql/query_options.h" // OPTION_NO_CONST_TABLES
80#include "sql/query_term.h"
81#include "sql/set_var.h"
82#include "sql/sql_array.h"
83#include "sql/sql_connect.h" // USER_RESOURCES
84#include "sql/sql_const.h"
85#include "sql/sql_data_change.h" // enum_duplicates
86#include "sql/sql_error.h" // warn_on_deprecated_charset
87#include "sql/sql_list.h"
88#include "sql/sql_plugin_ref.h"
89#include "sql/sql_servers.h" // Server_options
90#include "sql/sql_udf.h" // Item_udftype
91#include "sql/table.h" // Table_ref
92#include "sql/thr_malloc.h"
93#include "sql/trigger_def.h" // enum_trigger_action_time_type
94#include "sql/visible_fields.h"
95#include "sql_string.h"
96#include "string_with_len.h"
97#include "strings/sql_chars.h"
98#include "thr_lock.h" // thr_lock_type
99#include "violite.h" // SSL_type
100
101class Alter_info;
102class Event_parse_data;
103class Field;
104class Item_cond;
106class Item_func_match;
110class Item_sum;
111class JOIN;
112class Opt_hints_global;
113class Opt_hints_qb;
114class PT_subquery;
115class PT_with_clause;
116class Parse_tree_root;
117class Protocol;
118class Query_result;
121class Query_block;
122class Query_expression;
124class Sql_cmd;
125class THD;
126class Value_generator;
127class Window;
128class partition_info;
129class sp_head;
130class sp_name;
131class sp_pcontext;
132struct LEX;
133struct NESTED_JOIN;
134struct PSI_digest_locker;
135struct sql_digest_state;
136union Lexer_yystype;
138
140constexpr const int MAX_SELECT_NESTING{sizeof(nesting_map) * 8 - 1};
141
142/*
143 There are 8 different type of table access so there is no more than
144 combinations 2^8 = 256:
145
146 . STMT_READS_TRANS_TABLE
147
148 . STMT_READS_NON_TRANS_TABLE
149
150 . STMT_READS_TEMP_TRANS_TABLE
151
152 . STMT_READS_TEMP_NON_TRANS_TABLE
153
154 . STMT_WRITES_TRANS_TABLE
155
156 . STMT_WRITES_NON_TRANS_TABLE
157
158 . STMT_WRITES_TEMP_TRANS_TABLE
159
160 . STMT_WRITES_TEMP_NON_TRANS_TABLE
161
162 The unsafe conditions for each combination is represented within a byte
163 and stores the status of the option --binlog-direct-non-trans-updates,
164 whether the trx-cache is empty or not, and whether the isolation level
165 is lower than ISO_REPEATABLE_READ:
166
167 . option (OFF/ON)
168 . trx-cache (empty/not empty)
169 . isolation (>= ISO_REPEATABLE_READ / < ISO_REPEATABLE_READ)
170
171 bits 0 : . OFF, . empty, . >= ISO_REPEATABLE_READ
172 bits 1 : . OFF, . empty, . < ISO_REPEATABLE_READ
173 bits 2 : . OFF, . not empty, . >= ISO_REPEATABLE_READ
174 bits 3 : . OFF, . not empty, . < ISO_REPEATABLE_READ
175 bits 4 : . ON, . empty, . >= ISO_REPEATABLE_READ
176 bits 5 : . ON, . empty, . < ISO_REPEATABLE_READ
177 bits 6 : . ON, . not empty, . >= ISO_REPEATABLE_READ
178 bits 7 : . ON, . not empty, . < ISO_REPEATABLE_READ
179*/
180extern uint binlog_unsafe_map[256];
181/*
182 Initializes the array with unsafe combinations and its respective
183 conditions.
184*/
186
187/*
188 If we encounter a diagnostics statement (GET DIAGNOSTICS, or e.g.
189 the old SHOW WARNINGS|ERRORS, or "diagnostics variables" such as
190 @@warning_count | @@error_count, we'll set some hints so this
191 information is not lost. DA_KEEP_UNSPECIFIED is used in LEX constructor to
192 avoid leaving variables uninitialized.
193 */
195 DA_KEEP_NOTHING = 0, /**< keep nothing */
196 DA_KEEP_DIAGNOSTICS, /**< keep the diagnostics area */
197 DA_KEEP_COUNTS, /**< keep \@warning_count / \@error_count */
198 DA_KEEP_PARSE_ERROR, /**< keep diagnostics area after parse error */
199 DA_KEEP_UNSPECIFIED /**< keep semantics is unspecified */
201
207
215
216/**
217 enum_sp_type defines type codes of stored programs.
218
219 @note these codes are used when dealing with the mysql.routines system table,
220 so they must not be changed.
221
222 @note the following macros were used previously for the same purpose. Now they
223 are used for ACL only.
224*/
225enum class enum_sp_type {
226 FUNCTION = 1,
227 PROCEDURE,
228 TRIGGER,
229 EVENT,
230 LIBRARY,
231 /*
232 Must always be the last one.
233 Denotes an error condition.
234 */
236};
237
239 if (val >= static_cast<longlong>(enum_sp_type::FUNCTION) &&
240 val < static_cast<longlong>(enum_sp_type::INVALID_SP_TYPE))
241 return static_cast<enum_sp_type>(val);
242 else
244}
245
247 return static_cast<longlong>(val);
248}
249
250inline uint to_uint(enum_sp_type val) { return static_cast<uint>(val); }
251
252/*
253 Values for the type enum. This reflects the order of the enum declaration
254 in the CREATE TABLE command. These values are used to enumerate object types
255 for the ACL statements.
256
257 These values were also used for enumerating stored program types. However, now
258 enum_sp_type should be used for that instead of them.
259*/
260#define TYPE_ENUM_FUNCTION 1
261#define TYPE_ENUM_PROCEDURE 2
262#define TYPE_ENUM_TRIGGER 3
263#define TYPE_ENUM_PROXY 4
264#define TYPE_ENUM_LIBRARY 5
265#define TYPE_ENUM_INVALID 6
266
267enum class Acl_type {
268 TABLE = 0,
273};
274
275Acl_type lex_type_to_acl_type(ulong lex_type);
276
278
280
282 {STRING_WITH_LEN("")},
283 {STRING_WITH_LEN("CONTAINS SQL")},
284 {STRING_WITH_LEN("NO SQL")},
285 {STRING_WITH_LEN("READS SQL DATA")},
286 {STRING_WITH_LEN("MODIFIES SQL DATA")}};
287
289 VIEW_CREATE_NEW, // check that there are not such VIEW/table
290 VIEW_ALTER, // check that VIEW with such name exists
291 VIEW_CREATE_OR_REPLACE // check only that there are not such table
292};
293
295 ALTER_USER_COMMENT_NOT_USED, // No user metadata ALTER in the AST
296 ALTER_USER_COMMENT, // A text comment is expected
297 ALTER_USER_ATTRIBUTE // A JSON object is expected
298};
299
300/* Options to add_table_to_list() */
301#define TL_OPTION_UPDATING 0x01
302#define TL_OPTION_IGNORE_LEAVES 0x02
303#define TL_OPTION_ALIAS 0x04
304
305/* Structure for db & table in sql_yacc */
306class Table_function;
307
309 public:
314
315 Table_ident(Protocol *protocol, const LEX_CSTRING &db_arg,
316 const LEX_CSTRING &table_arg, bool force);
317 Table_ident(const LEX_CSTRING &db_arg, const LEX_CSTRING &table_arg)
318 : db(db_arg), table(table_arg), sel(nullptr), table_function(nullptr) {}
319 Table_ident(const LEX_CSTRING &table_arg)
320 : table(table_arg), sel(nullptr), table_function(nullptr) {
321 db = NULL_CSTR;
322 }
323 /**
324 This constructor is used only for the case when we create a derived
325 table. A derived table has no name and doesn't belong to any database.
326 Later, if there was an alias specified for the table, it will be set
327 by add_table_to_list.
328 */
330 db = EMPTY_CSTR; /* a subject to casedn_str */
332 }
333 /*
334 This constructor is used only for the case when we create a table function.
335 It has no name and doesn't belong to any database as it exists only
336 during query execution. Later, if there was an alias specified for the
337 table, it will be set by add_table_to_list.
338 */
339 Table_ident(LEX_CSTRING &table_arg, Table_function *table_func_arg)
340 : table(table_arg), sel(nullptr), table_function(table_func_arg) {
341 /* We must have a table name here as this is used with add_table_to_list */
342 db = EMPTY_CSTR; /* a subject to casedn_str */
343 }
344 // True if we can tell from syntax that this is a table function.
345 bool is_table_function() const { return (table_function != nullptr); }
346 // True if we can tell from syntax that this is an unnamed derived table.
347 bool is_derived_table() const { return sel; }
348 void change_db(const char *db_name) {
349 db.str = db_name;
350 db.length = strlen(db_name);
351 }
352};
353
356
357/**
358 Structure to hold parameters for CHANGE REPLICATION SOURCE, START REPLICA, and
359 STOP REPLICA.
360
361 Remark: this should not be confused with Master_info (and perhaps
362 would better be renamed to st_lex_replication_info). Some fields,
363 e.g., delay, are saved in Relay_log_info, not in Master_info.
364*/
366 /*
367 The array of IGNORE_SERVER_IDS has a preallocation, and is not expected
368 to grow to any significant size, so no instrumentation.
369 */
371 initialize();
372 }
379 char *gtid;
380 char *view_id;
381 const char *channel; // identifier similar to database name
382 enum {
389
390 /*
391 Enum is used for making it possible to detect if the user
392 changed variable or if it should be left at old value
393 */
394 enum {
404 /*
405 Ciphersuites used for TLS 1.3 communication with the master server.
406 */
411 };
420 /**
421 Flag that is set to `true` whenever `PRIVILEGE_CHECKS_USER` is set to `NULL`
422 as a part of a `CHANGE REPLICATION SOURCE TO` statement.
423 */
425 /**
426 Username and hostname parts of the `PRIVILEGE_CHECKS_USER`, when it's set to
427 a user.
428 */
430 /**
431 Flag indicating if row format should be enforced for this channel event
432 stream.
433 */
435
436 /**
437 Identifies what is the slave policy on primary keys in tables.
438 If set to STREAM it just replicates the value of sql_require_primary_key.
439 If set to ON it fails when the source tries to replicate a table creation
440 or alter operation that does not have a primary key.
441 If set to OFF it does not enforce any policies on the channel for primary
442 keys.
443 */
444 enum {
451
452 enum {
458
460
461 /// Initializes everything to zero/NULL/empty.
462 void initialize();
463 /// Sets all fields to their "unspecified" value.
464 void set_unspecified();
465
466 private:
467 // Not copyable or assignable.
470};
471
473 bool all;
474};
475
481
482/*
483 String names used to print a statement with index hints.
484 Keep in sync with index_hint_type.
485*/
486extern const char *index_hint_type_name[];
488
489/*
490 Bits in index_clause_map : one for each possible FOR clause in
491 USE/FORCE/IGNORE INDEX index hint specification
492*/
493#define INDEX_HINT_MASK_JOIN (1)
494#define INDEX_HINT_MASK_GROUP (1 << 1)
495#define INDEX_HINT_MASK_ORDER (1 << 2)
496
497#define INDEX_HINT_MASK_ALL \
498 (INDEX_HINT_MASK_JOIN | INDEX_HINT_MASK_GROUP | INDEX_HINT_MASK_ORDER)
499
500/* Single element of an USE/FORCE/IGNORE INDEX list specified as a SQL hint */
502 public:
503 /* The type of the hint : USE/FORCE/IGNORE */
505 /* Where the hit applies to. A bitmask of INDEX_HINT_MASK_<place> values */
507 /*
508 The index name. Empty (str=NULL) name represents an empty list
509 USE INDEX () clause
510 */
512
513 Index_hint(const char *str, uint length) {
514 key_name.str = str;
516 }
517
518 void print(const THD *thd, String *str);
519};
520
521/*
522 Class Query_expression represents a query expression.
523 Class Query_block represents a query block.
524
525 In addition to what is explained below, the query block(s) of a query
526 expression is contained in a tree expressing the nesting of set operations,
527 cf. query_term.h
528
529 A query expression contains one or more query blocks (more than one means
530 that the query expression contains one or more set operations - UNION,
531 INTERSECT or EXCEPT - unless the query blocks are used to describe
532 subqueries). These classes are connected as follows: both classes have a
533 master, a slave, a next and a prev field. For class Query_block, master and
534 slave connect to objects of type Query_expression, whereas for class
535 Query_expression, they connect to Query_block. master is pointer to outer
536 node. slave is pointer to the first inner node.
537
538 neighbors are two Query_block or Query_expression objects on
539 the same level.
540
541 The structures are linked with the following pointers:
542 - list of neighbors (next/prev) (prev of first element point to slave
543 pointer of outer structure)
544 - For Query_block, this is a list of query blocks.
545 - For Query_expression, this is a list of subqueries.
546
547 - pointer to outer node (master), which is
548 If this is Query_expression
549 - pointer to outer query_block.
550 If this is Query_block
551 - pointer to outer Query_expression.
552
553 - pointer to inner objects (slave), which is either:
554 If this is an Query_expression:
555 - first query block that belong to this query expression.
556 If this is an Query_block
557 - first query expression that belong to this query block (subqueries).
558
559 - list of all Query_block objects (link_next/link_prev)
560 This is to be used for things like derived tables creation, where we
561 go through this list and create the derived tables.
562
563 In addition to the above mentioned link, the query's tree structure is
564 represented by the member m_query_term, see query_term.h
565 For example for following query:
566
567 select *
568 from table1
569 where table1.field IN (select * from table1_1_1 union
570 select * from table1_1_2)
571 union
572 select *
573 from table2
574 where table2.field=(select (select f1 from table2_1_1_1_1
575 where table2_1_1_1_1.f2=table2_1_1.f3)
576 from table2_1_1
577 where table2_1_1.f1=table2.f2)
578 union
579 select * from table3;
580
581 we will have following structure:
582
583 select1: (select * from table1 ...)
584 select2: (select * from table2 ...)
585 select3: (select * from table3)
586 select1.1.1: (select * from table1_1_1)
587 ...
588
589 main unit
590 select1 select2 select3
591 |^^ |^
592 s||| ||master
593 l||| |+---------------------------------+
594 a||| +---------------------------------+|
595 v|||master slave ||
596 e||+-------------------------+ ||
597 V| neighbor | V|
598 unit1.1<+==================>unit1.2 unit2.1
599 select1.1.1 select 1.1.2 select1.2.1 select2.1.1
600 |^
601 ||
602 V|
603 unit2.1.1.1
604 select2.1.1.1.1
605
606
607 relation in main unit will be following:
608 (bigger picture for:
609 main unit
610 select1 select2 select3
611 in the above picture)
612
613 main unit
614 |^^^
615 ||||
616 ||||
617 |||+------------------------------+
618 ||+--------------+ |
619 slave||master | |
620 V| neighbor | neighbor |
621 select1<========>select2<========>select3
622
623 list of all query_block will be following (as it will be constructed by
624 parser):
625
626 select1->select2->select3->select2.1.1->select 2.1.2->select2.1.1.1.1-+
627 |
628 +---------------------------------------------------------------------+
629 |
630 +->select1.1.1->select1.1.2
631
632*/
633
634/**
635 This class represents a query expression (one query block or
636 several query blocks combined with UNION).
637*/
639 /**
640 Intrusive double-linked list of all query expressions
641 immediately contained within the same query block.
642 */
645
646 /**
647 The query block wherein this query expression is contained,
648 NULL if the query block is the outer-most one.
649 */
651 /// The first query block in this query expression.
653
654 // The query set operation structure, see doc for Query_term.
656
657 public:
658 /// Getter for m_query_term, q.v.
659 Query_term *query_term() const { return m_query_term; }
660 /// Setter for m_query_term, q.v.
662 /// Convenience method to avoid down casting, i.e. interpret m_query_term
663 /// as a Query_term_set_op.
664 /// @retval a non-null node iff !is_simple
665 /// @retval nullptr if is_simple() holds.
667 return is_simple() ? nullptr : down_cast<Query_term_set_op *>(m_query_term);
668 }
669 /// Return the query block iff !is_simple() holds
671 if (is_simple())
672 return nullptr;
673 else
674 return m_query_term->query_block();
675 }
676 bool is_leaf_block(Query_block *qb);
678 for (auto qt : query_terms<>()) {
679 if (qt->query_block() == qb) return qt;
680 }
681 return nullptr;
682 }
683
684 /**
685 Return iterator object over query terms rooted in m_query_term,
686 using either post order visiting (default) or pre order,
687 optionally skipping leaf nodes (query blocks corresponding to SELECTs or
688 table constructors). By default, we visit all nodes.
689 Usage: for (auto qt : query_terms<..>() { ... }
690 E.g.
691 for (auto qt : query_terms<>()) { } Visit all nodes, post order
692 for (auto qt : query_terms<QTC_PRE_ORDER, false>()) { }
693 Skip leaves, pre order
694 @tparam order == QTC_POST_ORDER if post order traversal is desired;default
695 == QTC_PRE_ORDER pre-order traversal
696 @tparam visit_leaves == VL_VISIT_LEAVES: if we want the traversal to include
697 leaf nodes i.e. the SELECTs or table constructors
698 == VL_SKIP_LEAVES: leaves will be skipped
699 @returns iterator object
700 */
701 template <Visit_order order = QTC_POST_ORDER,
702 Visit_leaves visit_leaves = VL_VISIT_LEAVES>
705 }
706
707 /**
708 Return the Query_block of the last query term in a n-ary set
709 operation that is the right side of the last DISTINCT set operation in that
710 n_ary set operation:
711 E.e. for
712 A UNION B UNION ALL C,
713 B's block will be returned. If no DISTINCT is present or not a set
714 operation, return nullptr.
715
716 @returns query block of last distinct right operand
717 */
719 auto const setop = down_cast<Query_term_set_op *>(m_query_term);
720 if (setop->last_distinct() > 0)
721 return setop->child(setop->last_distinct())->query_block();
722 else
723 return nullptr;
724 }
725
727 if (is_simple()) return false;
728 return down_cast<Query_term_set_op *>(m_query_term)->last_distinct() > 0;
729 }
730
731 private:
732 /**
733 Marker for subqueries in WHERE, HAVING, ORDER BY, GROUP BY and
734 SELECT item lists.
735 Must be read/written when holding LOCK_query_plan.
736
737 See Item_subselect::explain_subquery_checker
738 */
740
741 bool prepared; ///< All query blocks in query expression are prepared
742 bool optimized; ///< All query blocks in query expression are optimized
743 bool executed; ///< Query expression has been executed
744
745 /// Object to which the result for this query expression is sent.
746 /// Not used if we materialize directly into a parent query expression's
747 /// result table (see optimize()).
749
750 /**
751 An iterator you can read from to get all records for this query.
752
753 May be nullptr even after create_access_paths(), or in the case of an
754 unfinished materialization (see optimize()).
755 */
758
759 /**
760 If there is an unfinished materialization (see optimize()),
761 contains one element for each operand (query block) in this query
762 expression.
763 */
765
766 private:
767 /**
768 Convert the executor structures to a set of access paths, storing the result
769 in m_root_access_path.
770 */
771 void create_access_paths(THD *thd);
772
773 public:
774 /**
775 result of this query can't be cached, bit field, can be :
776 UNCACHEABLE_DEPENDENT
777 UNCACHEABLE_RAND
778 UNCACHEABLE_SIDEEFFECT
779 */
781
782 explicit Query_expression(enum_parsing_context parsing_context);
783
784 /// @return true for a query expression without UNION/INTERSECT/EXCEPT or
785 /// multi-level ORDER, i.e. we have a "simple table".
786 bool is_simple() const { return m_query_term->term_type() == QT_QUERY_BLOCK; }
787
788 /// Values for Query_expression::cleaned
790 UC_DIRTY, ///< Unit isn't cleaned
791 UC_PART_CLEAN, ///< Unit were cleaned, except JOIN and JOIN_TABs were
792 ///< kept for possible EXPLAIN
793 UC_CLEAN ///< Unit completely cleaned, all underlying JOINs were
794 ///< freed
795 };
796 enum_clean_state cleaned; ///< cleanliness state
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 Do everything that would be needed before running Init() on the root
981 iterator. In particular, clear out data from previous execution iterations,
982 if needed.
983 */
984 bool ClearForExecution();
985
986 bool ExecuteIteratorQuery(THD *thd);
987 bool execute(THD *thd);
988 bool explain(THD *explain_thd, const THD *query_thd);
989 bool explain_query_term(THD *explain_thd, const THD *query_thd,
990 Query_term *qt);
991 void cleanup(bool full);
992 /**
993 Destroy contained objects, in particular temporary tables which may
994 have their own mem_roots.
995 */
996 void destroy();
997
998 void print(const THD *thd, String *str, enum_query_type query_type);
999 bool accept(Select_lex_visitor *visitor);
1000
1001 /**
1002 Create a block to be used for ORDERING and LIMIT/OFFSET processing of a
1003 query term, which isn't itself a query specification or table value
1004 constructor. Such blocks are not included in the list starting in
1005 Query_Expression::first_query_block, and Query_block::next_query_block().
1006 They blocks are accessed via Query_term::query_block().
1007
1008 @param term the term on behalf of which we are making a post processing
1009 block
1010 @returns a query block
1011 */
1013
1015 assert(!is_prepared());
1016 prepared = true;
1017 }
1019 assert(is_prepared() && !is_optimized());
1020 optimized = true;
1021 }
1023 // assert(is_prepared() && is_optimized() && !is_executed());
1024 assert(is_prepared() && is_optimized());
1025 executed = true;
1026 }
1027 /// Reset this query expression for repeated evaluation within same execution
1029 assert(is_prepared() && is_optimized());
1030 executed = false;
1031 }
1032 /// Clear execution state, needed before new execution of prepared statement
1034 // Cannot be enforced when called from Prepared_statement::execute():
1035 // assert(is_prepared());
1036 optimized = false;
1037 executed = false;
1038 cleaned = UC_DIRTY;
1039 }
1040 /// Check state of preparation of the contained query expression.
1041 bool is_prepared() const { return prepared; }
1042 /// Check state of optimization of the contained query expression.
1043 bool is_optimized() const { return optimized; }
1044 /**
1045 Check state of execution of the contained query expression.
1046 Should not be used to check the state of a complete statement, use
1047 LEX::is_exec_completed() instead.
1048 */
1049 bool is_executed() const { return executed; }
1051 Query_result_interceptor *old_result);
1052 bool set_limit(THD *thd, Query_block *provider);
1053 bool has_any_limit() const;
1054
1055 inline bool is_union() const;
1056 inline bool is_set_operation() const;
1057
1058 /// Include a query expression below a query block.
1059 void include_down(LEX *lex, Query_block *outer);
1060
1061 /// Exclude this unit and immediately contained query_block objects
1062 void exclude_level();
1063
1064 /// Exclude subtree of current unit from tree of SELECTs
1065 void exclude_tree();
1066
1067 /// Renumber query blocks of a query expression according to supplied LEX
1068 void renumber_selects(LEX *lex);
1069
1071 bool save_cmd_properties(THD *thd);
1072
1073 friend class Query_block;
1074
1077 size_t num_visible_fields() const;
1078
1079 // If we are doing a query with global LIMIT, we need somewhere to store the
1080 // record count for FOUND_ROWS(). It can't be in any of the JOINs, since
1081 // they may have their own LimitOffsetIterators, which will write to
1082 // join->send_records whenever there is an OFFSET. Thus, we'll keep it here
1083 // instead.
1085
1088 void set_explain_marker_from(THD *thd, const Query_expression *u);
1089
1090#ifndef NDEBUG
1091 /**
1092 Asserts that none of {this unit and its children units} is fully cleaned
1093 up.
1094 */
1096#else
1097 void assert_not_fully_clean() {}
1098#endif
1099 void invalidate();
1100
1101 bool is_recursive() const { return first_recursive != nullptr; }
1102
1104
1106
1107 void fix_after_pullout(Query_block *parent_query_block,
1108 Query_block *removed_query_block);
1109
1110 /**
1111 If unit is a subquery, which forms an object of the upper level (an
1112 Item_subselect, a derived Table_ref), adds to this object a map
1113 of tables of the upper level which the unit references.
1114 */
1116
1117 /**
1118 If unit is a subquery, which forms an object of the upper level (an
1119 Item_subselect, a derived Table_ref), returns the place of this object
1120 in the upper level query block.
1121 */
1123
1124 bool walk(Item_processor processor, enum_walk walk, uchar *arg);
1125
1126 /**
1127 Replace all targeted items using transformer provided and info in
1128 arg.
1129 */
1130 bool replace_items(Item_transformer t, uchar *arg);
1131
1132 /*
1133 An exception: this is the only function that needs to adjust
1134 explain_marker.
1135 */
1136 friend bool parse_view_definition(THD *thd, Table_ref *view_ref);
1137};
1138
1141
1142/**
1143 Query_block type enum
1144*/
1146 EXPLAIN_NONE = 0,
1159 // Total:
1160 EXPLAIN_total ///< fake type, total number of all valid types
1161
1162 // Don't insert new types below this line!
1163};
1164
1165/**
1166 This class represents a query block, aka a query specification, which is
1167 a query consisting of a SELECT keyword, followed by a table list,
1168 optionally followed by a WHERE clause, a GROUP BY, etc.
1169*/
1170class Query_block : public Query_term {
1171 public:
1172 /**
1173 @note the group_by and order_by lists below will probably be added to the
1174 constructor when the parser is converted into a true bottom-up design.
1175
1176 //SQL_I_LIST<ORDER> *group_by, SQL_I_LIST<ORDER> order_by
1177 */
1179
1180 /// Query_term methods overridden
1181 void debugPrint(int level, std::ostringstream &buf) const override;
1182 /// Minion of debugPrint
1183 void qbPrint(int level, std::ostringstream &buf) const;
1185 Change_current_query_block *save_query_block,
1186 mem_root_deque<Item *> *insert_field_list,
1187 Query_result *common_result, ulonglong added_options,
1188 ulonglong removed_options,
1189 ulonglong create_option) override;
1191 // leaf block optimization done elsewhere
1192 return false;
1193 }
1194
1197 Mem_root_array<AppendPathParameters> *union_all_subpaths,
1198 bool calc_found_rows) override;
1199
1201 Query_term_type term_type() const override { return QT_QUERY_BLOCK; }
1202 const char *operator_string() const override { return "query_block"; }
1203 Query_block *query_block() const override {
1204 return const_cast<Query_block *>(this);
1205 }
1206 void label_children() override {}
1207 void destroy_tree() override { m_parent = nullptr; }
1208
1209 bool open_result_tables(THD *, int) override;
1210 /// end of overridden methods from Query_term
1211 bool absorb_limit_of(Query_block *block);
1212
1213 Item *where_cond() const { return m_where_cond; }
1215 void set_where_cond(Item *cond) { m_where_cond = cond; }
1216 Item *having_cond() const { return m_having_cond; }
1218 void set_having_cond(Item *cond) { m_having_cond = cond; }
1219 Item *qualify_cond() const { return m_qualify_cond; }
1221 void set_qualify_cond(Item *cond) { m_qualify_cond = cond; }
1224 bool change_query_result(THD *thd, Query_result_interceptor *new_result,
1225 Query_result_interceptor *old_result);
1226
1227 /// Set base options for a query block (and active options too)
1228 void set_base_options(ulonglong options_arg) {
1229 DBUG_EXECUTE_IF("no_const_tables", options_arg |= OPTION_NO_CONST_TABLES;);
1230
1231 // Make sure we do not overwrite options by accident
1232 assert(m_base_options == 0 && m_active_options == 0);
1233 m_base_options = options_arg;
1234 m_active_options = options_arg;
1235 }
1236
1237 /// Add base options to a query block, also update active options
1239 assert(first_execution);
1242 }
1243
1244 /**
1245 Remove base options from a query block.
1246 Active options are also updated, and we assume here that "extra" options
1247 cannot override removed base options.
1248 */
1250 assert(first_execution);
1253 }
1254
1255 /// Make active options from base options, supplied options and environment:
1256 void make_active_options(ulonglong added_options, ulonglong removed_options);
1257
1258 /// Adjust the active option set
1260
1261 /// @return the active query options
1263
1264 /**
1265 Set associated tables as read_only, ie. they cannot be inserted into,
1266 updated or deleted from during this statement.
1267 Commonly used for query blocks that are part of derived tables or
1268 views that are materialized.
1269 */
1271 // Set all referenced base tables as read only.
1272 for (Table_ref *tr = leaf_tables; tr != nullptr; tr = tr->next_leaf)
1273 tr->set_readonly();
1274 }
1275
1276 /// @returns a map of all tables references in the query block
1277 table_map all_tables_map() const { return (1ULL << leaf_table_count) - 1; }
1278
1279 bool remove_aggregates(THD *thd, Query_block *select);
1280
1284 Query_block *next_query_block() const { return next; }
1285
1287
1289
1290 void mark_as_dependent(Query_block *last, bool aggregate);
1291
1292 /// @returns true if query block references any tables
1293 bool has_tables() const { return m_table_list.elements != 0; }
1294
1295 /// @return true if query block is explicitly grouped (non-empty GROUP BY)
1296 bool is_explicitly_grouped() const { return group_list.elements != 0; }
1297
1298 /**
1299 @return true if this query block is implicitly grouped, ie it is not
1300 explicitly grouped but contains references to set functions.
1301 The query will return max. 1 row (@see also is_single_grouped()).
1302 */
1304 return m_agg_func_used && group_list.elements == 0;
1305 }
1306
1307 /**
1308 @return true if this query block has GROUP BY modifier.
1309 */
1311 return (olap != UNSPECIFIED_OLAP_TYPE);
1312 }
1313
1314 /**
1315 @return true if this query block is explicitly or implicitly grouped.
1316 @note a query with DISTINCT is not considered to be aggregated.
1317 @note in standard SQL, a query with HAVING is defined as grouped, however
1318 MySQL allows HAVING without any aggregation to be the same as WHERE.
1319 */
1320 bool is_grouped() const { return group_list.elements > 0 || m_agg_func_used; }
1321
1322 /// @return true if this query block contains DISTINCT at start of select list
1323 bool is_distinct() const { return active_options() & SELECT_DISTINCT; }
1324
1325 /**
1326 @return true if this query block contains an ORDER BY clause.
1327
1328 @note returns false if ORDER BY has been eliminated, e.g if the query
1329 can return max. 1 row.
1330 */
1331 bool is_ordered() const { return order_list.elements > 0; }
1332
1333 /**
1334 Based on the structure of the query at resolution time, it is possible to
1335 conclude that DISTINCT is useless and remove it.
1336 This is the case if:
1337 - all GROUP BY expressions are in SELECT list, so resulting group rows are
1338 distinct,
1339 - and ROLLUP is not specified, so it adds no row for NULLs.
1340
1341 @returns true if we can remove DISTINCT.
1342
1343 @todo could refine this to if ROLLUP were specified and all GROUP
1344 expressions were non-nullable, because ROLLUP then adds only NULL values.
1345 Currently, ROLLUP+DISTINCT is rejected because executor cannot handle
1346 it in all cases.
1347 */
1348 bool can_skip_distinct() const {
1349 return is_grouped() && hidden_group_field_count == 0 &&
1351 }
1352
1353 /// @return true if this query block has a LIMIT clause
1354 bool has_limit() const { return select_limit != nullptr; }
1355
1356 /// @return true if query block references full-text functions
1357 bool has_ft_funcs() const { return ftfunc_list->elements > 0; }
1358
1359 /// @returns true if query block is a recursive member of a recursive unit
1360 bool is_recursive() const { return recursive_reference != nullptr; }
1361
1362 /**
1363 Finds a group expression matching the given item, or nullptr if
1364 none. When there are multiple candidates, ones that match in name are
1365 given priority (such that “a AS c GROUP BY a,b,c” resolves to c, not a);
1366 if there is still a tie, the leftmost is given priority.
1367
1368 @param item The item to search for.
1369 @param [out] rollup_level If not nullptr, will be set to the group
1370 expression's index (0-based).
1371 */
1372 ORDER *find_in_group_list(Item *item, int *rollup_level) const;
1373 int group_list_size() const;
1374
1375 /// @returns true if query block contains window functions
1376 bool has_windows() const { return m_windows.elements > 0; }
1377
1378 void invalidate();
1379
1380 uint get_in_sum_expr() const { return in_sum_expr; }
1381
1382 bool add_item_to_list(Item *item);
1384 Table_ref *add_table_to_list(THD *thd, Table_ident *table, const char *alias,
1385 ulong table_options,
1387 enum_mdl_type mdl_type = MDL_SHARED_READ,
1388 List<Index_hint> *hints = nullptr,
1389 List<String> *partition_names = nullptr,
1390 LEX_STRING *option = nullptr,
1391 Parse_context *pc = nullptr);
1392
1393 /**
1394 Add item to the hidden part of select list
1395
1396 @param item item to add
1397
1398 @return Pointer to reference of the added item
1399 */
1400 Item **add_hidden_item(Item *item);
1401
1402 /// Remove hidden items from select list
1403 void remove_hidden_items();
1404
1405 Table_ref *get_table_list() const { return m_table_list.first; }
1406 bool init_nested_join(THD *thd);
1408 Table_ref *nest_last_join(THD *thd, size_t table_cnt = 2);
1411
1412 /// Wrappers over fields / \c get_fields_list() that hide items where
1413 /// item->hidden, meant for range-based for loops.
1414 /// See \c sql/visible_fields.h.
1416 auto visible_fields() const { return VisibleFields(fields); }
1417
1419 size_t visible_column_count() const override { return num_visible_fields(); }
1420
1421 /// Check privileges for views that are merged into query block
1422 bool check_view_privileges(THD *thd, Access_bitmask want_privilege_first,
1423 Access_bitmask want_privilege_next);
1424 /// Check privileges for all columns referenced from query block
1425 bool check_column_privileges(THD *thd);
1426
1427 /// Check privileges for column references in subqueries of a query block
1429
1430 /// Resolve and prepare information about tables for one query block
1431 bool setup_tables(THD *thd, Table_ref *tables, bool select_insert);
1432
1433 /// Resolve OFFSET and LIMIT clauses
1434 bool resolve_limits(THD *thd);
1435
1436 /// Resolve derived table, view, table function information for a query block
1437 bool resolve_placeholder_tables(THD *thd, bool apply_semijoin);
1438
1439 /// Propagate exclusion from table uniqueness test into subqueries
1441
1442 /// Merge name resolution context objects of a subquery into its parent
1443 void merge_contexts(Query_block *inner);
1444
1445 /// Merge derived table into query block
1446 bool merge_derived(THD *thd, Table_ref *derived_table);
1447
1448 bool flatten_subqueries(THD *thd);
1449
1450 /**
1451 Update available semijoin strategies for semijoin nests.
1452
1453 Available semijoin strategies needs to be updated on every execution since
1454 optimizer_switch setting may have changed.
1455
1456 @param thd Pointer to THD object for session.
1457 Used to access optimizer_switch
1458 */
1460
1461 /**
1462 Returns which subquery execution strategies can be used for this query
1463 block.
1464
1465 @param thd Pointer to THD object for session.
1466 Used to access optimizer_switch
1467
1468 @retval SUBQ_MATERIALIZATION Subquery Materialization should be used
1469 @retval SUBQ_EXISTS In-to-exists execution should be used
1470 @retval CANDIDATE_FOR_IN2EXISTS_OR_MAT A cost-based decision should be made
1471 */
1472 Subquery_strategy subquery_strategy(const THD *thd) const;
1473
1474 /**
1475 Returns whether semi-join is enabled for this query block
1476
1477 @see @c Opt_hints_qb::semijoin_enabled for details on how hints
1478 affect this decision. If there are no hints for this query block,
1479 optimizer_switch setting determines whether semi-join is used.
1480
1481 @param thd Pointer to THD object for session.
1482 Used to access optimizer_switch
1483
1484 @return true if semijoin is enabled,
1485 false otherwise
1486 */
1487 bool semijoin_enabled(const THD *thd) const;
1488
1490 sj_candidates = sj_cand;
1491 }
1493 sj_candidates->push_back(predicate);
1494 }
1495 bool has_sj_candidates() const {
1496 return sj_candidates != nullptr && !sj_candidates->empty();
1497 }
1498
1499 bool has_subquery_transforms() const { return sj_candidates != nullptr; }
1500
1501 /// Add full-text function elements from a list into this query block
1503
1504 void set_lock_for_table(const Lock_descriptor &descriptor, Table_ref *table);
1505
1506 void set_lock_for_tables(thr_lock_type lock_type);
1507
1508 inline void init_order() {
1509 assert(order_list.elements == 0);
1510 order_list.elements = 0;
1511 order_list.first = nullptr;
1512 order_list.next = &order_list.first;
1513 }
1514 /*
1515 This method created for reiniting LEX in mysql_admin_table() and can be
1516 used only if you are going remove all Query_block & units except belonger
1517 to LEX (LEX::unit & LEX::select, for other purposes use
1518 Query_expression::exclude_level()
1519 */
1520 void cut_subtree() { slave = nullptr; }
1521 bool test_limit();
1522 /**
1523 Get offset for LIMIT.
1524
1525 Evaluate offset item if necessary.
1526
1527 @return Number of rows to skip.
1528
1529 @todo Integrate better with Query_expression::set_limit()
1530 */
1531 ha_rows get_offset(const THD *thd) const;
1532 /**
1533 Get limit.
1534
1535 Evaluate limit item if necessary.
1536
1537 @return Limit of rows in result.
1538
1539 @todo Integrate better with Query_expression::set_limit()
1540 */
1541 ha_rows get_limit(const THD *thd) const;
1542
1543 /// Assign a default name resolution object for this query block.
1544 bool set_context(Name_resolution_context *outer_context);
1545
1546 /// Setup the array containing references to base items
1547 bool setup_base_ref_items(THD *thd);
1548 void print(const THD *thd, String *str, enum_query_type query_type);
1549
1550 /**
1551 Print detail of the Query_block object.
1552
1553 @param thd Thread handler
1554 @param query_type Options to print out string output
1555 @param[out] str String of output.
1556 */
1557 void print_query_block(const THD *thd, String *str,
1558 enum_query_type query_type);
1559
1560 /**
1561 Print detail of the UPDATE statement.
1562
1563 @param thd Thread handler
1564 @param[out] str String of output
1565 @param query_type Options to print out string output
1566 */
1567 void print_update(const THD *thd, String *str, enum_query_type query_type);
1568
1569 /**
1570 Print detail of the DELETE statement.
1571
1572 @param thd Thread handler
1573 @param[out] str String of output
1574 @param query_type Options to print out string output
1575 */
1576 void print_delete(const THD *thd, String *str, enum_query_type query_type);
1577
1578 /**
1579 Print detail of the INSERT statement.
1580
1581 @param thd Thread handler
1582 @param[out] str String of output
1583 @param query_type Options to print out string output
1584 */
1585 void print_insert(const THD *thd, String *str, enum_query_type query_type);
1586
1587 /**
1588 Print detail of Hints.
1589
1590 @param thd Thread handler
1591 @param[out] str String of output
1592 @param query_type Options to print out string output
1593 */
1594 void print_hints(const THD *thd, String *str, enum_query_type query_type);
1595
1596 /**
1597 Print error.
1598
1599 @param thd Thread handler
1600 @param[out] str String of output
1601
1602 @retval false If there is no error
1603 @retval true else
1604 */
1605 bool print_error(const THD *thd, String *str);
1606
1607 /**
1608 Print select options.
1609
1610 @param[out] str String of output
1611 */
1613
1614 /**
1615 Print UPDATE options.
1616
1617 @param[out] str String of output
1618 */
1620
1621 /**
1622 Print DELETE options.
1623
1624 @param[out] str String of output
1625 */
1627
1628 /**
1629 Print INSERT options.
1630
1631 @param[out] str String of output
1632 */
1634
1635 /**
1636 Print list of tables.
1637
1638 @param thd Thread handler
1639 @param[out] str String of output
1640 @param table_list Table_ref object
1641 @param query_type Options to print out string output
1642 */
1643 void print_table_references(const THD *thd, String *str,
1644 Table_ref *table_list,
1645 enum_query_type query_type);
1646
1647 /**
1648 Print list of items in Query_block object.
1649
1650 @param thd Thread handle
1651 @param[out] str String of output
1652 @param query_type Options to print out string output
1653 */
1654 void print_item_list(const THD *thd, String *str, enum_query_type query_type);
1655
1656 /**
1657 Print assignments list. Used in UPDATE and
1658 INSERT ... ON DUPLICATE KEY UPDATE ...
1659
1660 @param thd Thread handle
1661 @param[out] str String of output
1662 @param query_type Options to print out string output
1663 @param fields List columns to be assigned.
1664 @param values List of values.
1665 */
1666 void print_update_list(const THD *thd, String *str,
1667 enum_query_type query_type,
1669 const mem_root_deque<Item *> &values);
1670
1671 /**
1672 Print column list to be inserted into. Used in INSERT.
1673
1674 @param thd Thread handle
1675 @param[out] str String of output
1676 @param query_type Options to print out string output
1677 */
1678 void print_insert_fields(const THD *thd, String *str,
1679 enum_query_type query_type);
1680
1681 /**
1682 Print list of values, used in INSERT and for general VALUES clause.
1683
1684 @param thd Thread handle
1685 @param[out] str String of output
1686 @param query_type Options to print out string output
1687 @param values List of values
1688 @param prefix Prefix to print before each row in value list
1689 = nullptr: No prefix wanted
1690 */
1691 void print_values(const THD *thd, String *str, enum_query_type query_type,
1692 const mem_root_deque<mem_root_deque<Item *> *> &values,
1693 const char *prefix);
1694
1695 /**
1696 Print list of tables in FROM clause.
1697
1698 @param thd Thread handler
1699 @param[out] str String of output
1700 @param query_type Options to print out string output
1701 */
1702 void print_from_clause(const THD *thd, String *str,
1703 enum_query_type query_type);
1704
1705 /**
1706 Print list of conditions in WHERE clause.
1707
1708 @param thd Thread handle
1709 @param[out] str String of output
1710 @param query_type Options to print out string output
1711 */
1712 void print_where_cond(const THD *thd, String *str,
1713 enum_query_type query_type);
1714
1715 /**
1716 Print list of items in GROUP BY clause.
1717
1718 @param thd Thread handle
1719 @param[out] str String of output
1720 @param query_type Options to print out string output
1721 */
1722 void print_group_by(const THD *thd, String *str, enum_query_type query_type);
1723
1724 /**
1725 Print list of items in HAVING clause.
1726
1727 @param thd Thread handle
1728 @param[out] str String of output
1729 @param query_type Options to print out string output
1730 */
1731 void print_having(const THD *thd, String *str, enum_query_type query_type);
1732
1733 /**
1734 Print list of items in QUALIFY clause.
1735
1736 @param thd Thread handle
1737 @param[out] str String of output
1738 @param query_type Options to print out string output
1739 */
1740 void print_qualify(const THD *thd, String *str,
1741 enum_query_type query_type) const;
1742
1743 /**
1744 Print details of Windowing functions.
1745
1746 @param thd Thread handler
1747 @param[out] str String of output
1748 @param query_type Options to print out string output
1749 */
1750 void print_windows(const THD *thd, String *str, enum_query_type query_type);
1751
1752 /**
1753 Print list of items in ORDER BY clause.
1754
1755 @param thd Thread handle
1756 @param[out] str String of output
1757 @param query_type Options to print out string output
1758 */
1759 void print_order_by(const THD *thd, String *str,
1760 enum_query_type query_type) const;
1761
1762 void print_limit(const THD *thd, String *str,
1763 enum_query_type query_type) const;
1764 bool save_properties(THD *thd);
1765
1766 /**
1767 Accept function for SELECT and DELETE.
1768
1769 @param visitor Select_lex_visitor Object
1770 */
1771 bool accept(Select_lex_visitor *visitor);
1772
1773 /**
1774 Cleanup this subtree (this Query_block and all nested Query_blockes and
1775 Query_expressions).
1776 @param full if false only partial cleanup is done, JOINs and JOIN_TABs are
1777 kept to provide info for EXPLAIN CONNECTION; if true, complete cleanup is
1778 done, all JOINs are freed.
1779 */
1780 void cleanup(bool full) override;
1781 /*
1782 Recursively cleanup the join of this select lex and of all nested
1783 select lexes. This is not a full cleanup.
1784 */
1785 void cleanup_all_joins();
1786 /**
1787 Destroy contained objects, in particular temporary tables which may
1788 have their own mem_roots.
1789 */
1790 void destroy();
1791
1792 /// @return true when query block is not part of a set operation and is not a
1793 /// parenthesized query expression.
1796 }
1797
1798 /**
1799 @return true if query block is found during preparation to produce no data.
1800 Notice that if query is implicitly grouped, an aggregation row will
1801 still be returned.
1802 */
1803 bool is_empty_query() const { return m_empty_query; }
1804
1805 /// Set query block as returning no data
1806 /// @todo This may also be set when we have an always false WHERE clause
1808 assert(join == nullptr);
1809 m_empty_query = true;
1810 }
1811 /*
1812 For MODE_ONLY_FULL_GROUP_BY we need to know if
1813 this query block is the aggregation query of at least one aggregate
1814 function.
1815 */
1816 bool agg_func_used() const { return m_agg_func_used; }
1818
1819 void set_agg_func_used(bool val) { m_agg_func_used = val; }
1820
1822
1823 bool right_joins() const { return m_right_joins; }
1825
1826 /// Lookup for Query_block type
1827 enum_explain_type type() const;
1828
1829 /// Lookup for a type string
1830 const char *get_type_str() { return type_str[static_cast<int>(type())]; }
1832 return type_str[static_cast<int>(type)];
1833 }
1834
1836 bool is_cacheable() const { return !uncacheable; }
1837
1838 /// @returns true if this query block outputs at most one row.
1840 return (m_table_list.size() == 0 &&
1841 (!is_table_value_constructor || row_value_list->size() == 1));
1842 }
1843
1844 /// Include query block inside a query expression.
1845 void include_down(LEX *lex, Query_expression *outer);
1846
1847 /// Include a query block next to another query block.
1848 void include_neighbour(LEX *lex, Query_block *before);
1849
1850 /// Include query block inside a query expression, but do not link.
1852
1853 /// Include query block into global list.
1854 void include_in_global(Query_block **plink);
1855
1856 /// Include chain of query blocks into global list.
1858
1859 /// Renumber query blocks of contained query expressions
1860 void renumber(LEX *lex);
1861
1862 /**
1863 Does permanent transformations which are local to a query block (which do
1864 not merge it to another block).
1865 */
1866 bool apply_local_transforms(THD *thd, bool prune);
1867
1868 /// Pushes parts of the WHERE condition of this query block to materialized
1869 /// derived tables.
1871
1872 bool get_optimizable_conditions(THD *thd, Item **new_where,
1873 Item **new_having);
1874
1875 bool validate_outermost_option(LEX *lex, const char *wrong_option) const;
1876 bool validate_base_options(LEX *lex, ulonglong options) const;
1877
1878 bool walk(Item_processor processor, enum_walk walk, uchar *arg);
1879
1880 bool add_tables(THD *thd, const Mem_root_array<Table_ident *> *tables,
1881 ulong table_options, thr_lock_type lock_type,
1882 enum_mdl_type mdl_type);
1883
1884 bool resolve_rollup_wfs(THD *thd);
1885
1886 bool setup_conds(THD *thd);
1887 bool prepare(THD *thd, mem_root_deque<Item *> *insert_field_list);
1888 bool optimize(THD *thd, bool finalize_access_paths);
1889 void reset_nj_counters(mem_root_deque<Table_ref *> *join_list = nullptr);
1890
1891 // If the query block has exactly one single visible field, returns it.
1892 // If not, returns nullptr.
1893 Item *single_visible_field() const;
1894 size_t num_visible_fields() const;
1895
1896 // Whether the SELECT list is empty (hidden fields are ignored).
1897 // Typically used to distinguish INSERT INTO ... SELECT queries
1898 // from INSERT INTO ... VALUES queries.
1899 bool field_list_is_empty() const;
1900
1901 /// Creates a clone for the given expression by re-parsing the
1902 /// expression. Used in condition pushdown to derived tables.
1903 Item *clone_expression(THD *thd, Item *item, Table_ref *derived_table);
1904 /// Returns an expression from the select list of the query block
1905 /// using the field's index in a derived table.
1906 Item *get_derived_expr(uint expr_index);
1907
1909 AccessPath *child_path, TABLE *dst_table) const;
1910
1911 // ************************************************
1912 // * Members (most of these should not be public) *
1913 // ************************************************
1914
1916 /**
1917 All expressions needed after join and filtering, ie., select list,
1918 group by list, having clause, window clause, order by clause,
1919 including hidden fields.
1920 Does not include join conditions nor where clause.
1921
1922 This should ideally be changed into Mem_root_array<Item *>, but
1923 find_order_in_list() depends on pointer stability (it stores a pointer
1924 to an element in referenced_by[]). Similarly, there are some instances
1925 of thd->change_item_tree() that store pointers to elements in this list.
1926
1927 Because of this, adding or removing elements in the middle is not allowed;
1928 std::deque guarantees pointer stability only in the face of adding
1929 or removing elements from either end, ie., {push,pop}_{front_back}.
1930
1931 Currently, all hidden items must be before all visible items.
1932 This is primarily due to the requirement for pointer stability
1933 but also because change_to_use_tmp_fields() depends on it when mapping
1934 items to ref_item_array indexes. It would be good to get rid of this
1935 requirement in the future.
1936 */
1938
1939 /**
1940 All windows defined on the select, both named and inlined
1941 */
1943
1944 /**
1945 A pointer to ftfunc_list_alloc, list of full text search functions.
1946 */
1949
1950 /// The VALUES items of a table value constructor.
1952
1953 /// List of semi-join nests generated for this query block
1955
1956 /// List of tables in FROM clause - use Table_ref::next_local to traverse
1958
1959 /**
1960 ORDER BY clause.
1961 This list may be mutated during optimization (by remove_const() in the old
1962 optimizer or by RemoveRedundantOrderElements() in the hypergraph optimizer),
1963 so for prepared statements, we keep a copy of the ORDER.next pointers in
1964 order_list_ptrs, and re-establish the original list before each execution.
1965 */
1968
1969 /**
1970 GROUP BY clause. This list may be mutated during optimization (by
1971 \c remove_const() in the old optimizer or by
1972 RemoveRedundantOrderElements() in the hypergraph optimizer), so for prepared
1973 statements, we keep a copy of the ORDER.next pointers in \c group_list_ptrs,
1974 and re-establish the original list before each execution. The list can also
1975 be temporarily pruned and restored by \c Group_check (if transform done,
1976 cf. \c Query_block::m_gl_size_orig).
1977 */
1980 /**
1981 For an explicitly grouped, correlated, scalar subquery which is transformed
1982 to join with derived tables: the number of added non-column expressions.
1983 Used for better functional dependency analysis since this is checked during
1984 prepare *after* transformations. Transforms will append inner expressions
1985 to the group by list, rendering the check too optimistic. To remedy this,
1986 we temporarily remove the added compound (i.e. not simple column)
1987 expressions when doing the full group by check. This is bit too
1988 pessimistic: we can get occasionally false positives (full group by check
1989 error). The underlying problem is that we do not perform full group by
1990 checking before transformations. See also \c Group_check's ctor and dtor.
1991 */
1993
1994 // Used so that AggregateIterator knows which items to signal when the rollup
1995 // level changes. Obviously only used in the presence of rollup.
2000
2001 /// Query-block-level hints, for this query block
2003
2004 char *db{nullptr};
2005
2006 /**
2007 If this query block is a recursive member of a recursive unit: the
2008 Table_ref, in this recursive member, referencing the query
2009 name.
2010 */
2012
2013 /// Reference to LEX that this query block belongs to
2014 LEX *parent_lex{nullptr};
2015
2016 /**
2017 The set of those tables whose fields are referenced in the select list of
2018 this select level.
2019 */
2021 table_map outer_join{0}; ///< Bitmap of all inner tables from outer joins
2022
2023 /**
2024 Context for name resolution for all column references except columns
2025 from joined tables.
2026 */
2028
2029 /**
2030 Pointer to first object in list of Name res context objects that have
2031 this query block as the base query block.
2032 Includes field "context" which is embedded in this query block.
2033 */
2035
2036 /**
2037 After optimization it is pointer to corresponding JOIN. This member
2038 should be changed only when THD::LOCK_query_plan mutex is taken.
2039 */
2040 JOIN *join{nullptr};
2041 /// Set of table references contained in outer-most join nest
2043 /// Pointer to the set of table references in the currently active join
2045 /// table embedding the above list
2047 /**
2048 Points to first leaf table of query block. After setup_tables() is done,
2049 this is a list of base tables and derived tables. After derived tables
2050 processing is done, this is a list of base tables only.
2051 Use Table_ref::next_leaf to traverse the list.
2052 */
2054 /// Last table for LATERAL join, used by table functions
2056
2057 /// LIMIT clause, NULL if no limit is given
2059 /// LIMIT ... OFFSET clause, NULL if no offset is given
2061 /// Whether we have LIMIT 1 and no OFFSET.
2062 bool m_limit_1{false};
2063 /**
2064 Circular linked list of aggregate functions in nested query blocks.
2065 This is needed if said aggregate functions depend on outer values
2066 from this query block; if so, we want to add them as hidden items
2067 in our own field list, to be able to evaluate them.
2068 @see Item_sum::check_sum_func
2069 */
2071
2072 /**
2073 Array of pointers to "base" items; one each for every selected expression
2074 and referenced item in the query block. All references to fields are to
2075 buffers associated with the primary input tables.
2076 */
2078
2079 uint select_number{0}; ///< Query block number (used for EXPLAIN)
2080
2081 /**
2082 Saved values of the WHERE and HAVING clauses. Allowed values are:
2083 - COND_UNDEF if the condition was not specified in the query or if it
2084 has not been optimized yet
2085 - COND_TRUE if the condition is always true
2086 - COND_FALSE if the condition is impossible
2087 - COND_OK otherwise
2088 */
2091
2092 /// Parse context: indicates where the current expression is being parsed
2094 /// Parse context: is inside a set function if this is positive
2096 /// Parse context: is inside a window function if this is positive
2098
2099 /**
2100 Three fields used by semi-join transformations to know when semi-join is
2101 possible, and in which condition tree the subquery predicate is located.
2102 */
2112 RESOLVE_NONE}; ///< Indicates part of query being resolved
2113
2114 /**
2115 Number of fields used in select list or where clause of current select
2116 and all inner subselects.
2117 */
2119 /**
2120 Number of items in the select list, HAVING clause, QUALIFY clause and ORDER
2121 BY clause. It is used to reserve space in the base_ref_items array so that
2122 it is big enough to hold hidden items for any of the expressions or
2123 sub-expressions in those clauses.
2124 */
2126 /// Number of arguments of and/or/xor in where/having/on
2128 /// Number of predicates after preparation
2129 uint cond_count{0};
2130 /// Number of between predicates in where/having/on
2132 /// Maximal number of elements in multiple equalities
2134
2135 /**
2136 Number of Item_sum-derived objects in this SELECT. Keeps count of
2137 aggregate functions and window functions(to allocate items in ref array).
2138 See Query_block::setup_base_ref_items.
2139 */
2141 /// Number of Item_sum-derived objects in children and descendant SELECTs
2143
2144 /// Keep track for allocation of base_ref_items: scalar subqueries may be
2145 /// replaced by a field during scalar_to_derived transformation
2147
2148 /// Number of materialized derived tables and views in this query block.
2150 /// Number of partitioned tables
2152
2153 /**
2154 Number of wildcards used in the SELECT list. For example,
2155 SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
2156 has 3 wildcards.
2157 */
2158 uint with_wild{0};
2159
2160 /// Original query table map before aj/sj processing.
2162 /// Number of leaf tables in this query block.
2164 /// Number of derived tables and views in this query block.
2166 /// Number of table functions in this query block
2168
2169 /**
2170 Nesting level of query block, outer-most query block has level 0,
2171 its subqueries have level 1, etc. @see also sql/item_sum.h.
2172 */
2174
2175 /**
2176 Indicates whether this query block contains non-primitive grouping (such as
2177 ROLLUP).
2178 */
2180
2181 /// @see enum_condition_context
2183
2184 /// If set, the query block is of the form VALUES row_list.
2186
2187 /// Describes context of this query block (e.g if it is a derived table).
2189
2190 /**
2191 result of this query can't be cached, bit field, can be :
2192 UNCACHEABLE_DEPENDENT
2193 UNCACHEABLE_RAND
2194 UNCACHEABLE_SIDEEFFECT
2195 */
2197
2198 void update_used_tables();
2200 bool save_cmd_properties(THD *thd);
2201
2202 /**
2203 This variable is required to ensure proper work of subqueries and
2204 stored procedures. Generally, one should use the states of
2205 Query_arena to determine if it's a statement prepare or first
2206 execution of a stored procedure. However, in case when there was an
2207 error during the first execution of a stored procedure, the SP body
2208 is not expelled from the SP cache. Therefore, a deeply nested
2209 subquery might be left unoptimized. So we need this per-subquery
2210 variable to inidicate the optimization/execution state of every
2211 subquery. Prepared statements work OK in that regard, as in
2212 case of an error during prepare the PS is not created.
2213 */
2215
2216 /// True when semi-join pull-out processing is complete
2217 bool sj_pullout_done{false};
2218
2219 /// Used by nested scalar_to_derived transformations
2221
2222 /// True: skip local transformations during prepare() call (used by INSERT)
2224
2226
2227 /// true when having fix field called in processing of this query block
2228 bool having_fix_field{false};
2229 /// true when GROUP BY fix field called in processing of this query block
2230 bool group_fix_field{false};
2231 /// true when resolving a window's ORDER BY or PARTITION BY, the window
2232 /// belonging to this query block.
2234
2235 /**
2236 True if contains or aggregates set functions.
2237 @note this is wrong when a locally found set function is aggregated
2238 in an outer query block.
2239 */
2240 bool with_sum_func{false};
2241
2242 /**
2243 HAVING clause contains subquery => we can't close tables before
2244 query processing end even if we use temporary table
2245 */
2247
2248 /**
2249 If true, use select_limit to limit number of rows selected.
2250 Applicable when no explicit limit is supplied, and only for the
2251 outermost query block of a SELECT statement.
2252 */
2254
2255 /// If true, limit object is added internally
2256 bool m_internal_limit{false};
2257
2258 /// exclude this query block from unique_table() check
2260
2261 bool no_table_names_allowed{false}; ///< used for global order by
2262
2263 /// Hidden items added during optimization
2264 /// @note that using this means we modify resolved data during optimization
2266
2267 [[nodiscard]] bool limit_offset_preserves_first_row() const;
2268
2269 private:
2270 friend class Query_expression;
2271 friend class Condition_context;
2272
2273 /// Helper for save_properties()
2275 Group_list_ptrs **list_ptrs);
2276
2278 bool simplify_joins(THD *thd, mem_root_deque<Table_ref *> *join_list,
2279 bool top, bool in_sj, Item **new_conds,
2280 uint *changelog = nullptr);
2281 /// Remove semijoin condition for this query block
2282 void clear_sj_expressions(NESTED_JOIN *nested_join);
2283 /// Build semijoin condition for th query block
2284 bool build_sj_cond(THD *thd, NESTED_JOIN *nested_join,
2285 Query_block *subq_query_block, table_map outer_tables_map,
2286 Item **sj_cond, bool *simple_const);
2288 Table_ref *join_nest);
2289
2292 Item *join_cond, bool left_outer,
2293 bool use_inner_join);
2294 bool transform_subquery_to_derived(THD *thd, Table_ref **out_tl,
2295 Query_expression *subs_query_expression,
2296 Item_subselect *subq, bool use_inner_join,
2297 bool reject_multiple_rows,
2298 Item::Css_info *subquery,
2299 Item *lifted_where_cond);
2301 THD *thd, Item_exists_subselect *subq_pred);
2303 THD *thd, Table_ref *derived, Lifted_expressions_map *lifted_expressions,
2304 mem_root_deque<Item *> &exprs_added_to_group_by, uint hidden_fields);
2306 Lifted_expressions_map *lifted_exprs,
2307 Item *selected_field_or_ref,
2308 const uint first_non_hidden);
2310 THD *thd, Lifted_expressions_map *lifted_exprs);
2312 THD *thd, List_iterator<Item> &inner_exprs, Item *selected_item,
2313 bool *selected_expr_added_to_group_by,
2314 mem_root_deque<Item *> *exprs_added_to_group_by);
2316 THD *thd, Table_ref *derived, Item::Css_info *subquery,
2317 Item *lifted_where, Lifted_expressions_map *lifted_where_expressions,
2318 bool *added_card_check, size_t *added_window_card_checks);
2320 THD *thd, Table_ref *derived, Lifted_expressions_map *lifted_exprs,
2321 bool added_card_check, size_t added_window_card_checks);
2322 /// Replace the first visible item in the select list with a wrapping
2323 /// MIN or MAX aggregate function.
2324 bool replace_first_item_with_min_max(THD *thd, int item_no, bool use_min);
2325 void replace_referenced_item(Item *const old_item, Item *const new_item);
2326 void remap_tables(THD *thd);
2328 Item *resolve_rollup_item(THD *thd, Item *item);
2329 bool resolve_rollup(THD *thd);
2330
2331 bool setup_wild(THD *thd);
2332 bool setup_order_final(THD *thd);
2333 bool setup_group(THD *thd);
2334 void fix_after_pullout(Query_block *parent_query_block,
2335 Query_block *removed_query_block);
2338 bool empty_order_list(Query_block *sl);
2340 bool in_update);
2341 bool find_common_table_expr(THD *thd, Table_ident *table_id, Table_ref *tl,
2342 Parse_context *pc, bool *found);
2343 /**
2344 Transform eligible scalar subqueries in the SELECT list, WHERE condition,
2345 HAVING condition or JOIN conditions of a query block[*] to an equivalent
2346 derived table of a LEFT OUTER join, e.g. as shown in this uncorrelated
2347 subquery:
2348
2349 [*] a.k.a "transformed query block" throughout this method and its minions.
2350
2351 <pre>
2352 SELECT * FROM t1
2353 WHERE t1.a > (SELECT COUNT(a) AS cnt FROM t2); ->
2354
2355 SELECT t1.* FROM t1 LEFT OUTER JOIN
2356 (SELECT COUNT(a) AS cnt FROM t2) AS derived
2357 ON TRUE WHERE t1.a > derived.cnt;
2358 </pre>
2359
2360 Grouping in the transformed query block may necessitate the grouping to be
2361 moved down to another derived table, cf. transform_grouped_to_derived.
2362
2363 Limitations:
2364 - only implicitly grouped subqueries (guaranteed to have cardinality one)
2365 are identified as scalar subqueries.
2366 _ Correlated subqueries are not handled
2367
2368 @param[in,out] thd the session context
2369 @returns true on error
2370 */
2373 Item **lifted_where);
2374 bool replace_item_in_expression(Item **expr, bool was_hidden,
2376 Item_transformer transformer);
2377 bool transform_grouped_to_derived(THD *thd, bool *break_off);
2378 bool replace_subquery_in_expr(THD *thd, Item::Css_info *subquery,
2379 Table_ref *tr, Item **expr);
2380 bool nest_derived(THD *thd, Item *join_cond,
2381 mem_root_deque<Table_ref *> *join_list,
2382 Table_ref *new_derived_table);
2383
2385
2386 // Delete unused columns from merged derived tables
2388
2389 bool prepare_values(THD *thd);
2390 bool check_only_full_group_by(THD *thd);
2391 /**
2392 Copies all non-aggregated calls to the full-text search MATCH function from
2393 the HAVING clause to the SELECT list (as hidden items), so that we can
2394 materialize their result and not only their input. This is needed when the
2395 result will be accessed after aggregation, as the result from MATCH cannot
2396 be recalculated from its input alone. It also needs the underlying scan to
2397 be positioned on the correct row. Storing the value before aggregation
2398 removes the need for evaluating MATCH again after materialization.
2399 */
2401
2402 //
2403 // Members:
2404 //
2405
2406 /**
2407 Pointer to collection of subqueries candidate for semi/antijoin
2408 conversion.
2409 Template parameter is "true": no need to run DTORs on pointers.
2410 */
2412
2413 /// How many expressions are part of the order by but not select list.
2415
2416 /**
2417 Intrusive linked list of all query blocks within the same
2418 query expression.
2419 */
2421
2422 /// The query expression containing this query block.
2424 /// The first query expression contained within this query block.
2426
2427 /// Intrusive double-linked global list of query blocks.
2430
2431 /// Result of this query block
2433
2434 /**
2435 Options assigned from parsing and throughout resolving,
2436 should not be modified after resolving is done.
2437 */
2439 /**
2440 Active options. Derived from base options, modifiers added during
2441 resolving and values from session variable option_bits. Since the latter
2442 may change, active options are refreshed per execution of a statement.
2443 */
2445
2446 /**
2447 If the query block includes non-primitive grouping, then these modifiers are
2448 represented as grouping sets. The variable 'm_num_grouping_sets' holds the
2449 count of grouping sets.
2450 */
2452
2453 public:
2455 nullptr}; ///< Used when resolving outer join condition
2456
2457 /**
2458 Initializes the grouping set if the query block includes GROUP BY
2459 modifiers.
2460 */
2461 bool allocate_grouping_sets(THD *thd);
2462
2463 /**
2464 Populates the grouping sets if the query block includes non-primitive
2465 grouping.
2466 */
2467 bool populate_grouping_sets(THD *thd);
2469
2470 private:
2471 /**
2472 Condition to be evaluated after all tables in a query block are joined.
2473 After all permanent transformations have been conducted by
2474 Query_block::prepare(), this condition is "frozen", any subsequent changes
2475 to it must be done with change_item_tree(), unless they only modify AND/OR
2476 items and use a copy created by Query_block::get_optimizable_conditions().
2477 Same is true for 'having_cond'.
2478 */
2480
2481 /// Condition to be evaluated on grouped rows after grouping.
2483
2484 /// Condition to be evaluated after window functions.
2486
2487 /// Number of GROUP BY expressions added to all_fields
2489
2490 /// A backup of the items in base_ref_items at the end of preparation, so that
2491 /// base_ref_items can be restored between executions of prepared statements.
2492 /// Empty if it's a regular statement.
2494
2495 /**
2496 True if query block has semi-join nests merged into it. Notice that this
2497 is updated earlier than sj_nests, so check this if info is needed
2498 before the full resolver process is complete.
2499 */
2500 bool has_sj_nests{false};
2501 bool has_aj_nests{false}; ///< @see has_sj_nests; counts antijoin nests.
2502 bool m_right_joins{false}; ///< True if query block has right joins
2503
2504 /// Allow merge of immediate unnamed derived tables
2506
2507 bool m_agg_func_used{false};
2509
2510 /**
2511 True if query block does not generate any rows before aggregation,
2512 determined during preparation (not optimization).
2513 */
2514 bool m_empty_query{false};
2515
2516 static const char
2518};
2519
2520inline bool Query_expression::is_union() const {
2521 Query_term *qt = query_term();
2522 while (qt->term_type() == QT_UNARY)
2523 qt = down_cast<Query_term_unary *>(qt)->child(0);
2524 return qt->term_type() == QT_UNION;
2525}
2526
2528 Query_term *qt = query_term();
2529 while (qt->term_type() == QT_UNARY)
2530 qt = down_cast<Query_term_unary *>(qt)->child(0);
2531 const Query_term_type type = qt->term_type();
2532 return type == QT_UNION || type == QT_INTERSECT || type == QT_EXCEPT;
2533}
2534
2535/// Utility RAII class to save/modify/restore the condition_context information
2536/// of a query block. @see enum_condition_context.
2538 public:
2540 Query_block *select_ptr,
2542 : select(nullptr), saved_value() {
2543 if (select_ptr) {
2544 select = select_ptr;
2546 // More restrictive wins over less restrictive:
2547 if (new_type == enum_condition_context::NEITHER ||
2548 (new_type == enum_condition_context::ANDS_ORS &&
2550 select->condition_context = new_type;
2551 }
2552 }
2555 }
2556
2557 private:
2560};
2561
2563 std::function<bool(Table_ref *)> action);
2564
2565/**
2566 Base class for secondary engine execution context objects. Secondary
2567 storage engines may create classes derived from this one which
2568 contain state they need to preserve between optimization and
2569 execution of statements. The context objects should be allocated on
2570 the execution MEM_ROOT.
2571*/
2573 public:
2574 /**
2575 Destructs the secondary engine execution context object. It is
2576 called after the query execution has completed. Secondary engines
2577 may override the destructor in subclasses and add code that
2578 performs cleanup tasks that are needed after query execution.
2579 */
2581};
2582
2584 char *user;
2588
2589 void reset();
2591
2596
2598 : m_db{db}, m_name{name}, m_alias{alias} {}
2599};
2600
2604 bool detistic = false;
2606 LEX_CSTRING language = NULL_CSTR; ///< CREATE|ALTER ... LANGUAGE <language>
2607
2608 /**
2609 List of imported libraries for this routine
2610 */
2612
2613 /**
2614 Add library names to the set of imported libraries.
2615
2616 We only allow one USING clause in CREATE statements, so repeated calls
2617 to this function should fail.
2618
2619 @param libs Set of libraries to be added
2620 @param mem_root MEM_ROOT to use for allocation
2621
2622 @returns true on failures; false otherwise
2623 */
2625 MEM_ROOT *mem_root) {
2626 assert(!libs.empty());
2627
2628 if (m_imported_libraries != nullptr) return true; // Allow a single USING.
2629
2630 if (create_imported_libraries_deque(mem_root)) return true;
2631
2632 while (!libs.empty()) {
2633 if (m_imported_libraries->push_back(libs.front())) return true;
2634 libs.pop_front();
2635 }
2636 return false;
2637 }
2638
2639 /**
2640 Add a library to the set of imported libraries.
2641
2642 @param database The library's database.
2643 @param name The library's name.
2644 @param alias The library's alias.
2645 @param mem_root MEM_ROOT to use for allocation
2646
2647 @returns true on failures; false otherwise
2648 */
2649 bool add_imported_library(std::string_view database, std::string_view name,
2650 std::string_view alias, MEM_ROOT *mem_root) {
2651 if (create_imported_libraries_deque(mem_root)) return true;
2652
2653 return m_imported_libraries->push_back({
2654 {strmake_root(mem_root, database.data(), database.length()),
2655 database.length()}, // sp_name_with_alias.m_db
2656 {strmake_root(mem_root, name.data(), name.length()),
2657 name.length()}, // sp_name_with_alias.m_name
2658 {strmake_root(mem_root, alias.data(), alias.length()),
2659 alias.length()} // sp_name_with_alias.m_alias
2660 });
2661 }
2662
2664 if (m_imported_libraries != nullptr) return false; // Already allocated.
2667 return m_imported_libraries == nullptr;
2668 }
2669
2670 /**
2671 Get the set of imported libraries for the routine
2672
2673 @returns The set of imported libraries, nullptr if no imported libraries
2674 */
2676 return m_imported_libraries;
2677 }
2678
2679 /**
2680 Reset the structure.
2681 */
2682 void reset(void) {
2685 detistic = false;
2688 m_imported_libraries = nullptr;
2689 }
2690};
2691
2692extern const LEX_STRING null_lex_str;
2693
2697
2698 /**
2699 FOLLOWS or PRECEDES as specified in the CREATE TRIGGER statement.
2700 */
2702
2703 /**
2704 Trigger name referenced in the FOLLOWS/PRECEDES clause of the CREATE TRIGGER
2705 statement.
2706 */
2708};
2709
2711
2712/*
2713 Class representing list of all tables used by statement and other
2714 information which is necessary for opening and locking its tables,
2715 like SQL command for this statement.
2716
2717 Also contains information about stored functions used by statement
2718 since during its execution we may have to add all tables used by its
2719 stored functions/triggers to this list in order to pre-open and lock
2720 them.
2721
2722 Also used by LEX::reset_n_backup/restore_backup_query_tables_list()
2723 methods to save and restore this information.
2724*/
2725
2727 public:
2729
2730 /**
2731 SQL command for this statement. Part of this class since the
2732 process of opening and locking tables for the statement needs
2733 this information to determine correct type of lock for some of
2734 the tables.
2735 */
2737 /* Global list of all tables used by this statement */
2739 /* Pointer to next_global member of last element in the previous list. */
2741 /*
2742 If non-0 then indicates that query requires prelocking and points to
2743 next_global member of last own element in query table list (i.e. last
2744 table which was not added to it as part of preparation to prelocking).
2745 0 - indicates that this query does not need prelocking.
2746 */
2748 /*
2749 Set of stored routines called by statement.
2750 (Note that we use lazy-initialization for this hash).
2751
2752 See Sroutine_hash_entry for explanation why this hash uses binary
2753 key comparison.
2754 */
2756 std::unique_ptr<malloc_unordered_map<std::string, Sroutine_hash_entry *>>
2758 /*
2759 List linking elements of 'sroutines' set. Allows you to add new elements
2760 to this set as you iterate through the list of existing elements.
2761 'sroutines_list_own_last' is pointer to ::next member of last element of
2762 this list which represents routine which is explicitly used by query.
2763 'sroutines_list_own_elements' number of explicitly used routines.
2764 We use these two members for restoring of 'sroutines_list' to the state
2765 in which it was right after query parsing.
2766 */
2770
2771 /**
2772 Does this LEX context have any stored functions
2773 */
2775
2776 /**
2777 Locking state of tables in this particular statement.
2778
2779 If we under LOCK TABLES or in prelocked mode we consider tables
2780 for the statement to be "locked" if there was a call to lock_tables()
2781 (which called handler::start_stmt()) for tables of this statement
2782 and there was no matching close_thread_tables() call.
2783
2784 As result this state may differ significantly from one represented
2785 by Open_tables_state::lock/locked_tables_mode more, which are always
2786 "on" under LOCK TABLES or in prelocked mode.
2787 */
2791 return (lock_tables_state == LTS_LOCKED);
2792 }
2793
2794 /**
2795 Number of tables which were open by open_tables() and to be locked
2796 by lock_tables().
2797 Note that we set this member only in some cases, when this value
2798 needs to be passed from open_tables() to lock_tables() which are
2799 separated by some amount of code.
2800 */
2802
2803 /*
2804 These constructor and destructor serve for creation/destruction
2805 of Query_tables_list instances which are used as backup storage.
2806 */
2809
2810 /* Initializes (or resets) Query_tables_list object for "real" use. */
2811 void reset_query_tables_list(bool init);
2814 *this = std::move(*state);
2815 }
2816
2817 /*
2818 Direct addition to the list of query tables.
2819 If you are using this function, you must ensure that the table
2820 object, in particular table->db member, is initialized.
2821 */
2823 *(table->prev_global = query_tables_last) = table;
2824 query_tables_last = &table->next_global;
2825 }
2827 void mark_as_requiring_prelocking(Table_ref **tables_own_last) {
2828 query_tables_own_last = tables_own_last;
2829 }
2830 /* Return pointer to first not-own table in query-tables or 0 */
2832 return (query_tables_own_last ? *query_tables_own_last : nullptr);
2833 }
2836 *query_tables_own_last = nullptr;
2838 query_tables_own_last = nullptr;
2839 }
2840 }
2841
2842 /**
2843 All types of unsafe statements.
2844
2845 @note The int values of the enum elements are used to point to
2846 bits in two bitmaps in two different places:
2847
2848 - Query_tables_list::binlog_stmt_flags
2849 - THD::binlog_unsafe_warning_flags
2850
2851 Hence in practice this is not an enum at all, but a map from
2852 symbols to bit indexes.
2853
2854 The ordering of elements in this enum must correspond to the order of
2855 elements in the array binlog_stmt_unsafe_errcode.
2856 */
2858 /**
2859 SELECT..LIMIT is unsafe because the set of rows returned cannot
2860 be predicted.
2861 */
2863 /**
2864 Access to log tables is unsafe because slave and master probably
2865 log different things.
2866 */
2868 /**
2869 Inserting into an autoincrement column in a stored routine is unsafe.
2870 Even with just one autoincrement column, if the routine is invoked more
2871 than once slave is not guaranteed to execute the statement graph same way
2872 as the master. And since it's impossible to estimate how many times a
2873 routine can be invoked at the query pre-execution phase (see lock_tables),
2874 the statement is marked pessimistically unsafe.
2875 */
2877 /**
2878 Using a UDF (user-defined function) is unsafe.
2879 */
2881 /**
2882 Using most system variables is unsafe, because slave may run
2883 with different options than master.
2884 */
2886 /**
2887 Using some functions is unsafe (e.g., UUID).
2888 */
2890
2891 /**
2892 Mixing transactional and non-transactional statements are unsafe if
2893 non-transactional reads or writes are occur after transactional
2894 reads or writes inside a transaction.
2895 */
2897
2898 /**
2899 Mixing self-logging and non-self-logging engines in a statement
2900 is unsafe.
2901 */
2903
2904 /**
2905 Statements that read from both transactional and non-transactional
2906 tables and write to any of them are unsafe.
2907 */
2909
2910 /**
2911 INSERT...IGNORE SELECT is unsafe because which rows are ignored depends
2912 on the order that rows are retrieved by SELECT. This order cannot be
2913 predicted and may differ on master and the slave.
2914 */
2916
2917 /**
2918 INSERT...SELECT...UPDATE is unsafe because which rows are updated depends
2919 on the order that rows are retrieved by SELECT. This order cannot be
2920 predicted and may differ on master and the slave.
2921 */
2923
2924 /**
2925 Query that writes to a table with auto_inc column after selecting from
2926 other tables are unsafe as the order in which the rows are retrieved by
2927 select may differ on master and slave.
2928 */
2930
2931 /**
2932 INSERT...REPLACE SELECT is unsafe because which rows are replaced depends
2933 on the order that rows are retrieved by SELECT. This order cannot be
2934 predicted and may differ on master and the slave.
2935 */
2937
2938 /**
2939 CREATE TABLE... IGNORE... SELECT is unsafe because which rows are ignored
2940 depends on the order that rows are retrieved by SELECT. This order cannot
2941 be predicted and may differ on master and the slave.
2942 */
2944
2945 /**
2946 CREATE TABLE...REPLACE... SELECT is unsafe because which rows are replaced
2947 depends on the order that rows are retrieved from SELECT. This order
2948 cannot be predicted and may differ on master and the slave
2949 */
2951
2952 /**
2953 CREATE TABLE...SELECT on a table with auto-increment column is unsafe
2954 because which rows are replaced depends on the order that rows are
2955 retrieved from SELECT. This order cannot be predicted and may differ on
2956 master and the slave
2957 */
2959
2960 /**
2961 UPDATE...IGNORE is unsafe because which rows are ignored depends on the
2962 order that rows are updated. This order cannot be predicted and may differ
2963 on master and the slave.
2964 */
2966
2967 /**
2968 INSERT... ON DUPLICATE KEY UPDATE on a table with more than one
2969 UNIQUE KEYS is unsafe.
2970 */
2972
2973 /**
2974 INSERT into auto-inc field which is not the first part in composed
2975 primary key.
2976 */
2978
2979 /**
2980 Using a plugin is unsafe.
2981 */
2985
2986 /**
2987 XA transactions and statements.
2988 */
2990
2991 /**
2992 If a substatement inserts into or updates a table that has a column with
2993 an unsafe DEFAULT expression, it may not have the same effect on the
2994 slave.
2995 */
2997
2998 /**
2999 DML or DDL statement that reads a ACL table is unsafe, because the row
3000 are read without acquiring SE row locks. This would allow ACL tables to
3001 be updated by concurrent thread. It would not have the same effect on the
3002 slave.
3003 */
3005
3006 /**
3007 Generating invisible primary key for a table created using CREATE TABLE...
3008 SELECT... is unsafe because order in which rows are retrieved by the
3009 SELECT determines which (if any) rows are inserted. This order cannot be
3010 predicted and values for generated invisible primary key column may
3011 differ on source and replica when @@session.binlog_format=STATEMENT.
3012 */
3014
3015 /* the last element of this enumeration type. */
3018 /**
3019 This has all flags from 0 (inclusive) to BINLOG_STMT_FLAG_COUNT
3020 (exclusive) set.
3021 */
3023 ((1 << BINLOG_STMT_UNSAFE_COUNT) - 1);
3024
3025 /**
3026 Maps elements of enum_binlog_stmt_unsafe to error codes.
3027 */
3029
3030 /**
3031 Determine if this statement is marked as unsafe.
3032
3033 @retval 0 if the statement is not marked as unsafe.
3034 @retval nonzero if the statement is marked as unsafe.
3035 */
3036 inline bool is_stmt_unsafe() const { return get_stmt_unsafe_flags() != 0; }
3037
3039 return binlog_stmt_flags & (1 << unsafe);
3040 }
3041
3042 /**
3043 Flag the current (top-level) statement as unsafe.
3044 The flag will be reset after the statement has finished.
3045
3046 @param unsafe_type The type of unsafety: one of the @c
3047 BINLOG_STMT_FLAG_UNSAFE_* flags in @c enum_binlog_stmt_flag.
3048 */
3049 inline void set_stmt_unsafe(enum_binlog_stmt_unsafe unsafe_type) {
3050 DBUG_TRACE;
3051 assert(unsafe_type >= 0 && unsafe_type < BINLOG_STMT_UNSAFE_COUNT);
3052 binlog_stmt_flags |= (1U << unsafe_type);
3053 return;
3054 }
3055
3056 /**
3057 Set the bits of binlog_stmt_flags determining the type of
3058 unsafeness of the current statement. No existing bits will be
3059 cleared, but new bits may be set.
3060
3061 @param flags A binary combination of zero or more bits, (1<<flag)
3062 where flag is a member of enum_binlog_stmt_unsafe.
3063 */
3065 DBUG_TRACE;
3066 assert((flags & ~BINLOG_STMT_UNSAFE_ALL_FLAGS) == 0);
3068 return;
3069 }
3070
3071 /**
3072 Return a binary combination of all unsafe warnings for the
3073 statement. If the statement has been marked as unsafe by the
3074 'flag' member of enum_binlog_stmt_unsafe, then the return value
3075 from this function has bit (1<<flag) set to 1.
3076 */
3078 DBUG_TRACE;
3080 }
3081
3082 /**
3083 Determine if this statement is a row injection.
3084
3085 @retval 0 if the statement is not a row injection
3086 @retval nonzero if the statement is a row injection
3087 */
3088 inline bool is_stmt_row_injection() const {
3089 constexpr uint32_t shift =
3090 static_cast<uint32_t>(BINLOG_STMT_UNSAFE_COUNT) +
3091 static_cast<uint32_t>(BINLOG_STMT_TYPE_ROW_INJECTION);
3092 return binlog_stmt_flags & (1U << shift);
3093 }
3094
3095 /**
3096 Flag the statement as a row injection. A row injection is either
3097 a BINLOG statement, or a row event in the relay log executed by
3098 the slave SQL thread.
3099 */
3101 constexpr uint32_t shift =
3102 static_cast<uint32_t>(BINLOG_STMT_UNSAFE_COUNT) +
3103 static_cast<uint32_t>(BINLOG_STMT_TYPE_ROW_INJECTION);
3104 DBUG_TRACE;
3105 binlog_stmt_flags |= (1U << shift);
3106 }
3107
3109 /*
3110 If a transactional table is about to be read. Note that
3111 a write implies a read.
3112 */
3114 /*
3115 If a non-transactional table is about to be read. Note that
3116 a write implies a read.
3117 */
3119 /*
3120 If a temporary transactional table is about to be read. Note
3121 that a write implies a read.
3122 */
3124 /*
3125 If a temporary non-transactional table is about to be read. Note
3126 that a write implies a read.
3127 */
3129 /*
3130 If a transactional table is about to be updated.
3131 */
3133 /*
3134 If a non-transactional table is about to be updated.
3135 */
3137 /*
3138 If a temporary transactional table is about to be updated.
3139 */
3141 /*
3142 If a temporary non-transactional table is about to be updated.
3143 */
3145 /*
3146 The last element of the enumeration. Please, if necessary add
3147 anything before this.
3148 */
3151
3152#ifndef NDEBUG
3153 static inline const char *stmt_accessed_table_string(
3154 enum_stmt_accessed_table accessed_table) {
3155 switch (accessed_table) {
3157 return "STMT_READS_TRANS_TABLE";
3158 break;
3160 return "STMT_READS_NON_TRANS_TABLE";
3161 break;
3163 return "STMT_READS_TEMP_TRANS_TABLE";
3164 break;
3166 return "STMT_READS_TEMP_NON_TRANS_TABLE";
3167 break;
3169 return "STMT_WRITES_TRANS_TABLE";
3170 break;
3172 return "STMT_WRITES_NON_TRANS_TABLE";
3173 break;
3175 return "STMT_WRITES_TEMP_TRANS_TABLE";
3176 break;
3178 return "STMT_WRITES_TEMP_NON_TRANS_TABLE";
3179 break;
3181 default:
3182 assert(0);
3183 break;
3184 }
3186 return "";
3187 }
3188#endif /* DBUG */
3189
3190#define BINLOG_DIRECT_ON \
3191 0xF0 /* unsafe when \
3192 --binlog-direct-non-trans-updates \
3193 is ON */
3194
3195#define BINLOG_DIRECT_OFF \
3196 0xF /* unsafe when \
3197 --binlog-direct-non-trans-updates \
3198 is OFF */
3199
3200#define TRX_CACHE_EMPTY 0x33 /* unsafe when trx-cache is empty */
3201
3202#define TRX_CACHE_NOT_EMPTY 0xCC /* unsafe when trx-cache is not empty */
3203
3204#define IL_LT_REPEATABLE 0xAA /* unsafe when < ISO_REPEATABLE_READ */
3205
3206#define IL_GTE_REPEATABLE 0x55 /* unsafe when >= ISO_REPEATABLE_READ */
3207
3208 /**
3209 Sets the type of table that is about to be accessed while executing a
3210 statement.
3212 @param accessed_table Enumeration type that defines the type of table,
3213 e.g. temporary, transactional, non-transactional.
3214 */
3215 inline void set_stmt_accessed_table(enum_stmt_accessed_table accessed_table) {
3216 DBUG_TRACE;
3217
3218 assert(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
3219 stmt_accessed_table_flag |= (1U << accessed_table);
3220
3221 return;
3222 }
3223
3224 /**
3225 Checks if a type of table is about to be accessed while executing a
3226 statement.
3227
3228 @param accessed_table Enumeration type that defines the type of table,
3229 e.g. temporary, transactional, non-transactional.
3231 @retval true if the type of the table is about to be accessed
3232 @retval false otherwise
3233 */
3234 inline bool stmt_accessed_table(enum_stmt_accessed_table accessed_table) {
3235 DBUG_TRACE;
3236
3237 assert(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
3238
3239 return (stmt_accessed_table_flag & (1U << accessed_table)) != 0;
3240 }
3241
3242 /*
3243 Checks if a mixed statement is unsafe.
3244
3245
3246 @param in_multi_stmt_transaction_mode defines if there is an on-going
3247 multi-transactional statement.
3248 @param binlog_direct defines if --binlog-direct-non-trans-updates is
3249 active.
3250 @param trx_cache_is_not_empty defines if the trx-cache is empty or not.
3251 @param trx_isolation defines the isolation level.
3252
3253 @return
3254 @retval true if the mixed statement is unsafe
3255 @retval false otherwise
3256 */
3257 inline bool is_mixed_stmt_unsafe(bool in_multi_stmt_transaction_mode,
3258 bool binlog_direct,
3259 bool trx_cache_is_not_empty,
3260 uint tx_isolation) {
3261 bool unsafe = false;
3262
3263 if (in_multi_stmt_transaction_mode) {
3264 const uint condition =
3265 (binlog_direct ? BINLOG_DIRECT_ON : BINLOG_DIRECT_OFF) &
3266 (trx_cache_is_not_empty ? TRX_CACHE_NOT_EMPTY : TRX_CACHE_EMPTY) &
3267 (tx_isolation >= ISO_REPEATABLE_READ ? IL_GTE_REPEATABLE
3269
3270 unsafe = (binlog_unsafe_map[stmt_accessed_table_flag] & condition);
3271
3272#if !defined(NDEBUG)
3273 DBUG_PRINT("LEX::is_mixed_stmt_unsafe",
3274 ("RESULT %02X %02X %02X\n", condition,
3277
3278 int type_in = 0;
3279 for (; type_in < STMT_ACCESS_TABLE_COUNT; type_in++) {
3281 DBUG_PRINT("LEX::is_mixed_stmt_unsafe",
3282 ("ACCESSED %s ", stmt_accessed_table_string(
3283 (enum_stmt_accessed_table)type_in)));
3284 }
3285#endif
3286 }
3287
3290 tx_isolation < ISO_REPEATABLE_READ)
3291 unsafe = true;
3294 tx_isolation < ISO_REPEATABLE_READ)
3295 unsafe = true;
3296
3297 return (unsafe);
3298 }
3299
3300 /**
3301 true if the parsed tree contains references to stored procedures, triggers
3302 or functions, false otherwise
3304 bool uses_stored_routines() const { return sroutines_list.elements != 0; }
3306 void set_using_match() { using_match = true; }
3307 bool get_using_match() { return using_match; }
3308
3310 bool is_stmt_unsafe_with_mixed_mode() const {
3312 }
3313
3314 private:
3315 /**
3316 Enumeration listing special types of statements.
3317
3318 Currently, the only possible type is ROW_INJECTION.
3319 */
3321 /**
3322 The statement is a row injection (i.e., either a BINLOG
3323 statement or a row event executed by the slave SQL thread).
3324 */
3326
3327 /** The last element of this enumeration type. */
3329 };
3330
3331 /**
3332 Bit field indicating the type of statement.
3333
3334 There are two groups of bits:
3335
3336 - The low BINLOG_STMT_UNSAFE_COUNT bits indicate the types of
3337 unsafeness that the current statement has.
3338
3339 - The next BINLOG_STMT_TYPE_COUNT bits indicate if the statement
3340 is of some special type.
3341
3342 This must be a member of LEX, not of THD: each stored procedure
3343 needs to remember its unsafeness state between calls and each
3344 stored procedure has its own LEX object (but no own THD object).
3345 */
3347
3348 /**
3349 Bit field that determines the type of tables that are about to be
3350 be accessed while executing a statement.
3351 */
3354 /**
3355 It will be set true if 'MATCH () AGAINST' is used in the statement.
3356 */
3357 bool using_match;
3358
3359 /**
3360 This flag is set to true if statement is unsafe to be binlogged in STATEMENT
3361 format, when in MIXED mode.
3362 Currently this flag is set to true if stored program used in statement has
3363 CREATE/DROP temporary table operation(s) as sub-statement(s).
3364 */
3365 bool stmt_unsafe_with_mixed_mode{false};
3366};
3367
3368/*
3369 st_parsing_options contains the flags for constructions that are
3370 allowed in the current statement.
3372
3374 bool allows_variable;
3375 bool allows_select_into;
3376
3377 st_parsing_options() { reset(); }
3378 void reset();
3379};
3381/**
3382 The state of the lexical parser, when parsing comments.
3383*/
3385 /**
3386 Not parsing comments.
3387 */
3388 NO_COMMENT,
3389
3390 /**
3391 Parsing comments that need to be preserved.
3392 (Copy '/' '*' and '*' '/' sequences to the preprocessed buffer.)
3393 Typically, these are user comments '/' '*' ... '*' '/'.
3394 */
3396
3397 /**
3398 Parsing comments that need to be discarded.
3399 (Don't copy '/' '*' '!' and '*' '/' sequences to the preprocessed buffer.)
3400 Typically, these are special comments '/' '*' '!' ... '*' '/',
3401 or '/' '*' '!' 'M' 'M' 'm' 'm' 'm' ... '*' '/', where the comment
3402 markers should not be expanded.
3403 */
3405};
3406
3407/**
3408 This class represents the character input stream consumed during lexical
3409 analysis.
3410
3411 In addition to consuming the input stream, this class performs some comment
3412 pre processing, by filtering out out-of-bound special text from the query
3413 input stream.
3414
3415 Two buffers, with pointers inside each, are maintained in parallel. The
3416 'raw' buffer is the original query text, which may contain out-of-bound
3417 comments. The 'cpp' (for comments pre processor) is the pre-processed buffer
3418 that contains only the query text that should be seen once out-of-bound data
3419 is removed.
3420*/
3421
3422class Lex_input_stream {
3423 public:
3424 /**
3425 Constructor
3427 @param grammar_selector_token_arg See grammar_selector_token.
3428 */
3429
3430 explicit Lex_input_stream(uint grammar_selector_token_arg)
3431 : grammar_selector_token(grammar_selector_token_arg) {}
3432
3433 /**
3434 Object initializer. Must be called before usage.
3436 @retval false OK
3437 @retval true Error
3438 */
3439 bool init(THD *thd, const char *buff, size_t length);
3440
3441 void reset(const char *buff, size_t length);
3442
3443 /**
3444 Set the echo mode.
3445
3446 When echo is true, characters parsed from the raw input stream are
3447 preserved. When false, characters parsed are silently ignored.
3448 @param echo the echo mode.
3449 */
3450 void set_echo(bool echo) { m_echo = echo; }
3451
3452 void save_in_comment_state() {
3455 }
3456
3460 }
3461
3462 /**
3463 Skip binary from the input stream.
3464 @param n number of bytes to accept.
3465 */
3466 void skip_binary(int n) {
3467 assert(m_ptr + n <= m_end_of_query);
3468 if (m_echo) {
3469 memcpy(m_cpp_ptr, m_ptr, n);
3470 m_cpp_ptr += n;
3471 }
3472 m_ptr += n;
3473 }
3474
3475 /**
3476 Get a character, and advance in the stream.
3477 @return the next character to parse.
3478 */
3479 unsigned char yyGet() {
3480 assert(m_ptr <= m_end_of_query);
3481 const char c = *m_ptr++;
3482 if (m_echo) *m_cpp_ptr++ = c;
3483 return c;
3484 }
3485
3486 /**
3487 Get the last character accepted.
3488 @return the last character accepted.
3489 */
3490 unsigned char yyGetLast() const { return m_ptr[-1]; }
3492 /**
3493 Look at the next character to parse, but do not accept it.
3494 */
3495 unsigned char yyPeek() const {
3496 assert(m_ptr <= m_end_of_query);
3497 return m_ptr[0];
3498 }
3499
3500 /**
3501 Look ahead at some character to parse.
3502 @param n offset of the character to look up
3503 */
3504 unsigned char yyPeekn(int n) const {
3505 assert(m_ptr + n <= m_end_of_query);
3506 return m_ptr[n];
3507 }
3508
3509 /**
3510 Cancel the effect of the last yyGet() or yySkip().
3511 Note that the echo mode should not change between calls to yyGet / yySkip
3512 and yyUnget. The caller is responsible for ensuring that.
3513 */
3514 void yyUnget() {
3515 m_ptr--;
3516 if (m_echo) m_cpp_ptr--;
3517 }
3519 /**
3520 Accept a character, by advancing the input stream.
3521 */
3522 void yySkip() {
3523 assert(m_ptr <= m_end_of_query);
3524 if (m_echo)
3525 *m_cpp_ptr++ = *m_ptr++;
3526 else
3527 m_ptr++;
3528 }
3529
3530 /**
3531 Accept multiple characters at once.
3532 @param n the number of characters to accept.
3533 */
3534 void yySkipn(int n) {
3535 assert(m_ptr + n <= m_end_of_query);
3536 if (m_echo) {
3537 memcpy(m_cpp_ptr, m_ptr, n);
3538 m_cpp_ptr += n;
3539 }
3540 m_ptr += n;
3541 }
3542
3543 /**
3544 Puts a character back into the stream, canceling
3545 the effect of the last yyGet() or yySkip().
3546 Note that the echo mode should not change between calls
3547 to unput, get, or skip from the stream.
3548 */
3549 char *yyUnput(char ch) {
3550 *--m_ptr = ch;
3551 if (m_echo) m_cpp_ptr--;
3552 return m_ptr;
3553 }
3554
3555 /**
3556 Inject a character into the pre-processed stream.
3557
3558 Note, this function is used to inject a space instead of multi-character
3559 C-comment. Thus there is no boundary checks here (basically, we replace
3560 N-chars by 1-char here).
3561 */
3562 char *cpp_inject(char ch) {
3563 *m_cpp_ptr = ch;
3564 return ++m_cpp_ptr;
3565 }
3566
3567 /**
3568 End of file indicator for the query text to parse.
3569 @return true if there are no more characters to parse
3570 */
3571 bool eof() const { return (m_ptr >= m_end_of_query); }
3572
3573 /**
3574 End of file indicator for the query text to parse.
3575 @param n number of characters expected
3576 @return true if there are less than n characters to parse
3578 bool eof(int n) const { return ((m_ptr + n) >= m_end_of_query); }
3579
3580 /** Get the raw query buffer. */
3581 const char *get_buf() const { return m_buf; }
3582
3583 /** Get the pre-processed query buffer. */
3584 const char *get_cpp_buf() const { return m_cpp_buf; }
3585
3586 /** Get the end of the raw query buffer. */
3587 const char *get_end_of_query() const { return m_end_of_query; }
3588
3589 /** Mark the stream position as the start of a new token. */
3590 void start_token() {
3592 m_tok_end = m_ptr;
3593
3596 }
3597
3598 /**
3599 Adjust the starting position of the current token.
3600 This is used to compensate for starting whitespace.
3601 */
3602 void restart_token() {
3605 }
3606
3607 /** Get the token start position, in the raw buffer. */
3608 const char *get_tok_start() const { return m_tok_start; }
3609
3610 /** Get the token start position, in the pre-processed buffer. */
3611 const char *get_cpp_tok_start() const { return m_cpp_tok_start; }
3612
3613 /** Get the token end position, in the raw buffer. */
3614 const char *get_tok_end() const { return m_tok_end; }
3615
3616 /** Get the token end position, in the pre-processed buffer. */
3617 const char *get_cpp_tok_end() const { return m_cpp_tok_end; }
3618
3619 /** Get the current stream pointer, in the raw buffer. */
3620 const char *get_ptr() const { return m_ptr; }
3621
3622 /** Get the current stream pointer, in the pre-processed buffer. */
3623 const char *get_cpp_ptr() const { return m_cpp_ptr; }
3624
3625 /** Get the length of the current token, in the raw buffer. */
3626 uint yyLength() const {
3627 /*
3628 The assumption is that the lexical analyser is always 1 character ahead,
3629 which the -1 account for.
3630 */
3631 assert(m_ptr > m_tok_start);
3632 return (uint)((m_ptr - m_tok_start) - 1);
3633 }
3634
3635 /** Get the utf8-body string. */
3636 const char *get_body_utf8_str() const { return m_body_utf8; }
3637
3638 /** Get the utf8-body length. */
3643 void body_utf8_start(THD *thd, const char *begin_ptr);
3644 void body_utf8_append(const char *ptr);
3645 void body_utf8_append(const char *ptr, const char *end_ptr);
3647 const CHARSET_INFO *txt_cs,
3648 const char *end_ptr);
3649
3650 uint get_lineno(const char *raw_ptr) const;
3651
3652 /** Current thread. */
3653 THD *m_thd;
3654
3655 /** Current line number. */
3656 uint yylineno;
3657
3658 /** Length of the last token parsed. */
3659 uint yytoklen;
3660
3661 /** Interface with bison, value of the last token parsed. */
3663
3664 /**
3665 LALR(2) resolution, look ahead token.
3666 Value of the next token to return, if any,
3667 or -1, if no token was parsed in advance.
3668 Note: 0 is a legal token, and represents YYEOF.
3669 */
3670 int lookahead_token;
3671
3672 /** LALR(2) resolution, value of the look ahead token.*/
3674
3675 /// Skip adding of the current token's digest since it is already added
3676 ///
3677 /// Usually we calculate a digest token by token at the top-level function
3678 /// of the lexer: MYSQLlex(). However, some complex ("hintable") tokens break
3679 /// that data flow: for example, the `SELECT /*+ HINT(t) */` is the single
3680 /// token from the main parser's point of view, and we add the "SELECT"
3681 /// keyword to the digest buffer right after the lex_one_token() call,
3682 /// but the "/*+ HINT(t) */" is a sequence of separate tokens from the hint
3683 /// parser's point of view, and we add those tokens to the digest buffer
3684 /// *inside* the lex_one_token() call. Thus, the usual data flow adds
3685 /// tokens from the "/*+ HINT(t) */" string first, and only than it appends
3686 /// the "SELECT" keyword token to that stream: "/*+ HINT(t) */ SELECT".
3687 /// This is not acceptable, since we use the digest buffer to restore
3688 /// query strings in their normalized forms, so the order of added tokens is
3689 /// important. Thus, we add tokens of "hintable" keywords to a digest buffer
3690 /// right in the hint parser and skip adding of them at the caller with the
3691 /// help of skip_digest flag.
3693
3694 void add_digest_token(uint token, Lexer_yystype *yylval);
3695
3696 void reduce_digest_token(uint token_left, uint token_right);
3697
3698 /**
3699 True if this scanner tokenizes a partial query (partition expression,
3700 generated column expression etc.)
3701
3702 @return true if parsing a partial query, otherwise false.
3703 */
3704 bool is_partial_parser() const { return grammar_selector_token >= 0; }
3705
3706 /**
3707 Outputs warnings on deprecated charsets in complete SQL statements
3709 @param [in] cs The character set/collation to check for a deprecation.
3710 @param [in] alias The name/alias of @p cs.
3711 */
3713 const char *alias) const {
3714 if (!is_partial_parser()) {
3716 }
3717 }
3718
3719 /**
3720 Outputs warnings on deprecated collations in complete SQL statements
3721
3722 @param [in] collation The collation to check for a deprecation.
3723 */
3725 if (!is_partial_parser()) {
3727 }
3728 }
3729
3731
3732 private:
3733 /** Pointer to the current position in the raw input stream. */
3734 char *m_ptr;
3735
3736 /** Starting position of the last token parsed, in the raw buffer. */
3737 const char *m_tok_start;
3738
3739 /** Ending position of the previous token parsed, in the raw buffer. */
3740 const char *m_tok_end;
3741
3742 /** End of the query text in the input stream, in the raw buffer. */
3743 const char *m_end_of_query;
3744
3745 /** Beginning of the query text in the input stream, in the raw buffer. */
3746 const char *m_buf;
3747
3748 /** Length of the raw buffer. */
3749 size_t m_buf_length;
3750
3751 /** Echo the parsed stream to the pre-processed buffer. */
3752 bool m_echo;
3753 bool m_echo_saved;
3754
3755 /** Pre-processed buffer. */
3756 char *m_cpp_buf;
3757
3758 /** Pointer to the current position in the pre-processed input stream. */
3759 char *m_cpp_ptr;
3760
3761 /**
3762 Starting position of the last token parsed,
3763 in the pre-processed buffer.
3764 */
3765 const char *m_cpp_tok_start;
3766
3767 /**
3768 Ending position of the previous token parsed,
3769 in the pre-processed buffer.
3770 */
3771 const char *m_cpp_tok_end;
3772
3773 /** UTF8-body buffer created during parsing. */
3774 char *m_body_utf8;
3775
3776 /** Pointer to the current position in the UTF8-body buffer. */
3777 char *m_body_utf8_ptr;
3778
3779 /**
3780 Position in the pre-processed buffer. The query from m_cpp_buf to
3781 m_cpp_utf_processed_ptr is converted to UTF8-body.
3782 */
3783 const char *m_cpp_utf8_processed_ptr;
3784
3785 public:
3786 /** Current state of the lexical analyser. */
3788
3789 /**
3790 Position of ';' in the stream, to delimit multiple queries.
3791 This delimiter is in the raw buffer.
3792 */
3793 const char *found_semicolon;
3794
3795 /** Token character bitmaps, to detect 7bit strings. */
3797
3798 /** SQL_MODE = IGNORE_SPACE. */
3799 bool ignore_space;
3800
3801 /**
3802 true if we're parsing a prepared statement: in this mode
3803 we should allow placeholders.
3804 */
3805 bool stmt_prepare_mode;
3806 /**
3807 true if we should allow multi-statements.
3808 */
3809 bool multi_statements;
3810
3811 /** State of the lexical analyser for comments. */
3814
3815 /**
3816 Starting position of the TEXT_STRING or IDENT in the pre-processed
3817 buffer.
3818
3819 NOTE: this member must be used within MYSQLlex() function only.
3820 */
3821 const char *m_cpp_text_start;
3822
3823 /**
3824 Ending position of the TEXT_STRING or IDENT in the pre-processed
3825 buffer.
3826
3827 NOTE: this member must be used within MYSQLlex() function only.
3828 */
3829 const char *m_cpp_text_end;
3830
3831 /**
3832 Character set specified by the character-set-introducer.
3833
3834 NOTE: this member must be used within MYSQLlex() function only.
3835 */
3837
3838 /**
3839 Current statement digest instrumentation.
3840 */
3842
3843 /**
3844 The synthetic 1st token to prepend token stream with.
3845
3846 This token value tricks parser to simulate multiple %start-ing points.
3847 Currently the grammar is aware of 4 such synthetic tokens:
3848 1. GRAMMAR_SELECTOR_PART for partitioning stuff from DD,
3849 2. GRAMMAR_SELECTOR_GCOL for generated column stuff from DD,
3850 3. GRAMMAR_SELECTOR_EXPR for generic single expressions from DD/.frm.
3851 4. GRAMMAR_SELECTOR_CTE for generic subquery expressions from CTEs.
3852 5. -1 when parsing with the main grammar (no grammar selector available).
3853
3854 @note yylex() is expected to return the value of type int:
3855 0 is for EOF and everything else for real token numbers.
3856 Bison, in its turn, generates positive token numbers.
3857 So, the negative grammar_selector_token means "not a token".
3858 In other words, -1 is "empty value".
3859 */
3860 const int grammar_selector_token;
3862 bool text_string_is_7bit() const { return !(tok_bitmap & 0x80); }
3866 public:
3867 String column;
3869 LEX_COLUMN(const String &x, const Access_bitmask &y) : column(x), rights(y) {}
3870};
3871
3872enum class role_enum;
3874/*
3875 This structure holds information about grantor's context
3876*/
3877class LEX_GRANT_AS {
3878 public:
3880 void cleanup();
3882 public:
3883 bool grant_as_used;
3885 LEX_USER *user;
3887};
3888
3889/*
3890 Some queries can be executed only using the secondary engine. The enum
3891 "execute_only_in_secondary_reasons" retains the explanations for queries that
3892 cannot be executed using the primary engine.
3896 CUBE,
3899};
3900
3902 Some queries can be executed only in using the hypergraph optimizer. The enum
3903 "execute_only_in_hypergraph_reasons" retains the explanations for the same.
3908};
3909
3910/**
3911 The LEX object currently serves three different purposes:
3912
3913 - It contains some universal properties of an SQL command, such as
3914 sql_command, presence of IGNORE in data change statement syntax, and list
3915 of tables (query_tables).
3916
3917 - It contains some execution state variables, like m_exec_started
3918 (set to true when execution is started), plugins (list of plugins used
3919 by statement), insert_update_values_map (a map of objects used by certain
3920 INSERT statements), etc.
3921
3922 - It contains a number of members that should be local to subclasses of
3923 Sql_cmd, like purge_value_list (for the PURGE command), kill_value_list
3924 (for the KILL command).
3925
3926 The LEX object is strictly a part of class Sql_cmd, for those SQL commands
3927 that are represented by an Sql_cmd class. For the remaining SQL commands,
3928 it is a standalone object linked to the current THD.
3929
3930 The lifecycle of a LEX object is as follows:
3931
3932 - The LEX object is constructed either on the execution mem_root
3933 (for regular statements), on a Prepared_statement mem_root (for
3934 prepared statements), on an SP mem_root (for stored procedure instructions),
3935 or created on the current mem_root for short-lived uses.
3936
3937 - Call lex_start() to initialize a LEX object before use.
3938 This initializes the execution state part of the object.
3939 It also calls LEX::reset() to ensure that all members are properly inited.
3940
3941 - Parse and resolve the statement, using the LEX as a work area.
3942
3943 - Execute an SQL command: call set_exec_started() when starting to execute
3944 (actually when starting to optimize).
3945 Typically call is_exec_started() to distinguish between preparation
3946 and optimization/execution stages of SQL command execution.
3947
3948 - Call clear_execution() when execution is finished. This will clear all
3949 execution state associated with the SQL command, it also includes calling
3950 LEX::reset_exec_started().
3951
3952 @todo - Create subclasses of Sql_cmd to contain data that are local
3953 to specific commands.
3954
3955 @todo - Create a Statement context object that will hold the execution state
3956 part of struct LEX.
3957
3958 @todo - Ensure that a LEX struct is never reused, thus making e.g
3959 LEX::reset() redundant.
3960*/
3962struct LEX : public Query_tables_list {
3963 friend bool lex_start(THD *thd);
3965 Query_expression *unit; ///< Outer-most query expression
3966 /// @todo: query_block can be replaced with unit->first-select()
3967 Query_block *query_block; ///< First query block
3968 Query_block *all_query_blocks_list; ///< List of all query blocks
3969 private:
3970 /* current Query_block in parsing */
3972
3974 Some queries can only be executed on a secondary engine, for example,
3975 queries with non-primitive grouping like CUBE.
3976 */
3978
3981
3983 Some queries can only be executed in hypergraph optimizer, for example,
3984 queries with QUALIFY clause.
3985 */
3990 bool m_splitting_window_expression = false;
3991
3992 public:
3993 inline Query_block *current_query_block() const {
3994 return m_current_query_block;
3995 }
3996
3997 /*
3998 We want to keep current_thd out of header files, so the debug assert
3999 is moved to the .cc file.
4000 */
4002 inline void set_current_query_block(Query_block *select) {
4003#ifndef NDEBUG
4005#endif
4007 }
4008 /// @return true if this is an EXPLAIN statement
4009 bool is_explain() const { return explain_format != nullptr; }
4010 bool is_explain_analyze = false;
4011
4012 /**
4013 Whether the currently-running statement should be prepared and executed
4014 with the hypergraph optimizer. This will not change after the statement is
4015 prepared, so you can use it in any optimization phase to e.g. figure out
4016 whether to inhibit some transformation that the hypergraph optimizer
4017 does not properly understand yet. If a different optimizer is requested,
4018 the statement must be re-prepared with the proper optimizer settings.
4019 */
4022 }
4023
4024 void set_using_hypergraph_optimizer(bool use_hypergraph) {
4026 }
4028 /// RAII class to set state \c m_splitting_window_expression for a scope
4030 private:
4031 LEX *m_lex{nullptr};
4032
4033 public:
4034 explicit Splitting_window_expression(LEX *lex, bool v) {
4035 m_lex = lex;
4037 }
4040 }
4041 };
4042
4045 }
4046
4047 void set_splitting_window_expression(bool v) {
4049 }
4050
4051 private:
4054 public:
4057 char *to_log; /* For PURGE BINARY LOGS TO */
4059 // Widcard from SHOW ... LIKE <wildcard> statements.
4063 nullptr, 0}; ///< Argument of the BINLOG event statement.
4070 THD *thd;
4071
4072 /* Optimizer hints */
4075 /* maintain a list of used plugins for this LEX */
4080 /// Table being inserted into (may be a view)
4082 /// Leaf table being inserted into (always a base table)
4084
4085 /** SELECT of CREATE VIEW statement */
4087
4088 /* Partition info structure filled in by PARTITION BY parse part */
4090
4092 The definer of the object being created (view, trigger, stored routine).
4093 I.e. the value of DEFINER clause.
4103
4104 // PURGE statement-specific fields:
4106
4107 // KILL statement-specific fields:
4109
4110 // other stuff:
4112 List<Item_func_set_user_var> set_var_list; // in-query assignment list
4113 /**
4114 List of placeholders ('?') for parameters of a prepared statement. Because
4115 we append to this list during parsing, it is naturally sorted by
4116 position of the '?' in the query string. The code which fills placeholders
4117 with user-supplied values, and the code which writes a query for
4118 statement-based logging, rely on this order.
4119 This list contains only real placeholders, not the clones which originate
4120 in a re-parsed CTE definition.
4121 */
4123
4125
4126 void insert_values_map(Item_field *f1, Field *f2) {
4128 insert_update_values_map = new std::map<Item_field *, Field *>;
4129 insert_update_values_map->insert(std::make_pair(f1, f2));
4130 }
4131 void destroy_values_map() {
4133 insert_update_values_map->clear();
4135 insert_update_values_map = nullptr;
4136 }
4137 }
4138 void clear_values_map() {
4141 }
4142 }
4143
4145 return result != nullptr && result->export_result_to_object_storage();
4146 }
4147
4148 bool has_values_map() const { return insert_update_values_map != nullptr; }
4149 std::map<Item_field *, Field *>::iterator begin_values_map() {
4150 return insert_update_values_map->begin();
4152 std::map<Item_field *, Field *>::iterator end_values_map() {
4153 return insert_update_values_map->end();
4157 }
4159 const bool execute_only_in_secondary_engine_param,
4162 execute_only_in_secondary_engine_param;
4165 reason == SUPPORTED_IN_PRIMARY);
4166 }
4167
4169 const {
4171 }
4172
4176 case CUBE:
4177 return "CUBE";
4178 case TABLESAMPLE:
4179 return "TABLESAMPLE";
4181 return "OUTFILE to object store";
4182 default:
4183 return "UNDEFINED";
4184 }
4188 }
4190 bool execute_in_hypergraph_optimizer_param,
4193 execute_in_hypergraph_optimizer_param;
4195 }
4196
4200 ? "QUALIFY clause"
4201 : "UNDEFINED";
4202 }
4203
4205 const {
4207 }
4208
4209 private:
4210 /*
4211 With Visual Studio, an std::map will always allocate two small objects
4212 on the heap. Sometimes we put LEX objects in a MEM_ROOT, and never run
4213 the LEX DTOR. To avoid memory leaks, put this std::map on the heap,
4214 and call clear_values_map() at the end of each statement.
4215 */
4216 std::map<Item_field *, Field *> *insert_update_values_map;
4217
4218 public:
4219 /*
4220 A stack of name resolution contexts for the query. This stack is used
4221 at parse time to set local name resolution contexts for various parts
4222 of a query. For example, in a JOIN ... ON (some_condition) clause the
4223 Items in 'some_condition' must be resolved only against the operands
4224 of the the join, and not against the whole clause. Similarly, Items in
4225 subqueries should be resolved against the subqueries (and outer queries).
4226 The stack is used in the following way: when the parser detects that
4227 all Items in some clause need a local context, it creates a new context
4228 and pushes it on the stack. All newly created Items always store the
4229 top-most context in the stack. Once the parser leaves the clause that
4230 required a local context, the parser pops the top-most context.
4236 HA_CHECK_OPT check_opt; // check/repair options
4239 LEX_SOURCE_INFO mi; // used by CHANGE REPLICATION SOURCE
4244 ulong type;
4245 /**
4246 This field is used as a work field during resolving to validate
4247 the use of aggregate functions. For example in a query
4248 SELECT ... FROM ...WHERE MIN(i) == 1 GROUP BY ... HAVING MIN(i) > 2
4249 MIN(i) in the WHERE clause is not allowed since only non-aggregated data
4250 is present, whereas MIN(i) in the HAVING clause is allowed because HAVING
4251 operates on the output of a grouping operation.
4252 Each query block is assigned a nesting level. This field is a bit field
4253 that contains the value one in the position of that nesting level if
4254 aggregate functions are allowed for that query block.
4255 */
4257 /**
4258 Windowing functions are not allowed in HAVING - in contrast to grouped
4259 aggregate functions, since windowing in SQL logically follows after all
4260 grouping operations. Nor are they allowed inside grouped aggregate
4261 function arguments. One bit per query block, as also \c allow_sum_func. For
4262 ORDER BY and QUALIFY predicates, window functions \em are allowed unless
4263 they are contained in arguments of a grouped aggregate function. Nor are
4264 references to outer window functions (via alias) allowed in subqueries, but
4265 that is checked separately.
4266 */
4269 /// If true: during prepare, we did a subquery transformation (IN-to-EXISTS,
4270 /// SOME/ANY) that doesn't currently work for subquery to a derived table
4271 /// transformation.
4273
4275
4276 /*
4277 Usually `expr` rule of yacc is quite reused but some commands better
4278 not support subqueries which comes standard with this rule, like
4279 KILL, HA_READ, CREATE/ALTER EVENT etc. Set this to `false` to get
4280 syntax error back.
4281 */
4282 bool expr_allows_subquery{true};
4283 /**
4284 If currently re-parsing a CTE's definition, this is the offset in bytes
4285 of that definition in the original statement which had the WITH
4286 clause. Otherwise this is 0.
4287 */
4289 /**
4290 If currently re-parsing a condition which is pushed down to a derived
4291 table, this will be set to true.
4292 */
4294 /**
4295 If currently re-parsing a condition that is being pushed down to a
4296 derived table, this has the positions of all the parameters that are
4297 part of that condition in the original statement. Otherwise it is empty.
4301 enum SSL_type ssl_type; /* defined in violite.h */
4307 /// QUERY ID for SHOW PROFILE
4309 uint profile_options;
4310 uint grant, grant_tot_col;
4311 /**
4312 Set to true when GRANT ... GRANT OPTION ... TO ...
4313 is used (vs. GRANT ... WITH GRANT OPTION).
4314 The flag is used by @ref mysql_grant to grant GRANT OPTION (@ref GRANT_ACL)
4315 to all dynamic privileges.
4319 int select_number; ///< Number of query block (by EXPLAIN)
4322 /**
4323 @todo ensure that correct CONTEXT_ANALYSIS_ONLY is set for all preparation
4324 code, so we can fully rely on this field.
4325 */
4327 bool drop_if_exists;
4328 /**
4329 refers to optional IF EXISTS clause in REVOKE sql. This flag when set to
4330 true will report warnings in case privilege being granted is not granted to
4331 given user/role. When set to false error is reported.
4332 */
4333 bool grant_if_exists;
4334 /**
4335 refers to optional IGNORE UNKNOWN USER clause in REVOKE sql. This flag when
4336 set to true will report warnings in case target user/role for which
4337 privilege being granted does not exists. When set to false error is
4338 reported.
4342 bool autocommit;
4344 // For show commands to show hidden columns and indexes.
4345 bool m_extended_show;
4346
4347 enum enum_yes_no_unknown tx_chain, tx_release;
4348
4349 /**
4350 Whether this query will return the same answer every time, given unchanged
4351 data. Used to be for the query cache, but is now used to find out if an
4352 expression is usable for partitioning.
4353 */
4356 private:
4357 /// True if statement references UDF functions
4358 bool m_has_udf{false};
4359 bool ignore;
4360 /// True if query has at least one external table
4363 public:
4364 bool is_ignore() const { return ignore; }
4365 void set_ignore(bool ignore_param) { ignore = ignore_param; }
4366 void set_has_udf() { m_has_udf = true; }
4367 bool has_udf() const { return m_has_udf; }
4373 /* Prepared statements SQL syntax:*/
4374 LEX_CSTRING prepared_stmt_name; /* Statement name (in all queries) */
4376 Prepared statement query text or name of variable that holds the
4377 prepared statement (in PREPARE ... queries)
4378 */
4380 /* If true, prepared_stmt_code is a name of variable that holds the query */
4382 /* Names of user variables holding parameters (in EXECUTE) */
4386 bool sp_lex_in_use; /* Keep track on lex usage in SPs for error handling */
4387 bool all_privileges;
4391
4392 private:
4393 bool m_broken; ///< see mark_broken()
4394 /**
4395 Set to true when execution has started (after parsing, tables opened and
4396 query preparation is complete. Used to track arena state for SPs).
4397 */
4398 bool m_exec_started;
4399 /**
4400 Set to true when execution is completed, ie optimization has been done
4401 and execution is successful or ended in error.
4402 */
4404 /**
4405 Set to true when execution crosses global_connection_memory_status_limit.
4406 */
4408 /**
4409 Set to true when execution crosses connection_memory_status_limit.
4410 */
4412 /**
4413 Current SP parsing context.
4414 @see also sp_head::m_root_parsing_ctx.
4415 */
4418 /**
4419 Statement context for Query_block::make_active_options.
4420 */
4422
4423 public:
4424 /**
4425 Gets the options that have been set for this statement. The options are
4426 propagated to the Query_block objects and should usually be read with
4427 #Query_block::active_options().
4428
4429 @return a bit set of options set for this statement
4430 */
4432 /**
4433 Add options to values of m_statement_options. options is an ORed
4434 bit set of options defined in query_options.h
4436 @param options Add this set of options to the set already in
4437 m_statement_options
4441 }
4442 bool is_broken() const { return m_broken; }
4443 /**
4444 Certain permanent transformations (like in2exists), if they fail, may
4445 leave the LEX in an inconsistent state. They should call the
4446 following function, so that this LEX is not reused by another execution.
4448 @todo If lex_start () were a member function of LEX, the "broken"
4449 argument could always be "true" and thus could be removed.
4450 */
4451 void mark_broken(bool broken = true) {
4452 if (broken) {
4453 /*
4454 "OPEN <cursor>" cannot be re-prepared if the cursor uses no tables
4455 ("SELECT FROM DUAL"). Indeed in that case cursor_query is left empty
4456 in constructions of sp_instr_cpush, and thus
4457 sp_lex_instr::parse_expr() cannot re-prepare. So we mark the statement
4458 as broken only if tables are used.
4459 */
4460 if (is_metadata_used()) m_broken = true;
4461 } else
4462 m_broken = false;
4464
4466
4467 void cleanup(bool full) {
4468 unit->cleanup(full);
4469 if (full) {
4474
4475 bool is_exec_started() const { return m_exec_started; }
4476 void set_exec_started() { m_exec_started = true; }
4477 void reset_exec_started() {
4478 m_exec_started = false;
4479 m_exec_completed = false;
4480 }
4481 /**
4482 Check whether the statement has been executed (regardless of completion -
4483 successful or in error).
4484 Check this instead of Query_expression::is_executed() to determine
4485 the state of a complete statement.
4487 bool is_exec_completed() const { return m_exec_completed; }
4488 void set_exec_completed() { m_exec_completed = true; }
4501 }
4505 }
4507
4511
4512 /// Check if the current statement uses meta-data (uses a table or a stored
4513 /// routine).
4514 bool is_metadata_used() const {
4515 return query_tables != nullptr || has_udf() ||
4516 (sroutines != nullptr && !sroutines->empty());
4517 }
4518
4519 /// We have detected the presence of an alias of a window function with a
4520 /// window on query block qb. Check if the reference is illegal at this point
4521 /// during resolution.
4522 /// @param qb The query block of the window function
4523 /// @return true if window function is referenced from another query block
4524 /// than its window, or if window functions are disallowed at the current
4525 /// point during prepare, cf. also documentation of \c m_deny_window_func.
4526 bool deny_window_function(Query_block *qb) const {
4527 return qb != current_query_block() ||
4528 ((~allow_sum_func | m_deny_window_func) >>
4530 0x1;
4531 }
4533 public:
4535
4536 bool only_view; /* used for SHOW CREATE TABLE/VIEW */
4537 /*
4538 view created to be run from definer (standard behaviour)
4539 */
4541
4542 /**
4543 Intended to point to the next word after DEFINER-clause in the
4544 following statements:
4545
4546 - CREATE TRIGGER (points to "TRIGGER");
4547 - CREATE PROCEDURE (points to "PROCEDURE");
4548 - CREATE FUNCTION (points to "FUNCTION" or "AGGREGATE");
4549 - CREATE EVENT (points to "EVENT")
4551 This pointer is required to add possibly omitted DEFINER-clause to the
4552 DDL-statement before dumping it to the binlog.
4553 */
4554 const char *stmt_definition_begin;
4555 const char *stmt_definition_end;
4556
4557 /**
4558 During name resolution search only in the table list given by
4559 Name_resolution_context::first_name_resolution_table and
4560 Name_resolution_context::last_name_resolution_table
4561 (see Item_field::fix_fields()).
4562 */
4564
4565 bool is_lex_started; /* If lex_start() did run. For debugging. */
4566 /// Set to true while resolving values in ON DUPLICATE KEY UPDATE clause
4569 class Explain_format *explain_format{nullptr};
4570
4571 // Maximum execution time for a statement.
4572 ulong max_execution_time;
4573
4575 To flag the current statement as dependent for binary logging
4576 on explicit_defaults_for_timestamp
4577 */
4579
4580 /**
4581 Used to inform the parser whether it should contextualize the parse
4582 tree. When we get a pure parser this will not be needed.
4583 */
4584 bool will_contextualize;
4585
4586 LEX();
4588 virtual ~LEX();
4589
4590 /// Destroy contained objects, but not the LEX object itself.
4591 void destroy() {
4592 if (unit == nullptr) return;
4593 unit->destroy();
4594 unit = nullptr;
4595 query_block = nullptr;
4596 all_query_blocks_list = nullptr;
4597 m_current_query_block = nullptr;
4598 explain_format = nullptr;
4600 }
4601
4602 /// Reset query context to initial state
4603 void reset();
4604
4605 /// Create an empty query block within this LEX object.
4607
4608 /// Create query expression object that contains one query block.
4609 Query_block *new_query(Query_block *curr_query_block);
4610
4611 /// Create query block and attach it to the current query expression.
4613
4614 /// Create top-level query expression and query block.
4615 bool new_top_level_query();
4616
4617 /// Create query expression and query block in existing memory objects.
4618 void new_static_query(Query_expression *sel_query_expression,
4619 Query_block *select);
4620
4621 /// Create query expression under current_query_block and a query block under
4622 /// the new query expression. The new query expression is linked in under
4623 /// current_query_block. The new query block is linked in under the new
4624 /// query expression.
4625 ///
4626 /// @param thd current session context
4627 /// @param current_query_block the root under which we create the new
4628 /// expression
4629 /// and block
4630 /// @param where_clause any where clause for the block
4631 /// @param having_clause any having clause for the block
4632 /// @param ctx the parsing context
4633 ///
4634 /// @returns the new query expression, or nullptr on error.
4636 THD *thd, Query_block *current_query_block, Item *where_clause,
4637 Item *having_clause, enum_parsing_context ctx);
4638
4639 inline bool is_ps_or_view_context_analysis() {
4642 }
4643
4644 inline bool is_view_context_analysis() {
4646 }
4647
4648 void clear_execution();
4649
4650 /**
4651 Set the current query as uncacheable.
4652
4653 @param curr_query_block Current select query block
4654 @param cause Why this query is uncacheable.
4655
4656 @details
4657 All query blocks representing subqueries, from the current one up to
4658 the outer-most one, but excluding the main query block, are also set
4659 as uncacheable.
4660 */
4661 void set_uncacheable(Query_block *curr_query_block, uint8 cause) {
4662 safe_to_cache_query = false;
4663
4664 if (m_current_query_block == nullptr) return;
4665 Query_block *sl;
4666 Query_expression *un;
4667 for (sl = curr_query_block, un = sl->master_query_expression(); un != unit;
4668 sl = sl->outer_query_block(), un = sl->master_query_expression()) {
4669 sl->uncacheable |= cause;
4670 un->uncacheable |= cause;
4671 }
4672 }
4674
4675 Table_ref *unlink_first_table(bool *link_to_local);
4676 void link_first_table_back(Table_ref *first, bool link_to_local);
4678
4680
4682 for (Table_ref *tr = insert_table->first_leaf_table(); tr != nullptr;
4683 tr = tr->next_leaf)
4684 tr->restore_properties();
4685 }
4686
4688
4689 bool can_use_merged();
4690 bool can_not_use_merged();
4691 bool need_correct_ident();
4692 /*
4693 Is this update command where 'WHITH CHECK OPTION' clause is important
4694
4695 SYNOPSIS
4696 LEX::which_check_option_applicable()
4697
4698 RETURN
4699 true have to take 'WHITH CHECK OPTION' clause into account
4700 false 'WHITH CHECK OPTION' clause do not need
4701 */
4702 inline bool which_check_option_applicable() {
4703 switch (sql_command) {
4704 case SQLCOM_UPDATE:
4706 case SQLCOM_INSERT:
4708 case SQLCOM_REPLACE:
4710 case SQLCOM_LOAD:
4711 return true;
4712 default:
4713 return false;
4714 }
4716
4718
4720 return context_stack.push_front(context);
4721 }
4722
4723 void pop_context() { context_stack.pop(); }
4724
4725 bool copy_db_to(char const **p_db, size_t *p_db_length) const;
4726
4727 bool copy_db_to(char **p_db, size_t *p_db_length) const {
4728 return copy_db_to(const_cast<const char **>(p_db), p_db_length);
4729 }
4730
4732
4735
4736 bool table_or_sp_used();
4737
4738 /**
4739 @brief check if the statement is a single-level join
4740 @return result of the check
4741 @retval true The statement doesn't contain subqueries, unions and
4742 stored procedure calls.
4743 @retval false There are subqueries, UNIONs or stored procedure calls.
4744 */
4745 bool is_single_level_stmt() {
4746 /*
4747 This check exploits the fact that the last added to all_select_list is
4748 on its top. So query_block (as the first added) will be at the tail
4749 of the list.
4750 */
4752 (sroutines == nullptr || sroutines->empty())) {
4754 return true;
4755 }
4756 return false;
4757 }
4758
4759 void release_plugins();
4760
4761 /**
4762 IS schema queries read some dynamic table statistics from SE.
4763 These statistics are cached, to avoid opening of table more
4764 than once while preparing a single output record buffer.
4765 */
4768
4769 bool accept(Select_lex_visitor *visitor);
4770
4771 bool set_wild(LEX_STRING);
4772 void clear_privileges();
4773
4774 bool make_sql_cmd(Parse_tree_root *parse_tree);
4775
4776 private:
4777 /**
4778 Context object used by secondary storage engines to store query
4779 state during optimization and execution.
4780 */
4782
4783 public:
4784 /**
4785 Gets the secondary engine execution context for this statement.
4786 */
4788 const {
4790 }
4791
4792 /**
4793 Sets the secondary engine execution context for this statement.
4794 The old context object is destroyed, if there is one. Can be set
4795 to nullptr to destroy the old context object and clear the
4796 pointer.
4797
4798 The supplied context object should be allocated on the execution
4799 MEM_ROOT, so that its memory doesn't have to be manually freed
4800 after query execution.
4801 */
4804