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