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