MySQL 8.1.0
Source Code Documentation
sql_lex.h
Go to the documentation of this file.
1/* Copyright (c) 2000, 2023, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23/**
24 @defgroup GROUP_PARSER Parser
25 @{
26*/
27
28#ifndef SQL_LEX_INCLUDED
29#define SQL_LEX_INCLUDED
30
31#include <string.h>
32#include <sys/types.h> // TODO: replace with cstdint
33
34#include <algorithm>
35#include <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 "map_helpers.h"
45#include "mem_root_deque.h"
46#include "memory_debugging.h"
47#include "my_alloc.h" // Destroy_only
48#include "my_base.h"
49#include "my_compiler.h"
50#include "my_dbug.h"
51#include "my_inttypes.h" // TODO: replace with cstdint
52#include "my_sqlcommand.h"
53#include "my_sys.h"
54#include "my_table_map.h"
55#include "my_thread_local.h"
57#include "mysql/service_mysql_alloc.h" // my_free
59#include "mysql_com.h"
60#include "mysqld_error.h"
61#include "prealloced_array.h" // Prealloced_array
62#include "sql/dd/info_schema/table_stats.h" // dd::info_schema::Table_stati...
63#include "sql/dd/info_schema/tablespace_stats.h" // dd::info_schema::Tablesp...
64#include "sql/enum_query_type.h"
65#include "sql/handler.h"
66#include "sql/item.h" // Name_resolution_context
67#include "sql/item_subselect.h" // Subquery_strategy
70#include "sql/key_spec.h" // KEY_CREATE_INFO
71#include "sql/mdl.h"
72#include "sql/mem_root_array.h" // Mem_root_array
73#include "sql/parse_location.h"
74#include "sql/parse_tree_node_base.h" // enum_parsing_context
75#include "sql/parser_yystype.h"
76#include "sql/query_options.h" // OPTION_NO_CONST_TABLES
77#include "sql/query_term.h"
78#include "sql/set_var.h"
79#include "sql/sql_array.h"
80#include "sql/sql_connect.h" // USER_RESOURCES
81#include "sql/sql_const.h"
82#include "sql/sql_data_change.h" // enum_duplicates
83#include "sql/sql_error.h" // warn_on_deprecated_charset
84#include "sql/sql_list.h"
85#include "sql/sql_plugin_ref.h"
86#include "sql/sql_servers.h" // Server_options
87#include "sql/sql_udf.h" // Item_udftype
88#include "sql/table.h" // Table_ref
89#include "sql/thr_malloc.h"
90#include "sql/trigger_def.h" // enum_trigger_action_time_type
91#include "sql/visible_fields.h"
92#include "sql_string.h"
93#include "string_with_len.h"
94#include "strings/sql_chars.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
996 @param term the term on behalf of which we are making a post processing
997 block
998 @returns a query block
999 */
1001
1002 bool prepare_query_term(THD *thd, Query_term *qts,
1003 Query_result *common_result, ulonglong added_options,
1004 ulonglong create_options, int level,
1005 Mem_root_array<bool> &nullable);
1007 assert(!is_prepared());
1008 prepared = true;
1009 }
1011 assert(is_prepared() && !is_optimized());
1012 optimized = true;
1013 }
1015 // assert(is_prepared() && is_optimized() && !is_executed());
1016 assert(is_prepared() && is_optimized());
1017 executed = true;
1018 }
1019 /// Reset this query expression for repeated evaluation within same execution
1021 assert(is_prepared() && is_optimized());
1022 executed = false;
1023 }
1024 /// Clear execution state, needed before new execution of prepared statement
1026 // Cannot be enforced when called from Prepared_statement::execute():
1027 // assert(is_prepared());
1028 optimized = false;
1029 executed = false;
1030 cleaned = UC_DIRTY;
1031 }
1032 /// Check state of preparation of the contained query expression.
1033 bool is_prepared() const { return prepared; }
1034 /// Check state of optimization of the contained query expression.
1035 bool is_optimized() const { return optimized; }
1036 /**
1037 Check state of execution of the contained query expression.
1038 Should not be used to check the state of a complete statement, use
1039 LEX::is_exec_completed() instead.
1040 */
1041 bool is_executed() const { return executed; }
1043 Query_result_interceptor *old_result);
1044 bool set_limit(THD *thd, Query_block *provider);
1045 bool has_any_limit() const;
1046
1047 inline bool is_union() const;
1048 inline bool is_set_operation() const;
1049
1050 /// Include a query expression below a query block.
1051 void include_down(LEX *lex, Query_block *outer);
1052
1053 /// Exclude this unit and immediately contained query_block objects
1054 void exclude_level();
1055
1056 /// Exclude subtree of current unit from tree of SELECTs
1057 void exclude_tree();
1058
1059 /// Renumber query blocks of a query expression according to supplied LEX
1060 void renumber_selects(LEX *lex);
1061
1063 bool save_cmd_properties(THD *thd);
1064
1065 friend class Query_block;
1066
1069 size_t num_visible_fields() const;
1070
1071 // If we are doing a query with global LIMIT, we need somewhere to store the
1072 // record count for FOUND_ROWS(). It can't be in any of the JOINs, since
1073 // they may have their own LimitOffsetIterators, which will write to
1074 // join->send_records whenever there is an OFFSET. Thus, we'll keep it here
1075 // instead.
1077
1080 void set_explain_marker_from(THD *thd, const Query_expression *u);
1081
1082#ifndef NDEBUG
1083 /**
1084 Asserts that none of {this unit and its children units} is fully cleaned
1085 up.
1086 */
1088#else
1089 void assert_not_fully_clean() {}
1090#endif
1091 void invalidate();
1092
1093 bool is_recursive() const { return first_recursive != nullptr; }
1094
1096
1098
1099 void fix_after_pullout(Query_block *parent_query_block,
1100 Query_block *removed_query_block);
1101
1102 /**
1103 If unit is a subquery, which forms an object of the upper level (an
1104 Item_subselect, a derived Table_ref), adds to this object a map
1105 of tables of the upper level which the unit references.
1106 */
1108
1109 /**
1110 If unit is a subquery, which forms an object of the upper level (an
1111 Item_subselect, a derived Table_ref), returns the place of this object
1112 in the upper level query block.
1113 */
1115
1116 bool walk(Item_processor processor, enum_walk walk, uchar *arg);
1117
1118 /**
1119 Replace all targeted items using transformer provided and info in
1120 arg.
1121 */
1122 bool replace_items(Item_transformer t, uchar *arg);
1123
1124 /*
1125 An exception: this is the only function that needs to adjust
1126 explain_marker.
1127 */
1128 friend bool parse_view_definition(THD *thd, Table_ref *view_ref);
1129};
1130
1133
1134/**
1135 Query_block type enum
1136*/
1138 EXPLAIN_NONE = 0,
1151 // Total:
1152 EXPLAIN_total ///< fake type, total number of all valid types
1153
1154 // Don't insert new types below this line!
1155};
1156
1157/**
1158 This class represents a query block, aka a query specification, which is
1159 a query consisting of a SELECT keyword, followed by a table list,
1160 optionally followed by a WHERE clause, a GROUP BY, etc.
1161*/
1162class Query_block : public Query_term {
1163 public:
1164 /**
1165 @note the group_by and order_by lists below will probably be added to the
1166 constructor when the parser is converted into a true bottom-up design.
1167
1168 //SQL_I_LIST<ORDER> *group_by, SQL_I_LIST<ORDER> order_by
1169 */
1171
1172 /// Query_term methods overridden
1173 void debugPrint(int level, std::ostringstream &buf) const override;
1174 /// Minion of debugPrint
1175 void qbPrint(int level, std::ostringstream &buf) const;
1176 Query_term_type term_type() const override { return QT_QUERY_BLOCK; }
1177 const char *operator_string() const override { return "query_block"; }
1178 Query_block *query_block() const override {
1179 return const_cast<Query_block *>(this);
1180 }
1181 void destroy_tree() override { m_parent = nullptr; }
1182
1183 bool open_result_tables(THD *, int) override;
1184 /// end of overridden methods from Query_term
1185 bool absorb_limit_of(Query_block *block);
1186
1187 Item *where_cond() const { return m_where_cond; }
1189 void set_where_cond(Item *cond) { m_where_cond = cond; }
1190 Item *having_cond() const { return m_having_cond; }
1192 void set_having_cond(Item *cond) { m_having_cond = cond; }
1195 bool change_query_result(THD *thd, Query_result_interceptor *new_result,
1196 Query_result_interceptor *old_result);
1197
1198 /// Set base options for a query block (and active options too)
1199 void set_base_options(ulonglong options_arg) {
1200 DBUG_EXECUTE_IF("no_const_tables", options_arg |= OPTION_NO_CONST_TABLES;);
1201
1202 // Make sure we do not overwrite options by accident
1203 assert(m_base_options == 0 && m_active_options == 0);
1204 m_base_options = options_arg;
1205 m_active_options = options_arg;
1206 }
1207
1208 /// Add base options to a query block, also update active options
1210 assert(first_execution);
1213 }
1214
1215 /**
1216 Remove base options from a query block.
1217 Active options are also updated, and we assume here that "extra" options
1218 cannot override removed base options.
1219 */
1221 assert(first_execution);
1224 }
1225
1226 /// Make active options from base options, supplied options and environment:
1227 void make_active_options(ulonglong added_options, ulonglong removed_options);
1228
1229 /// Adjust the active option set
1231
1232 /// @return the active query options
1234
1235 /**
1236 Set associated tables as read_only, ie. they cannot be inserted into,
1237 updated or deleted from during this statement.
1238 Commonly used for query blocks that are part of derived tables or
1239 views that are materialized.
1240 */
1242 // Set all referenced base tables as read only.
1243 for (Table_ref *tr = leaf_tables; tr != nullptr; tr = tr->next_leaf)
1244 tr->set_readonly();
1245 }
1246
1247 /// @returns a map of all tables references in the query block
1248 table_map all_tables_map() const { return (1ULL << leaf_table_count) - 1; }
1249
1250 bool remove_aggregates(THD *thd, Query_block *select);
1251
1255 Query_block *next_query_block() const { return next; }
1256
1258
1260
1261 void mark_as_dependent(Query_block *last, bool aggregate);
1262
1263 /// @returns true if query block references any tables
1264 bool has_tables() const { return m_table_list.elements != 0; }
1265
1266 /// @return true if query block is explicitly grouped (non-empty GROUP BY)
1267 bool is_explicitly_grouped() const { return group_list.elements != 0; }
1268
1269 /**
1270 @return true if this query block is implicitly grouped, ie it is not
1271 explicitly grouped but contains references to set functions.
1272 The query will return max. 1 row (@see also is_single_grouped()).
1273 */
1275 return m_agg_func_used && group_list.elements == 0;
1276 }
1277
1278 /**
1279 @return true if this query block is explicitly or implicitly grouped.
1280 @note a query with DISTINCT is not considered to be aggregated.
1281 @note in standard SQL, a query with HAVING is defined as grouped, however
1282 MySQL allows HAVING without any aggregation to be the same as WHERE.
1283 */
1284 bool is_grouped() const { return group_list.elements > 0 || m_agg_func_used; }
1285
1286 /// @return true if this query block contains DISTINCT at start of select list
1287 bool is_distinct() const { return active_options() & SELECT_DISTINCT; }
1288
1289 /**
1290 @return true if this query block contains an ORDER BY clause.
1291
1292 @note returns false if ORDER BY has been eliminated, e.g if the query
1293 can return max. 1 row.
1294 */
1295 bool is_ordered() const { return order_list.elements > 0; }
1296
1297 /**
1298 Based on the structure of the query at resolution time, it is possible to
1299 conclude that DISTINCT is useless and remove it.
1300 This is the case if:
1301 - all GROUP BY expressions are in SELECT list, so resulting group rows are
1302 distinct,
1303 - and ROLLUP is not specified, so it adds no row for NULLs.
1304
1305 @returns true if we can remove DISTINCT.
1306
1307 @todo could refine this to if ROLLUP were specified and all GROUP
1308 expressions were non-nullable, because ROLLUP then adds only NULL values.
1309 Currently, ROLLUP+DISTINCT is rejected because executor cannot handle
1310 it in all cases.
1311 */
1312 bool can_skip_distinct() const {
1313 return is_grouped() && hidden_group_field_count == 0 &&
1315 }
1316
1317 /// @return true if this query block has a LIMIT clause
1318 bool has_limit() const { return select_limit != nullptr; }
1319
1320 /// @return true if query block references full-text functions
1321 bool has_ft_funcs() const { return ftfunc_list->elements > 0; }
1322
1323 /// @returns true if query block is a recursive member of a recursive unit
1324 bool is_recursive() const { return recursive_reference != nullptr; }
1325
1326 /**
1327 Finds a group expression matching the given item, or nullptr if
1328 none. When there are multiple candidates, ones that match in name are
1329 given priority (such that “a AS c GROUP BY a,b,c” resolves to c, not a);
1330 if there is still a tie, the leftmost is given priority.
1331
1332 @param item The item to search for.
1333 @param [out] rollup_level If not nullptr, will be set to the group
1334 expression's index (0-based).
1335 */
1336 ORDER *find_in_group_list(Item *item, int *rollup_level) const;
1337 int group_list_size() const;
1338
1339 /// @returns true if query block contains window functions
1340 bool has_windows() const { return m_windows.elements > 0; }
1341
1342 void invalidate();
1343
1344 uint get_in_sum_expr() const { return in_sum_expr; }
1345
1346 bool add_item_to_list(Item *item);
1348 Table_ref *add_table_to_list(THD *thd, Table_ident *table, const char *alias,
1349 ulong table_options,
1351 enum_mdl_type mdl_type = MDL_SHARED_READ,
1352 List<Index_hint> *hints = nullptr,
1353 List<String> *partition_names = nullptr,
1354 LEX_STRING *option = nullptr,
1355 Parse_context *pc = nullptr);
1356
1357 /**
1358 Add item to the hidden part of select list
1359
1360 @param item item to add
1361
1362 @return Pointer to reference of the added item
1363 */
1364 Item **add_hidden_item(Item *item);
1365
1366 /// Remove hidden items from select list
1367 void remove_hidden_items();
1368
1369 Table_ref *get_table_list() const { return m_table_list.first; }
1370 bool init_nested_join(THD *thd);
1372 Table_ref *nest_last_join(THD *thd, size_t table_cnt = 2);
1375
1376 /// Wrappers over fields / get_fields_list() that hide items where
1377 /// item->hidden, meant for range-based for loops. See sql/visible_fields.h.
1379 auto visible_fields() const { return VisibleFields(fields); }
1380
1381 /// Check privileges for views that are merged into query block
1382 bool check_view_privileges(THD *thd, ulong want_privilege_first,
1383 ulong want_privilege_next);
1384 /// Check privileges for all columns referenced from query block
1385 bool check_column_privileges(THD *thd);
1386
1387 /// Check privileges for column references in subqueries of a query block
1389
1390 /// Resolve and prepare information about tables for one query block
1391 bool setup_tables(THD *thd, Table_ref *tables, bool select_insert);
1392
1393 /// Resolve OFFSET and LIMIT clauses
1394 bool resolve_limits(THD *thd);
1395
1396 /// Resolve derived table, view, table function information for a query block
1397 bool resolve_placeholder_tables(THD *thd, bool apply_semijoin);
1398
1399 /// Propagate exclusion from table uniqueness test into subqueries
1401
1402 /// Merge name resolution context objects of a subquery into its parent
1403 void merge_contexts(Query_block *inner);
1404
1405 /// Merge derived table into query block
1406 bool merge_derived(THD *thd, Table_ref *derived_table);
1407
1408 bool flatten_subqueries(THD *thd);
1409
1410 /**
1411 Update available semijoin strategies for semijoin nests.
1412
1413 Available semijoin strategies needs to be updated on every execution since
1414 optimizer_switch setting may have changed.
1415
1416 @param thd Pointer to THD object for session.
1417 Used to access optimizer_switch
1418 */
1420
1421 /**
1422 Returns which subquery execution strategies can be used for this query
1423 block.
1424
1425 @param thd Pointer to THD object for session.
1426 Used to access optimizer_switch
1427
1428 @retval SUBQ_MATERIALIZATION Subquery Materialization should be used
1429 @retval SUBQ_EXISTS In-to-exists execution should be used
1430 @retval CANDIDATE_FOR_IN2EXISTS_OR_MAT A cost-based decision should be made
1431 */
1432 Subquery_strategy subquery_strategy(const THD *thd) const;
1433
1434 /**
1435 Returns whether semi-join is enabled for this query block
1436
1437 @see @c Opt_hints_qb::semijoin_enabled for details on how hints
1438 affect this decision. If there are no hints for this query block,
1439 optimizer_switch setting determines whether semi-join is used.
1440
1441 @param thd Pointer to THD object for session.
1442 Used to access optimizer_switch
1443
1444 @return true if semijoin is enabled,
1445 false otherwise
1446 */
1447 bool semijoin_enabled(const THD *thd) const;
1448
1450 sj_candidates = sj_cand;
1451 }
1453 sj_candidates->push_back(predicate);
1454 }
1455 bool has_sj_candidates() const {
1456 return sj_candidates != nullptr && !sj_candidates->empty();
1457 }
1458
1459 bool has_subquery_transforms() const { return sj_candidates != nullptr; }
1460
1461 /// Add full-text function elements from a list into this query block
1463
1464 void set_lock_for_table(const Lock_descriptor &descriptor, Table_ref *table);
1465
1466 void set_lock_for_tables(thr_lock_type lock_type);
1467
1468 inline void init_order() {
1469 assert(order_list.elements == 0);
1470 order_list.elements = 0;
1471 order_list.first = nullptr;
1472 order_list.next = &order_list.first;
1473 }
1474 /*
1475 This method created for reiniting LEX in mysql_admin_table() and can be
1476 used only if you are going remove all Query_block & units except belonger
1477 to LEX (LEX::unit & LEX::select, for other purposes use
1478 Query_expression::exclude_level()
1479 */
1480 void cut_subtree() { slave = nullptr; }
1481 bool test_limit();
1482 /**
1483 Get offset for LIMIT.
1484
1485 Evaluate offset item if necessary.
1486
1487 @return Number of rows to skip.
1488
1489 @todo Integrate better with Query_expression::set_limit()
1490 */
1491 ha_rows get_offset(const THD *thd) const;
1492 /**
1493 Get limit.
1494
1495 Evaluate limit item if necessary.
1496
1497 @return Limit of rows in result.
1498
1499 @todo Integrate better with Query_expression::set_limit()
1500 */
1501 ha_rows get_limit(const THD *thd) const;
1502
1503 /// Assign a default name resolution object for this query block.
1504 bool set_context(Name_resolution_context *outer_context);
1505
1506 /// Setup the array containing references to base items
1507 bool setup_base_ref_items(THD *thd);
1508 void print(const THD *thd, String *str, enum_query_type query_type);
1509
1510 /**
1511 Print detail of the Query_block object.
1512
1513 @param thd Thread handler
1514 @param query_type Options to print out string output
1515 @param[out] str String of output.
1516 */
1517 void print_query_block(const THD *thd, String *str,
1518 enum_query_type query_type);
1519
1520 /**
1521 Print detail of the UPDATE statement.
1522
1523 @param thd Thread handler
1524 @param[out] str String of output
1525 @param query_type Options to print out string output
1526 */
1527 void print_update(const THD *thd, String *str, enum_query_type query_type);
1528
1529 /**
1530 Print detail of the DELETE statement.
1531
1532 @param thd Thread handler
1533 @param[out] str String of output
1534 @param query_type Options to print out string output
1535 */
1536 void print_delete(const THD *thd, String *str, enum_query_type query_type);
1537
1538 /**
1539 Print detail of the INSERT statement.
1540
1541 @param thd Thread handler
1542 @param[out] str String of output
1543 @param query_type Options to print out string output
1544 */
1545 void print_insert(const THD *thd, String *str, enum_query_type query_type);
1546
1547 /**
1548 Print detail of Hints.
1549
1550 @param thd Thread handler
1551 @param[out] str String of output
1552 @param query_type Options to print out string output
1553 */
1554 void print_hints(const THD *thd, String *str, enum_query_type query_type);
1555
1556 /**
1557 Print error.
1558
1559 @param thd Thread handler
1560 @param[out] str String of output
1561
1562 @retval false If there is no error
1563 @retval true else
1564 */
1565 bool print_error(const THD *thd, String *str);
1566
1567 /**
1568 Print select options.
1569
1570 @param[out] str String of output
1571 */
1573
1574 /**
1575 Print UPDATE options.
1576
1577 @param[out] str String of output
1578 */
1580
1581 /**
1582 Print DELETE options.
1583
1584 @param[out] str String of output
1585 */
1587
1588 /**
1589 Print INSERT options.
1590
1591 @param[out] str String of output
1592 */
1594
1595 /**
1596 Print list of tables.
1597
1598 @param thd Thread handler
1599 @param[out] str String of output
1600 @param table_list Table_ref object
1601 @param query_type Options to print out string output
1602 */
1603 void print_table_references(const THD *thd, String *str,
1604 Table_ref *table_list,
1605 enum_query_type query_type);
1606
1607 /**
1608 Print list of items in Query_block object.
1609
1610 @param thd Thread handle
1611 @param[out] str String of output
1612 @param query_type Options to print out string output
1613 */
1614 void print_item_list(const THD *thd, String *str, enum_query_type query_type);
1615
1616 /**
1617 Print assignments list. Used in UPDATE and
1618 INSERT ... ON DUPLICATE KEY UPDATE ...
1619
1620 @param thd Thread handle
1621 @param[out] str String of output
1622 @param query_type Options to print out string output
1623 @param fields List columns to be assigned.
1624 @param values List of values.
1625 */
1626 void print_update_list(const THD *thd, String *str,
1627 enum_query_type query_type,
1629 const mem_root_deque<Item *> &values);
1630
1631 /**
1632 Print column list to be inserted into. Used in INSERT.
1633
1634 @param thd Thread handle
1635 @param[out] str String of output
1636 @param query_type Options to print out string output
1637 */
1638 void print_insert_fields(const THD *thd, String *str,
1639 enum_query_type query_type);
1640
1641 /**
1642 Print list of values, used in INSERT and for general VALUES clause.
1643
1644 @param thd Thread handle
1645 @param[out] str String of output
1646 @param query_type Options to print out string output
1647 @param values List of values
1648 @param prefix Prefix to print before each row in value list
1649 = nullptr: No prefix wanted
1650 */
1651 void print_values(const THD *thd, String *str, enum_query_type query_type,
1652 const mem_root_deque<mem_root_deque<Item *> *> &values,
1653 const char *prefix);
1654
1655 /**
1656 Print list of tables in FROM clause.
1657
1658 @param thd Thread handler
1659 @param[out] str String of output
1660 @param query_type Options to print out string output
1661 */
1662 void print_from_clause(const THD *thd, String *str,
1663 enum_query_type query_type);
1664
1665 /**
1666 Print list of conditions in WHERE clause.
1667
1668 @param thd Thread handle
1669 @param[out] str String of output
1670 @param query_type Options to print out string output
1671 */
1672 void print_where_cond(const THD *thd, String *str,
1673 enum_query_type query_type);
1674
1675 /**
1676 Print list of items in GROUP BY clause.
1677
1678 @param thd Thread handle
1679 @param[out] str String of output
1680 @param query_type Options to print out string output
1681 */
1682 void print_group_by(const THD *thd, String *str, enum_query_type query_type);
1683
1684 /**
1685 Print list of items in HAVING clause.
1686
1687 @param thd Thread handle
1688 @param[out] str String of output
1689 @param query_type Options to print out string output
1690 */
1691 void print_having(const THD *thd, String *str, enum_query_type query_type);
1692
1693 /**
1694 Print details of Windowing functions.
1695
1696 @param thd Thread handler
1697 @param[out] str String of output
1698 @param query_type Options to print out string output
1699 */
1700 void print_windows(const THD *thd, String *str, enum_query_type query_type);
1701
1702 /**
1703 Print list of items in ORDER BY clause.
1704
1705 @param thd Thread handle
1706 @param[out] str String of output
1707 @param query_type Options to print out string output
1708 */
1709 void print_order_by(const THD *thd, String *str,
1710 enum_query_type query_type) const;
1711
1712 void print_limit(const THD *thd, String *str,
1713 enum_query_type query_type) const;
1714 bool save_properties(THD *thd);
1715
1716 /**
1717 Accept function for SELECT and DELETE.
1718
1719 @param visitor Select_lex_visitor Object
1720 */
1721 bool accept(Select_lex_visitor *visitor);
1722
1723 /**
1724 Cleanup this subtree (this Query_block and all nested Query_blockes and
1725 Query_expressions).
1726 @param full if false only partial cleanup is done, JOINs and JOIN_TABs are
1727 kept to provide info for EXPLAIN CONNECTION; if true, complete cleanup is
1728 done, all JOINs are freed.
1729 */
1730 void cleanup(bool full) override;
1731 /*
1732 Recursively cleanup the join of this select lex and of all nested
1733 select lexes. This is not a full cleanup.
1734 */
1735 void cleanup_all_joins();
1736 /**
1737 Destroy contained objects, in particular temporary tables which may
1738 have their own mem_roots.
1739 */
1740 void destroy();
1741
1742 /// @return true when query block is not part of a set operation and is not a
1743 /// parenthesized query expression.
1746 }
1747
1748 /**
1749 @return true if query block is found during preparation to produce no data.
1750 Notice that if query is implicitly grouped, an aggregation row will
1751 still be returned.
1752 */
1753 bool is_empty_query() const { return m_empty_query; }
1754
1755 /// Set query block as returning no data
1756 /// @todo This may also be set when we have an always false WHERE clause
1758 assert(join == nullptr);
1759 m_empty_query = true;
1760 }
1761 /*
1762 For MODE_ONLY_FULL_GROUP_BY we need to know if
1763 this query block is the aggregation query of at least one aggregate
1764 function.
1765 */
1766 bool agg_func_used() const { return m_agg_func_used; }
1768
1769 void set_agg_func_used(bool val) { m_agg_func_used = val; }
1770
1772
1773 bool right_joins() const { return m_right_joins; }
1775
1776 /// Lookup for Query_block type
1777 enum_explain_type type() const;
1778
1779 /// Lookup for a type string
1780 const char *get_type_str() { return type_str[static_cast<int>(type())]; }
1782 return type_str[static_cast<int>(type)];
1783 }
1784
1786 bool is_cacheable() const { return !uncacheable; }
1787
1788 /// @returns true if this query block outputs at most one row.
1790 return (m_table_list.size() == 0 &&
1791 (!is_table_value_constructor || row_value_list->size() == 1));
1792 }
1793
1794 /// Include query block inside a query expression.
1795 void include_down(LEX *lex, Query_expression *outer);
1796
1797 /// Include a query block next to another query block.
1798 void include_neighbour(LEX *lex, Query_block *before);
1799
1800 /// Include query block inside a query expression, but do not link.
1802
1803 /// Include query block into global list.
1804 void include_in_global(Query_block **plink);
1805
1806 /// Include chain of query blocks into global list.
1808
1809 /// Renumber query blocks of contained query expressions
1810 void renumber(LEX *lex);
1811
1812 /**
1813 Does permanent transformations which are local to a query block (which do
1814 not merge it to another block).
1815 */
1816 bool apply_local_transforms(THD *thd, bool prune);
1817
1818 /// Pushes parts of the WHERE condition of this query block to materialized
1819 /// derived tables.
1821
1822 bool get_optimizable_conditions(THD *thd, Item **new_where,
1823 Item **new_having);
1824
1825 bool validate_outermost_option(LEX *lex, const char *wrong_option) const;
1826 bool validate_base_options(LEX *lex, ulonglong options) const;
1827
1828 bool walk(Item_processor processor, enum_walk walk, uchar *arg);
1829
1830 bool add_tables(THD *thd, const Mem_root_array<Table_ident *> *tables,
1831 ulong table_options, thr_lock_type lock_type,
1832 enum_mdl_type mdl_type);
1833
1834 bool resolve_rollup_wfs(THD *thd);
1835
1836 bool setup_conds(THD *thd);
1837 bool prepare(THD *thd, mem_root_deque<Item *> *insert_field_list);
1838 bool optimize(THD *thd, bool finalize_access_paths);
1839 void reset_nj_counters(mem_root_deque<Table_ref *> *join_list = nullptr);
1840
1841 // If the query block has exactly one single visible field, returns it.
1842 // If not, returns nullptr.
1843 Item *single_visible_field() const;
1844 size_t num_visible_fields() const;
1845
1846 // Whether the SELECT list is empty (hidden fields are ignored).
1847 // Typically used to distinguish INSERT INTO ... SELECT queries
1848 // from INSERT INTO ... VALUES queries.
1849 bool field_list_is_empty() const;
1850
1851 /// Creates a clone for the given expression by re-parsing the
1852 /// expression. Used in condition pushdown to derived tables.
1853 Item *clone_expression(THD *thd, Item *item);
1854 /// Returns an expression from the select list of the query block
1855 /// using the field's index in a derived table.
1856 Item *get_derived_expr(uint expr_index);
1857
1859 AccessPath *childPath, TABLE *dst_table);
1860
1861 // ************************************************
1862 // * Members (most of these should not be public) *
1863 // ************************************************
1864
1866 /**
1867 All expressions needed after join and filtering, ie., select list,
1868 group by list, having clause, window clause, order by clause,
1869 including hidden fields.
1870 Does not include join conditions nor where clause.
1871
1872 This should ideally be changed into Mem_root_array<Item *>, but
1873 find_order_in_list() depends on pointer stability (it stores a pointer
1874 to an element in referenced_by[]). Similarly, there are some instances
1875 of thd->change_item_tree() that store pointers to elements in this list.
1876
1877 Because of this, adding or removing elements in the middle is not allowed;
1878 std::deque guarantees pointer stability only in the face of adding
1879 or removing elements from either end, ie., {push,pop}_{front_back}.
1880
1881 Currently, all hidden items must be before all visible items.
1882 This is primarily due to the requirement for pointer stability
1883 but also because change_to_use_tmp_fields() depends on it when mapping
1884 items to ref_item_array indexes. It would be good to get rid of this
1885 requirement in the future.
1886 */
1888
1889 /**
1890 All windows defined on the select, both named and inlined
1891 */
1893
1894 /**
1895 A pointer to ftfunc_list_alloc, list of full text search functions.
1896 */
1899
1900 /// The VALUES items of a table value constructor.
1902
1903 /// List of semi-join nests generated for this query block
1905
1906 /// List of tables in FROM clause - use Table_ref::next_local to traverse
1908
1909 /**
1910 ORDER BY clause.
1911 This list may be mutated during optimization (by remove_const() in the old
1912 optimizer or by RemoveRedundantOrderElements() in the hypergraph optimizer),
1913 so for prepared statements, we keep a copy of the ORDER.next pointers in
1914 order_list_ptrs, and re-establish the original list before each execution.
1915 */
1918
1919 /**
1920 GROUP BY clause.
1921 This list may be mutated during optimization (by remove_const() in the old
1922 optimizer or by RemoveRedundantOrderElements() in the hypergraph optimizer),
1923 so for prepared statements, we keep a copy of the ORDER.next pointers in
1924 group_list_ptrs, and re-establish the original list before each execution.
1925 */
1928
1929 // Used so that AggregateIterator knows which items to signal when the rollup
1930 // level changes. Obviously only used in the presence of rollup.
1935
1936 /// Query-block-level hints, for this query block
1938
1939 char *db{nullptr};
1940
1941 /**
1942 If this query block is a recursive member of a recursive unit: the
1943 Table_ref, in this recursive member, referencing the query
1944 name.
1945 */
1947
1948 /// Reference to LEX that this query block belongs to
1949 LEX *parent_lex{nullptr};
1950
1951 /**
1952 The set of those tables whose fields are referenced in the select list of
1953 this select level.
1954 */
1956 table_map outer_join{0}; ///< Bitmap of all inner tables from outer joins
1957
1958 /**
1959 Context for name resolution for all column references except columns
1960 from joined tables.
1961 */
1963
1964 /**
1965 Pointer to first object in list of Name res context objects that have
1966 this query block as the base query block.
1967 Includes field "context" which is embedded in this query block.
1968 */
1970
1971 /**
1972 After optimization it is pointer to corresponding JOIN. This member
1973 should be changed only when THD::LOCK_query_plan mutex is taken.
1974 */
1975 JOIN *join{nullptr};
1976 /// Set of table references contained in outer-most join nest
1978 /// Pointer to the set of table references in the currently active join
1980 /// table embedding the above list
1982 /**
1983 Points to first leaf table of query block. After setup_tables() is done,
1984 this is a list of base tables and derived tables. After derived tables
1985 processing is done, this is a list of base tables only.
1986 Use Table_ref::next_leaf to traverse the list.
1987 */
1989 /// Last table for LATERAL join, used by table functions
1991
1992 /// LIMIT clause, NULL if no limit is given
1994 /// LIMIT ... OFFSET clause, NULL if no offset is given
1996
1997 /**
1998 Circular linked list of aggregate functions in nested query blocks.
1999 This is needed if said aggregate functions depend on outer values
2000 from this query block; if so, we want to add them as hidden items
2001 in our own field list, to be able to evaluate them.
2002 @see Item_sum::check_sum_func
2003 */
2005
2006 /**
2007 Array of pointers to "base" items; one each for every selected expression
2008 and referenced item in the query block. All references to fields are to
2009 buffers associated with the primary input tables.
2010 */
2012
2013 uint select_number{0}; ///< Query block number (used for EXPLAIN)
2014
2015 /**
2016 Saved values of the WHERE and HAVING clauses. Allowed values are:
2017 - COND_UNDEF if the condition was not specified in the query or if it
2018 has not been optimized yet
2019 - COND_TRUE if the condition is always true
2020 - COND_FALSE if the condition is impossible
2021 - COND_OK otherwise
2022 */
2025
2026 /// Parse context: indicates where the current expression is being parsed
2028 /// Parse context: is inside a set function if this is positive
2030
2031 /**
2032 Three fields used by semi-join transformations to know when semi-join is
2033 possible, and in which condition tree the subquery predicate is located.
2034 */
2043 RESOLVE_NONE}; ///< Indicates part of query being resolved
2044
2045 /**
2046 Number of fields used in select list or where clause of current select
2047 and all inner subselects.
2048 */
2050 /**
2051 number of items in select_list and HAVING clause used to get number
2052 bigger then can be number of entries that will be added to all item
2053 list during split_sum_func
2054 */
2056 /// Number of arguments of and/or/xor in where/having/on
2058 /// Number of predicates after preparation
2059 uint cond_count{0};
2060 /// Number of between predicates in where/having/on
2062 /// Maximal number of elements in multiple equalities
2064
2065 /**
2066 Number of Item_sum-derived objects in this SELECT. Keeps count of
2067 aggregate functions and window functions(to allocate items in ref array).
2068 See Query_block::setup_base_ref_items.
2069 */
2071 /// Number of Item_sum-derived objects in children and descendant SELECTs
2073
2074 /// Keep track for allocation of base_ref_items: scalar subqueries may be
2075 /// replaced by a field during scalar_to_derived transformation
2077
2078 /// Number of materialized derived tables and views in this query block.
2080 /// Number of partitioned tables
2082
2083 /**
2084 Number of wildcards used in the SELECT list. For example,
2085 SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
2086 has 3 wildcards.
2087 */
2088 uint with_wild{0};
2089
2090 /// Number of leaf tables in this query block.
2092 /// Number of derived tables and views in this query block.
2094 /// Number of table functions in this query block
2096
2097 /**
2098 Nesting level of query block, outer-most query block has level 0,
2099 its subqueries have level 1, etc. @see also sql/item_sum.h.
2100 */
2102
2103 /// Indicates whether this query block contains the WITH ROLLUP clause
2105
2106 /// @see enum_condition_context
2108
2109 /// If set, the query block is of the form VALUES row_list.
2111
2112 /// Describes context of this query block (e.g if it is a derived table).
2114
2115 /**
2116 result of this query can't be cached, bit field, can be :
2117 UNCACHEABLE_DEPENDENT
2118 UNCACHEABLE_RAND
2119 UNCACHEABLE_SIDEEFFECT
2120 */
2122
2123 void update_used_tables();
2125 bool save_cmd_properties(THD *thd);
2126
2127 /**
2128 This variable is required to ensure proper work of subqueries and
2129 stored procedures. Generally, one should use the states of
2130 Query_arena to determine if it's a statement prepare or first
2131 execution of a stored procedure. However, in case when there was an
2132 error during the first execution of a stored procedure, the SP body
2133 is not expelled from the SP cache. Therefore, a deeply nested
2134 subquery might be left unoptimized. So we need this per-subquery
2135 variable to inidicate the optimization/execution state of every
2136 subquery. Prepared statements work OK in that regard, as in
2137 case of an error during prepare the PS is not created.
2138 */
2140
2141 /// True when semi-join pull-out processing is complete
2142 bool sj_pullout_done{false};
2143
2144 /// Used by nested scalar_to_derived transformations
2146
2147 /// True: skip local transformations during prepare() call (used by INSERT)
2149
2151
2152 /// true when having fix field called in processing of this query block
2153 bool having_fix_field{false};
2154 /// true when GROUP BY fix field called in processing of this query block
2155 bool group_fix_field{false};
2156
2157 /**
2158 True if contains or aggregates set functions.
2159 @note this is wrong when a locally found set function is aggregated
2160 in an outer query block.
2161 */
2162 bool with_sum_func{false};
2163
2164 /**
2165 HAVING clause contains subquery => we can't close tables before
2166 query processing end even if we use temporary table
2167 */
2169
2170 /**
2171 If true, use select_limit to limit number of rows selected.
2172 Applicable when no explicit limit is supplied, and only for the
2173 outermost query block of a SELECT statement.
2174 */
2176
2177 /// If true, limit object is added internally
2178 bool m_internal_limit{false};
2179
2180 /// exclude this query block from unique_table() check
2182
2183 bool no_table_names_allowed{false}; ///< used for global order by
2184
2185 /// Hidden items added during optimization
2186 /// @note that using this means we modify resolved data during optimization
2188
2190
2191 private:
2192 friend class Query_expression;
2193 friend class Condition_context;
2194
2195 /// Helper for save_properties()
2197 Group_list_ptrs **list_ptrs);
2198
2200 bool simplify_joins(THD *thd, mem_root_deque<Table_ref *> *join_list,
2201 bool top, bool in_sj, Item **new_conds,
2202 uint *changelog = nullptr);
2203 /// Remove semijoin condition for this query block
2204 void clear_sj_expressions(NESTED_JOIN *nested_join);
2205 /// Build semijoin condition for th query block
2206 bool build_sj_cond(THD *thd, NESTED_JOIN *nested_join,
2207 Query_block *subq_query_block, table_map outer_tables_map,
2208 Item **sj_cond);
2210 Table_ref *join_nest);
2211
2214 Item *join_cond, bool left_outer,
2215 bool use_inner_join);
2216 bool transform_subquery_to_derived(THD *thd, Table_ref **out_tl,
2217 Query_expression *subs_query_expression,
2218 Item_subselect *subq, bool use_inner_join,
2219 bool reject_multiple_rows,
2220 Item *join_condition,
2221 Item *lifted_where_cond);
2223 THD *thd, Item_exists_subselect *subq_pred);
2225 THD *thd, Table_ref *derived, Item *lifted_where,
2226 Lifted_fields_map *lifted_where_fields, bool *added_card_check);
2228 THD *thd, Table_ref *derived, Lifted_fields_map *lifted_where_fields,
2229 bool added_card_check);
2230 void replace_referenced_item(Item *const old_item, Item *const new_item);
2231 void remap_tables(THD *thd);
2233 Item *resolve_rollup_item(THD *thd, Item *item);
2234 bool resolve_rollup(THD *thd);
2235
2236 bool setup_wild(THD *thd);
2237 bool setup_order_final(THD *thd);
2238 bool setup_group(THD *thd);
2239 void fix_after_pullout(Query_block *parent_query_block,
2240 Query_block *removed_query_block);
2244 bool empty_order_list(Query_block *sl);
2246 bool in_update);
2247 bool find_common_table_expr(THD *thd, Table_ident *table_id, Table_ref *tl,
2248 Parse_context *pc, bool *found);
2249 /**
2250 Transform eligible scalar subqueries in the SELECT list, WHERE condition,
2251 HAVING condition or JOIN conditions of a query block[*] to an equivalent
2252 derived table of a LEFT OUTER join, e.g. as shown in this uncorrelated
2253 subquery:
2254
2255 [*] a.k.a "transformed query block" throughout this method and its minions.
2256
2257 <pre>
2258 SELECT * FROM t1
2259 WHERE t1.a > (SELECT COUNT(a) AS cnt FROM t2); ->
2260
2261 SELECT t1.* FROM t1 LEFT OUTER JOIN
2262 (SELECT COUNT(a) AS cnt FROM t2) AS derived
2263 ON TRUE WHERE t1.a > derived.cnt;
2264 </pre>
2265
2266 Grouping in the transformed query block may necessitate the grouping to be
2267 moved down to another derived table, cf. transform_grouped_to_derived.
2268
2269 Limitations:
2270 - only implicitly grouped subqueries (guaranteed to have cardinality one)
2271 are identified as scalar subqueries.
2272 _ Correlated subqueries are not handled
2273
2274 @param[in,out] thd the session context
2275 @returns true on error
2276 */
2279 Item **lifted_where);
2280 bool replace_item_in_expression(Item **expr, bool was_hidden,
2282 Item_transformer transformer);
2283 bool transform_grouped_to_derived(THD *thd, bool *break_off);
2284 bool replace_subquery_in_expr(THD *thd, Item::Css_info *subquery,
2285 Table_ref *tr, Item **expr);
2286 bool nest_derived(THD *thd, Item *join_cond,
2287 mem_root_deque<Table_ref *> *join_list,
2288 Table_ref *new_derived_table);
2289
2291
2292 // Delete unused columns from merged derived tables
2294
2295 bool prepare_values(THD *thd);
2296 bool check_only_full_group_by(THD *thd);
2297 /**
2298 Copies all non-aggregated calls to the full-text search MATCH function from
2299 the HAVING clause to the SELECT list (as hidden items), so that we can
2300 materialize their result and not only their input. This is needed when the
2301 result will be accessed after aggregation, as the result from MATCH cannot
2302 be recalculated from its input alone. It also needs the underlying scan to
2303 be positioned on the correct row. Storing the value before aggregation
2304 removes the need for evaluating MATCH again after materialization.
2305 */
2307
2308 //
2309 // Members:
2310 //
2311
2312 /**
2313 Pointer to collection of subqueries candidate for semi/antijoin
2314 conversion.
2315 Template parameter is "true": no need to run DTORs on pointers.
2316 */
2318
2319 /// How many expressions are part of the order by but not select list.
2321
2322 /**
2323 Intrusive linked list of all query blocks within the same
2324 query expression.
2325 */
2327
2328 /// The query expression containing this query block.
2330 /// The first query expression contained within this query block.
2332
2333 /// Intrusive double-linked global list of query blocks.
2336
2337 /// Result of this query block
2339
2340 /**
2341 Options assigned from parsing and throughout resolving,
2342 should not be modified after resolving is done.
2343 */
2345 /**
2346 Active options. Derived from base options, modifiers added during
2347 resolving and values from session variable option_bits. Since the latter
2348 may change, active options are refreshed per execution of a statement.
2349 */
2351
2352 public:
2354 nullptr}; ///< Used when resolving outer join condition
2355 private:
2356 /**
2357 Condition to be evaluated after all tables in a query block are joined.
2358 After all permanent transformations have been conducted by
2359 Query_block::prepare(), this condition is "frozen", any subsequent changes
2360 to it must be done with change_item_tree(), unless they only modify AND/OR
2361 items and use a copy created by Query_block::get_optimizable_conditions().
2362 Same is true for 'having_cond'.
2363 */
2365
2366 /// Condition to be evaluated on grouped rows after grouping.
2368
2369 /// Number of GROUP BY expressions added to all_fields
2371
2372 /**
2373 True if query block has semi-join nests merged into it. Notice that this
2374 is updated earlier than sj_nests, so check this if info is needed
2375 before the full resolver process is complete.
2376 */
2377 bool has_sj_nests{false};
2378 bool has_aj_nests{false}; ///< @see has_sj_nests; counts antijoin nests.
2379 bool m_right_joins{false}; ///< True if query block has right joins
2380
2381 /// Allow merge of immediate unnamed derived tables
2383
2384 bool m_agg_func_used{false};
2386
2387 /**
2388 True if query block does not generate any rows before aggregation,
2389 determined during preparation (not optimization).
2390 */
2391 bool m_empty_query{false};
2392
2393 static const char
2395};
2396
2397inline bool Query_expression::is_union() const {
2398 Query_term *qt = query_term();
2399 while (qt->term_type() == QT_UNARY)
2400 qt = down_cast<Query_term_unary *>(qt)->m_children[0];
2401 return qt->term_type() == QT_UNION;
2402}
2403
2405 Query_term *qt = query_term();
2406 while (qt->term_type() == QT_UNARY)
2407 qt = down_cast<Query_term_unary *>(qt)->m_children[0];
2408 const Query_term_type type = qt->term_type();
2409 return type == QT_UNION || type == QT_INTERSECT || type == QT_EXCEPT;
2410}
2411
2412/// Utility RAII class to save/modify/restore the condition_context information
2413/// of a query block. @see enum_condition_context.
2415 public:
2417 Query_block *select_ptr,
2419 : select(nullptr), saved_value() {
2420 if (select_ptr) {
2421 select = select_ptr;
2423 // More restrictive wins over less restrictive:
2424 if (new_type == enum_condition_context::NEITHER ||
2425 (new_type == enum_condition_context::ANDS_ORS &&
2427 select->condition_context = new_type;
2428 }
2429 }
2432 }
2433
2434 private:
2437};
2438
2440 std::function<bool(Table_ref *)> action);
2441
2442/**
2443 Base class for secondary engine execution context objects. Secondary
2444 storage engines may create classes derived from this one which
2445 contain state they need to preserve between optimization and
2446 execution of statements. The context objects should be allocated on
2447 the execution MEM_ROOT.
2448*/
2450 public:
2451 /**
2452 Destructs the secondary engine execution context object. It is
2453 called after the query execution has completed. Secondary engines
2454 may override the destructor in subclasses and add code that
2455 performs cleanup tasks that are needed after query execution.
2456 */
2458};
2459
2461 char *user;
2465
2466 void reset();
2468
2474 LEX_CSTRING language; ///< CREATE|ALTER ... LANGUAGE <language>
2475};
2476
2477extern const LEX_STRING null_lex_str;
2478
2482
2483 /**
2484 FOLLOWS or PRECEDES as specified in the CREATE TRIGGER statement.
2485 */
2487
2488 /**
2489 Trigger name referenced in the FOLLOWS/PRECEDES clause of the CREATE TRIGGER
2490 statement.
2491 */
2493};
2494
2496
2497/*
2498 Class representing list of all tables used by statement and other
2499 information which is necessary for opening and locking its tables,
2500 like SQL command for this statement.
2501
2502 Also contains information about stored functions used by statement
2503 since during its execution we may have to add all tables used by its
2504 stored functions/triggers to this list in order to pre-open and lock
2505 them.
2506
2507 Also used by LEX::reset_n_backup/restore_backup_query_tables_list()
2508 methods to save and restore this information.
2509*/
2510
2512 public:
2514
2515 /**
2516 SQL command for this statement. Part of this class since the
2517 process of opening and locking tables for the statement needs
2518 this information to determine correct type of lock for some of
2519 the tables.
2520 */
2522 /* Global list of all tables used by this statement */
2524 /* Pointer to next_global member of last element in the previous list. */
2526 /*
2527 If non-0 then indicates that query requires prelocking and points to
2528 next_global member of last own element in query table list (i.e. last
2529 table which was not added to it as part of preparation to prelocking).
2530 0 - indicates that this query does not need prelocking.
2531 */
2533 /*
2534 Set of stored routines called by statement.
2535 (Note that we use lazy-initialization for this hash).
2536
2537 See Sroutine_hash_entry for explanation why this hash uses binary
2538 key comparison.
2539 */
2541 std::unique_ptr<malloc_unordered_map<std::string, Sroutine_hash_entry *>>
2543 /*
2544 List linking elements of 'sroutines' set. Allows you to add new elements
2545 to this set as you iterate through the list of existing elements.
2546 'sroutines_list_own_last' is pointer to ::next member of last element of
2547 this list which represents routine which is explicitly used by query.
2548 'sroutines_list_own_elements' number of explicitly used routines.
2549 We use these two members for restoring of 'sroutines_list' to the state
2550 in which it was right after query parsing.
2551 */
2555
2556 /**
2557 Locking state of tables in this particular statement.
2558
2559 If we under LOCK TABLES or in prelocked mode we consider tables
2560 for the statement to be "locked" if there was a call to lock_tables()
2561 (which called handler::start_stmt()) for tables of this statement
2562 and there was no matching close_thread_tables() call.
2563
2564 As result this state may differ significantly from one represented
2565 by Open_tables_state::lock/locked_tables_mode more, which are always
2566 "on" under LOCK TABLES or in prelocked mode.
2567 */
2571 return (lock_tables_state == LTS_LOCKED);
2572 }
2573
2574 /**
2575 Number of tables which were open by open_tables() and to be locked
2576 by lock_tables().
2577 Note that we set this member only in some cases, when this value
2578 needs to be passed from open_tables() to lock_tables() which are
2579 separated by some amount of code.
2580 */
2582
2583 /*
2584 These constructor and destructor serve for creation/destruction
2585 of Query_tables_list instances which are used as backup storage.
2586 */
2589
2590 /* Initializes (or resets) Query_tables_list object for "real" use. */
2591 void reset_query_tables_list(bool init);
2594 *this = std::move(*state);
2595 }
2596
2597 /*
2598 Direct addition to the list of query tables.
2599 If you are using this function, you must ensure that the table
2600 object, in particular table->db member, is initialized.
2601 */
2603 *(table->prev_global = query_tables_last) = table;
2604 query_tables_last = &table->next_global;
2605 }
2607 void mark_as_requiring_prelocking(Table_ref **tables_own_last) {
2608 query_tables_own_last = tables_own_last;
2609 }
2610 /* Return pointer to first not-own table in query-tables or 0 */
2612 return (query_tables_own_last ? *query_tables_own_last : nullptr);
2613 }
2616 *query_tables_own_last = nullptr;
2618 query_tables_own_last = nullptr;
2619 }
2620 }
2621
2622 /**
2623 All types of unsafe statements.
2624
2625 @note The int values of the enum elements are used to point to
2626 bits in two bitmaps in two different places:
2627
2628 - Query_tables_list::binlog_stmt_flags
2629 - THD::binlog_unsafe_warning_flags
2630
2631 Hence in practice this is not an enum at all, but a map from
2632 symbols to bit indexes.
2633
2634 The ordering of elements in this enum must correspond to the order of
2635 elements in the array binlog_stmt_unsafe_errcode.
2636 */
2638 /**
2639 SELECT..LIMIT is unsafe because the set of rows returned cannot
2640 be predicted.
2641 */
2643 /**
2644 Access to log tables is unsafe because slave and master probably
2645 log different things.
2646 */
2648 /**
2649 Inserting into an autoincrement column in a stored routine is unsafe.
2650 Even with just one autoincrement column, if the routine is invoked more
2651 than once slave is not guaranteed to execute the statement graph same way
2652 as the master. And since it's impossible to estimate how many times a
2653 routine can be invoked at the query pre-execution phase (see lock_tables),
2654 the statement is marked pessimistically unsafe.
2655 */
2657 /**
2658 Using a UDF (user-defined function) is unsafe.
2659 */
2661 /**
2662 Using most system variables is unsafe, because slave may run
2663 with different options than master.
2664 */
2666 /**
2667 Using some functions is unsafe (e.g., UUID).
2668 */
2670
2671 /**
2672 Mixing transactional and non-transactional statements are unsafe if
2673 non-transactional reads or writes are occur after transactional
2674 reads or writes inside a transaction.
2675 */
2677
2678 /**
2679 Mixing self-logging and non-self-logging engines in a statement
2680 is unsafe.
2681 */
2683
2684 /**
2685 Statements that read from both transactional and non-transactional
2686 tables and write to any of them are unsafe.
2687 */
2689
2690 /**
2691 INSERT...IGNORE SELECT is unsafe because which rows are ignored 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 INSERT...SELECT...UPDATE is unsafe because which rows are updated depends
2699 on the order that rows are retrieved by SELECT. This order cannot be
2700 predicted and may differ on master and the slave.
2701 */
2703
2704 /**
2705 Query that writes to a table with auto_inc column after selecting from
2706 other tables are unsafe as the order in which the rows are retrieved by
2707 select may differ on master and slave.
2708 */
2710
2711 /**
2712 INSERT...REPLACE SELECT is unsafe because which rows are replaced depends
2713 on the order that rows are retrieved by SELECT. This order cannot be
2714 predicted and may differ on master and the slave.
2715 */
2717
2718 /**
2719 CREATE TABLE... IGNORE... SELECT is unsafe because which rows are ignored
2720 depends on the order that rows are retrieved by SELECT. This order cannot
2721 be predicted and may differ on master and the slave.
2722 */
2724
2725 /**
2726 CREATE TABLE...REPLACE... SELECT is unsafe because which rows are replaced
2727 depends on the order that rows are retrieved from SELECT. This order
2728 cannot be predicted and may differ on master and the slave
2729 */
2731
2732 /**
2733 CREATE TABLE...SELECT on a table with auto-increment column is unsafe
2734 because which rows are replaced depends on the order that rows are
2735 retrieved from SELECT. This order cannot be predicted and may differ on
2736 master and the slave
2737 */
2739
2740 /**
2741 UPDATE...IGNORE is unsafe because which rows are ignored depends on the
2742 order that rows are updated. This order cannot be predicted and may differ
2743 on master and the slave.
2744 */
2746
2747 /**
2748 INSERT... ON DUPLICATE KEY UPDATE on a table with more than one
2749 UNIQUE KEYS is unsafe.
2750 */
2752
2753 /**
2754 INSERT into auto-inc field which is not the first part in composed
2755 primary key.
2756 */
2758
2759 /**
2760 Using a plugin is unsafe.
2761 */
2765
2766 /**
2767 XA transactions and statements.
2768 */
2770
2771 /**
2772 If a substatement inserts into or updates a table that has a column with
2773 an unsafe DEFAULT expression, it may not have the same effect on the
2774 slave.
2775 */
2777
2778 /**
2779 DML or DDL statement that reads a ACL table is unsafe, because the row
2780 are read without acquiring SE row locks. This would allow ACL tables to
2781 be updated by concurrent thread. It would not have the same effect on the
2782 slave.
2783 */
2785
2786 /**
2787 Generating invisible primary key for a table created using CREATE TABLE...
2788 SELECT... is unsafe because order in which rows are retrieved by the
2789 SELECT determines which (if any) rows are inserted. This order cannot be
2790 predicted and values for generated invisible primary key column may
2791 differ on source and replica when @@session.binlog_format=STATEMENT.
2792 */
2794
2795 /* the last element of this enumeration type. */
2798 /**
2799 This has all flags from 0 (inclusive) to BINLOG_STMT_FLAG_COUNT
2800 (exclusive) set.
2801 */
2803 ((1 << BINLOG_STMT_UNSAFE_COUNT) - 1);
2804
2805 /**
2806 Maps elements of enum_binlog_stmt_unsafe to error codes.
2807 */
2809
2810 /**
2811 Determine if this statement is marked as unsafe.
2812
2813 @retval 0 if the statement is not marked as unsafe.
2814 @retval nonzero if the statement is marked as unsafe.
2815 */
2816 inline bool is_stmt_unsafe() const { return get_stmt_unsafe_flags() != 0; }
2817
2819 return binlog_stmt_flags & (1 << unsafe);
2820 }
2821
2822 /**
2823 Flag the current (top-level) statement as unsafe.
2824 The flag will be reset after the statement has finished.
2825
2826 @param unsafe_type The type of unsafety: one of the @c
2827 BINLOG_STMT_FLAG_UNSAFE_* flags in @c enum_binlog_stmt_flag.
2828 */
2829 inline void set_stmt_unsafe(enum_binlog_stmt_unsafe unsafe_type) {
2830 DBUG_TRACE;
2831 assert(unsafe_type >= 0 && unsafe_type < BINLOG_STMT_UNSAFE_COUNT);
2832 binlog_stmt_flags |= (1U << unsafe_type);
2833 return;
2834 }
2835
2836 /**
2837 Set the bits of binlog_stmt_flags determining the type of
2838 unsafeness of the current statement. No existing bits will be
2839 cleared, but new bits may be set.
2840
2841 @param flags A binary combination of zero or more bits, (1<<flag)
2842 where flag is a member of enum_binlog_stmt_unsafe.
2843 */
2845 DBUG_TRACE;
2846 assert((flags & ~BINLOG_STMT_UNSAFE_ALL_FLAGS) == 0);
2848 return;
2849 }
2850
2851 /**
2852 Return a binary combination of all unsafe warnings for the
2853 statement. If the statement has been marked as unsafe by the
2854 'flag' member of enum_binlog_stmt_unsafe, then the return value
2855 from this function has bit (1<<flag) set to 1.
2856 */
2858 DBUG_TRACE;
2860 }
2861
2862 /**
2863 Determine if this statement is a row injection.
2864
2865 @retval 0 if the statement is not a row injection
2866 @retval nonzero if the statement is a row injection
2867 */
2868 inline bool is_stmt_row_injection() const {
2869 return binlog_stmt_flags &
2871 }
2872
2873 /**
2874 Flag the statement as a row injection. A row injection is either
2875 a BINLOG statement, or a row event in the relay log executed by
2876 the slave SQL thread.
2877 */
2879 DBUG_TRACE;
2882 return;
2883 }
2884
2886 /*
2887 If a transactional table is about to be read. Note that
2888 a write implies a read.
2889 */
2891 /*
2892 If a non-transactional table is about to be read. Note that
2893 a write implies a read.
2894 */
2896 /*
2897 If a temporary transactional table is about to be read. Note
2898 that a write implies a read.
2899 */
2901 /*
2902 If a temporary non-transactional table is about to be read. Note
2903 that a write implies a read.
2904 */
2906 /*
2907 If a transactional table is about to be updated.
2908 */
2910 /*
2911 If a non-transactional table is about to be updated.
2912 */
2914 /*
2915 If a temporary transactional table is about to be updated.
2916 */
2918 /*
2919 If a temporary non-transactional table is about to be updated.
2920 */
2922 /*
2923 The last element of the enumeration. Please, if necessary add
2924 anything before this.
2925 */
2928
2929#ifndef NDEBUG
2930 static inline const char *stmt_accessed_table_string(
2931 enum_stmt_accessed_table accessed_table) {
2932 switch (accessed_table) {
2934 return "STMT_READS_TRANS_TABLE";
2935 break;
2937 return "STMT_READS_NON_TRANS_TABLE";
2938 break;
2940 return "STMT_READS_TEMP_TRANS_TABLE";
2941 break;
2943 return "STMT_READS_TEMP_NON_TRANS_TABLE";
2944 break;
2946 return "STMT_WRITES_TRANS_TABLE";
2947 break;
2949 return "STMT_WRITES_NON_TRANS_TABLE";
2950 break;
2952 return "STMT_WRITES_TEMP_TRANS_TABLE";
2953 break;
2955 return "STMT_WRITES_TEMP_NON_TRANS_TABLE";
2956 break;
2958 default:
2959 assert(0);
2960 break;
2961 }
2963 return "";
2964 }
2965#endif /* DBUG */
2966
2967#define BINLOG_DIRECT_ON \
2968 0xF0 /* unsafe when \
2969 --binlog-direct-non-trans-updates \
2970 is ON */
2971
2972#define BINLOG_DIRECT_OFF \
2973 0xF /* unsafe when \
2974 --binlog-direct-non-trans-updates \
2975 is OFF */
2976
2977#define TRX_CACHE_EMPTY 0x33 /* unsafe when trx-cache is empty */
2978
2979#define TRX_CACHE_NOT_EMPTY 0xCC /* unsafe when trx-cache is not empty */
2980
2981#define IL_LT_REPEATABLE 0xAA /* unsafe when < ISO_REPEATABLE_READ */
2982
2983#define IL_GTE_REPEATABLE 0x55 /* unsafe when >= ISO_REPEATABLE_READ */
2984
2985 /**
2986 Sets the type of table that is about to be accessed while executing a
2987 statement.
2989 @param accessed_table Enumeration type that defines the type of table,
2990 e.g. temporary, transactional, non-transactional.
2991 */
2992 inline void set_stmt_accessed_table(enum_stmt_accessed_table accessed_table) {
2993 DBUG_TRACE;
2994
2995 assert(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
2996 stmt_accessed_table_flag |= (1U << accessed_table);
2997
2998 return;
2999 }
3000
3001 /**
3002 Checks if a type of table is about to be accessed while executing a
3003 statement.
3004
3005 @param accessed_table Enumeration type that defines the type of table,
3006 e.g. temporary, transactional, non-transactional.
3008 @retval true if the type of the table is about to be accessed
3009 @retval false otherwise
3010 */
3011 inline bool stmt_accessed_table(enum_stmt_accessed_table accessed_table) {
3012 DBUG_TRACE;
3013
3014 assert(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
3015
3016 return (stmt_accessed_table_flag & (1U << accessed_table)) != 0;
3017 }
3018
3019 /*
3020 Checks if a mixed statement is unsafe.
3021
3022
3023 @param in_multi_stmt_transaction_mode defines if there is an on-going
3024 multi-transactional statement.
3025 @param binlog_direct defines if --binlog-direct-non-trans-updates is
3026 active.
3027 @param trx_cache_is_not_empty defines if the trx-cache is empty or not.
3028 @param trx_isolation defines the isolation level.
3029
3030 @return
3031 @retval true if the mixed statement is unsafe
3032 @retval false otherwise
3033 */
3034 inline bool is_mixed_stmt_unsafe(bool in_multi_stmt_transaction_mode,
3035 bool binlog_direct,
3036 bool trx_cache_is_not_empty,
3037 uint tx_isolation) {
3038 bool unsafe = false;
3039
3040 if (in_multi_stmt_transaction_mode) {
3041 const uint condition =
3042 (binlog_direct ? BINLOG_DIRECT_ON : BINLOG_DIRECT_OFF) &
3043 (trx_cache_is_not_empty ? TRX_CACHE_NOT_EMPTY : TRX_CACHE_EMPTY) &
3044 (tx_isolation >= ISO_REPEATABLE_READ ? IL_GTE_REPEATABLE
3046
3047 unsafe = (binlog_unsafe_map[stmt_accessed_table_flag] & condition);
3048
3049#if !defined(NDEBUG)
3050 DBUG_PRINT("LEX::is_mixed_stmt_unsafe",
3051 ("RESULT %02X %02X %02X\n", condition,
3054
3055 int type_in = 0;
3056 for (; type_in < STMT_ACCESS_TABLE_COUNT; type_in++) {
3058 DBUG_PRINT("LEX::is_mixed_stmt_unsafe",
3059 ("ACCESSED %s ", stmt_accessed_table_string(
3060 (enum_stmt_accessed_table)type_in)));
3061 }
3062#endif
3063 }
3064
3067 tx_isolation < ISO_REPEATABLE_READ)
3068 unsafe = true;
3071 tx_isolation < ISO_REPEATABLE_READ)
3072 unsafe = true;
3073
3074 return (unsafe);
3075 }
3076
3077 /**
3078 true if the parsed tree contains references to stored procedures
3079 or functions, false otherwise
3081 bool uses_stored_routines() const { return sroutines_list.elements != 0; }
3083 void set_using_match() { using_match = true; }
3084 bool get_using_match() { return using_match; }
3085
3087 bool is_stmt_unsafe_with_mixed_mode() const {
3089 }
3090
3091 private:
3092 /**
3093 Enumeration listing special types of statements.
3094
3095 Currently, the only possible type is ROW_INJECTION.
3096 */
3098 /**
3099 The statement is a row injection (i.e., either a BINLOG
3100 statement or a row event executed by the slave SQL thread).
3101 */
3103
3104 /** The last element of this enumeration type. */
3106 };
3107
3108 /**
3109 Bit field indicating the type of statement.
3110
3111 There are two groups of bits:
3112
3113 - The low BINLOG_STMT_UNSAFE_COUNT bits indicate the types of
3114 unsafeness that the current statement has.
3115
3116 - The next BINLOG_STMT_TYPE_COUNT bits indicate if the statement
3117 is of some special type.
3118
3119 This must be a member of LEX, not of THD: each stored procedure
3120 needs to remember its unsafeness state between calls and each
3121 stored procedure has its own LEX object (but no own THD object).
3122 */
3124
3125 /**
3126 Bit field that determines the type of tables that are about to be
3127 be accessed while executing a statement.
3128 */
3131 /**
3132 It will be set true if 'MATCH () AGAINST' is used in the statement.
3133 */
3134 bool using_match;
3135
3136 /**
3137 This flag is set to true if statement is unsafe to be binlogged in STATEMENT
3138 format, when in MIXED mode.
3139 Currently this flag is set to true if stored program used in statement has
3140 CREATE/DROP temporary table operation(s) as sub-statement(s).
3141 */
3142 bool stmt_unsafe_with_mixed_mode{false};
3143};
3144
3145/*
3146 st_parsing_options contains the flags for constructions that are
3147 allowed in the current statement.
3149
3151 bool allows_variable;
3152 bool allows_select_into;
3153
3154 st_parsing_options() { reset(); }
3155 void reset();
3156};
3158/**
3159 The state of the lexical parser, when parsing comments.
3160*/
3162 /**
3163 Not parsing comments.
3164 */
3165 NO_COMMENT,
3166
3167 /**
3168 Parsing comments that need to be preserved.
3169 (Copy '/' '*' and '*' '/' sequences to the preprocessed buffer.)
3170 Typically, these are user comments '/' '*' ... '*' '/'.
3171 */
3173
3174 /**
3175 Parsing comments that need to be discarded.
3176 (Don't copy '/' '*' '!' and '*' '/' sequences to the preprocessed buffer.)
3177 Typically, these are special comments '/' '*' '!' ... '*' '/',
3178 or '/' '*' '!' 'M' 'M' 'm' 'm' 'm' ... '*' '/', where the comment
3179 markers should not be expanded.
3180 */
3182};
3183
3184/**
3185 This class represents the character input stream consumed during lexical
3186 analysis.
3187
3188 In addition to consuming the input stream, this class performs some comment
3189 pre processing, by filtering out out-of-bound special text from the query
3190 input stream.
3191
3192 Two buffers, with pointers inside each, are maintained in parallel. The
3193 'raw' buffer is the original query text, which may contain out-of-bound
3194 comments. The 'cpp' (for comments pre processor) is the pre-processed buffer
3195 that contains only the query text that should be seen once out-of-bound data
3196 is removed.
3197*/
3198
3199class Lex_input_stream {
3200 public:
3201 /**
3202 Constructor
3204 @param grammar_selector_token_arg See grammar_selector_token.
3205 */
3206
3207 explicit Lex_input_stream(uint grammar_selector_token_arg)
3208 : grammar_selector_token(grammar_selector_token_arg) {}
3209
3210 /**
3211 Object initializer. Must be called before usage.
3213 @retval false OK
3214 @retval true Error
3215 */
3216 bool init(THD *thd, const char *buff, size_t length);
3217
3218 void reset(const char *buff, size_t length);
3219
3220 /**
3221 Set the echo mode.
3222
3223 When echo is true, characters parsed from the raw input stream are
3224 preserved. When false, characters parsed are silently ignored.
3225 @param echo the echo mode.
3226 */
3227 void set_echo(bool echo) { m_echo = echo; }
3228
3229 void save_in_comment_state() {
3232 }
3233
3237 }
3238
3239 /**
3240 Skip binary from the input stream.
3241 @param n number of bytes to accept.
3242 */
3243 void skip_binary(int n) {
3244 assert(m_ptr + n <= m_end_of_query);
3245 if (m_echo) {
3246 memcpy(m_cpp_ptr, m_ptr, n);
3247 m_cpp_ptr += n;
3248 }
3249 m_ptr += n;
3250 }
3251
3252 /**
3253 Get a character, and advance in the stream.
3254 @return the next character to parse.
3255 */
3256 unsigned char yyGet() {
3257 assert(m_ptr <= m_end_of_query);
3258 const char c = *m_ptr++;
3259 if (m_echo) *m_cpp_ptr++ = c;
3260 return c;
3261 }
3262
3263 /**
3264 Get the last character accepted.
3265 @return the last character accepted.
3266 */
3267 unsigned char yyGetLast() const { return m_ptr[-1]; }
3269 /**
3270 Look at the next character to parse, but do not accept it.
3271 */
3272 unsigned char yyPeek() const {
3273 assert(m_ptr <= m_end_of_query);
3274 return m_ptr[0];
3275 }
3276
3277 /**
3278 Look ahead at some character to parse.
3279 @param n offset of the character to look up
3280 */
3281 unsigned char yyPeekn(int n) const {
3282 assert(m_ptr + n <= m_end_of_query);
3283 return m_ptr[n];
3284 }
3285
3286 /**
3287 Cancel the effect of the last yyGet() or yySkip().
3288 Note that the echo mode should not change between calls to yyGet / yySkip
3289 and yyUnget. The caller is responsible for ensuring that.
3290 */
3291 void yyUnget() {
3292 m_ptr--;
3293 if (m_echo) m_cpp_ptr--;
3294 }
3296 /**
3297 Accept a character, by advancing the input stream.
3298 */
3299 void yySkip() {
3300 assert(m_ptr <= m_end_of_query);
3301 if (m_echo)
3302 *m_cpp_ptr++ = *m_ptr++;
3303 else
3304 m_ptr++;
3305 }
3306
3307 /**
3308 Accept multiple characters at once.
3309 @param n the number of characters to accept.
3310 */
3311 void yySkipn(int n) {
3312 assert(m_ptr + n <= m_end_of_query);
3313 if (m_echo) {
3314 memcpy(m_cpp_ptr, m_ptr, n);
3315 m_cpp_ptr += n;
3316 }
3317 m_ptr += n;
3318 }
3319
3320 /**
3321 Puts a character back into the stream, canceling
3322 the effect of the last yyGet() or yySkip().
3323 Note that the echo mode should not change between calls
3324 to unput, get, or skip from the stream.
3325 */
3326 char *yyUnput(char ch) {
3327 *--m_ptr = ch;
3328 if (m_echo) m_cpp_ptr--;
3329 return m_ptr;
3330 }
3331
3332 /**
3333 Inject a character into the pre-processed stream.
3334
3335 Note, this function is used to inject a space instead of multi-character
3336 C-comment. Thus there is no boundary checks here (basically, we replace
3337 N-chars by 1-char here).
3338 */
3339 char *cpp_inject(char ch) {
3340 *m_cpp_ptr = ch;
3341 return ++m_cpp_ptr;
3342 }
3343
3344 /**
3345 End of file indicator for the query text to parse.
3346 @return true if there are no more characters to parse
3347 */
3348 bool eof() const { return (m_ptr >= m_end_of_query); }
3349
3350 /**
3351 End of file indicator for the query text to parse.
3352 @param n number of characters expected
3353 @return true if there are less than n characters to parse
3355 bool eof(int n) const { return ((m_ptr + n) >= m_end_of_query); }
3356
3357 /** Get the raw query buffer. */
3358 const char *get_buf() const { return m_buf; }
3359
3360 /** Get the pre-processed query buffer. */
3361 const char *get_cpp_buf() const { return m_cpp_buf; }
3362
3363 /** Get the end of the raw query buffer. */
3364 const char *get_end_of_query() const { return m_end_of_query; }
3365
3366 /** Mark the stream position as the start of a new token. */
3367 void start_token() {
3369 m_tok_end = m_ptr;
3370
3373 }
3374
3375 /**
3376 Adjust the starting position of the current token.
3377 This is used to compensate for starting whitespace.
3378 */
3379 void restart_token() {
3382 }
3383
3384 /** Get the token start position, in the raw buffer. */
3385 const char *get_tok_start() const { return m_tok_start; }
3386
3387 /** Get the token start position, in the pre-processed buffer. */
3388 const char *get_cpp_tok_start() const { return m_cpp_tok_start; }
3389
3390 /** Get the token end position, in the raw buffer. */
3391 const char *get_tok_end() const { return m_tok_end; }
3392
3393 /** Get the token end position, in the pre-processed buffer. */
3394 const char *get_cpp_tok_end() const { return m_cpp_tok_end; }
3395
3396 /** Get the current stream pointer, in the raw buffer. */
3397 const char *get_ptr() const { return m_ptr; }
3398
3399 /** Get the current stream pointer, in the pre-processed buffer. */
3400 const char *get_cpp_ptr() const { return m_cpp_ptr; }
3401
3402 /** Get the length of the current token, in the raw buffer. */
3403 uint yyLength() const {
3404 /*
3405 The assumption is that the lexical analyser is always 1 character ahead,
3406 which the -1 account for.
3407 */
3408 assert(m_ptr > m_tok_start);
3409 return (uint)((m_ptr - m_tok_start) - 1);
3410 }
3411
3412 /** Get the utf8-body string. */
3413 const char *get_body_utf8_str() const { return m_body_utf8; }
3414
3415 /** Get the utf8-body length. */
3420 void body_utf8_start(THD *thd, const char *begin_ptr);
3421 void body_utf8_append(const char *ptr);
3422 void body_utf8_append(const char *ptr, const char *end_ptr);
3424 const CHARSET_INFO *txt_cs,
3425 const char *end_ptr);
3426
3427 uint get_lineno(const char *raw_ptr) const;
3428
3429 /** Current thread. */
3430 THD *m_thd;
3431
3432 /** Current line number. */
3433 uint yylineno;
3434
3435 /** Length of the last token parsed. */
3436 uint yytoklen;
3437
3438 /** Interface with bison, value of the last token parsed. */
3440
3441 /**
3442 LALR(2) resolution, look ahead token.
3443 Value of the next token to return, if any,
3444 or -1, if no token was parsed in advance.
3445 Note: 0 is a legal token, and represents YYEOF.
3446 */
3447 int lookahead_token;
3448
3449 /** LALR(2) resolution, value of the look ahead token.*/
3451
3452 /// Skip adding of the current token's digest since it is already added
3453 ///
3454 /// Usually we calculate a digest token by token at the top-level function
3455 /// of the lexer: MYSQLlex(). However, some complex ("hintable") tokens break
3456 /// that data flow: for example, the `SELECT /*+ HINT(t) */` is the single
3457 /// token from the main parser's point of view, and we add the "SELECT"
3458 /// keyword to the digest buffer right after the lex_one_token() call,
3459 /// but the "/*+ HINT(t) */" is a sequence of separate tokens from the hint
3460 /// parser's point of view, and we add those tokens to the digest buffer
3461 /// *inside* the lex_one_token() call. Thus, the usual data flow adds
3462 /// tokens from the "/*+ HINT(t) */" string first, and only than it appends
3463 /// the "SELECT" keyword token to that stream: "/*+ HINT(t) */ SELECT".
3464 /// This is not acceptable, since we use the digest buffer to restore
3465 /// query strings in their normalized forms, so the order of added tokens is
3466 /// important. Thus, we add tokens of "hintable" keywords to a digest buffer
3467 /// right in the hint parser and skip adding of them at the caller with the
3468 /// help of skip_digest flag.
3470
3471 void add_digest_token(uint token, Lexer_yystype *yylval);
3472
3473 void reduce_digest_token(uint token_left, uint token_right);
3474
3475 /**
3476 True if this scanner tokenizes a partial query (partition expression,
3477 generated column expression etc.)
3478
3479 @return true if parsing a partial query, otherwise false.
3480 */
3481 bool is_partial_parser() const { return grammar_selector_token >= 0; }
3482
3483 /**
3484 Outputs warnings on deprecated charsets in complete SQL statements
3486 @param [in] cs The character set/collation to check for a deprecation.
3487 @param [in] alias The name/alias of @p cs.
3488 */
3490 const char *alias) const {
3491 if (!is_partial_parser()) {
3493 }
3494 }
3495
3496 /**
3497 Outputs warnings on deprecated collations in complete SQL statements
3498
3499 @param [in] collation The collation to check for a deprecation.
3500 */
3502 if (!is_partial_parser()) {
3504 }
3505 }
3506
3508
3509 private:
3510 /** Pointer to the current position in the raw input stream. */
3511 char *m_ptr;
3512
3513 /** Starting position of the last token parsed, in the raw buffer. */
3514 const char *m_tok_start;
3515
3516 /** Ending position of the previous token parsed, in the raw buffer. */
3517 const char *m_tok_end;
3518
3519 /** End of the query text in the input stream, in the raw buffer. */
3520 const char *m_end_of_query;
3521
3522 /** Beginning of the query text in the input stream, in the raw buffer. */
3523 const char *m_buf;
3524
3525 /** Length of the raw buffer. */
3526 size_t m_buf_length;
3527
3528 /** Echo the parsed stream to the pre-processed buffer. */
3529 bool m_echo;
3530 bool m_echo_saved;
3531
3532 /** Pre-processed buffer. */
3533 char *m_cpp_buf;
3534
3535 /** Pointer to the current position in the pre-processed input stream. */
3536 char *m_cpp_ptr;
3537
3538 /**
3539 Starting position of the last token parsed,
3540 in the pre-processed buffer.
3541 */
3542 const char *m_cpp_tok_start;
3543
3544 /**
3545 Ending position of the previous token parsed,
3546 in the pre-processed buffer.
3547 */
3548 const char *m_cpp_tok_end;
3549
3550 /** UTF8-body buffer created during parsing. */
3551 char *m_body_utf8;
3552
3553 /** Pointer to the current position in the UTF8-body buffer. */
3554 char *m_body_utf8_ptr;
3555
3556 /**
3557 Position in the pre-processed buffer. The query from m_cpp_buf to
3558 m_cpp_utf_processed_ptr is converted to UTF8-body.
3559 */
3560 const char *m_cpp_utf8_processed_ptr;
3561
3562 public:
3563 /** Current state of the lexical analyser. */
3565
3566 /**
3567 Position of ';' in the stream, to delimit multiple queries.
3568 This delimiter is in the raw buffer.
3569 */
3570 const char *found_semicolon;
3571
3572 /** Token character bitmaps, to detect 7bit strings. */
3574
3575 /** SQL_MODE = IGNORE_SPACE. */
3576 bool ignore_space;
3577
3578 /**
3579 true if we're parsing a prepared statement: in this mode
3580 we should allow placeholders.
3581 */
3582 bool stmt_prepare_mode;
3583 /**
3584 true if we should allow multi-statements.
3585 */
3586 bool multi_statements;
3587
3588 /** State of the lexical analyser for comments. */
3591
3592 /**
3593 Starting position of the TEXT_STRING or IDENT in the pre-processed
3594 buffer.
3595
3596 NOTE: this member must be used within MYSQLlex() function only.
3597 */
3598 const char *m_cpp_text_start;
3599
3600 /**
3601 Ending position of the TEXT_STRING or IDENT in the pre-processed
3602 buffer.
3603
3604 NOTE: this member must be used within MYSQLlex() function only.
3605 */
3606 const char *m_cpp_text_end;
3607
3608 /**
3609 Character set specified by the character-set-introducer.
3610
3611 NOTE: this member must be used within MYSQLlex() function only.
3612 */
3614
3615 /**
3616 Current statement digest instrumentation.
3617 */
3619
3620 /**
3621 The synthetic 1st token to prepend token stream with.
3622
3623 This token value tricks parser to simulate multiple %start-ing points.
3624 Currently the grammar is aware of 4 such synthetic tokens:
3625 1. GRAMMAR_SELECTOR_PART for partitioning stuff from DD,
3626 2. GRAMMAR_SELECTOR_GCOL for generated column stuff from DD,
3627 3. GRAMMAR_SELECTOR_EXPR for generic single expressions from DD/.frm.
3628 4. GRAMMAR_SELECTOR_CTE for generic subquery expressions from CTEs.
3629 5. -1 when parsing with the main grammar (no grammar selector available).
3630
3631 @note yylex() is expected to return the value of type int:
3632 0 is for EOF and everything else for real token numbers.
3633 Bison, in its turn, generates positive token numbers.
3634 So, the negative grammar_selector_token means "not a token".
3635 In other words, -1 is "empty value".
3636 */
3637 const int grammar_selector_token;
3639 bool text_string_is_7bit() const { return !(tok_bitmap & 0x80); }
3643 public:
3644 String column;
3645 uint rights;
3646 LEX_COLUMN(const String &x, const uint &y) : column(x), rights(y) {}
3647};
3648
3649enum class role_enum;
3651/*
3652 This structure holds information about grantor's context
3653*/
3654class LEX_GRANT_AS {
3655 public:
3657 void cleanup();
3659 public:
3660 bool grant_as_used;
3662 LEX_USER *user;
3664};
3665
3666/**
3667 The LEX object currently serves three different purposes:
3668
3669 - It contains some universal properties of an SQL command, such as
3670 sql_command, presence of IGNORE in data change statement syntax, and list
3671 of tables (query_tables).
3672
3673 - It contains some execution state variables, like m_exec_started
3674 (set to true when execution is started), plugins (list of plugins used
3675 by statement), insert_update_values_map (a map of objects used by certain
3676 INSERT statements), etc.
3677
3678 - It contains a number of members that should be local to subclasses of
3679 Sql_cmd, like purge_value_list (for the PURGE command), kill_value_list
3680 (for the KILL command).
3681
3682 The LEX object is strictly a part of class Sql_cmd, for those SQL commands
3683 that are represented by an Sql_cmd class. For the remaining SQL commands,
3684 it is a standalone object linked to the current THD.
3685
3686 The lifecycle of a LEX object is as follows:
3687
3688 - The LEX object is constructed either on the execution mem_root
3689 (for regular statements), on a Prepared_statement mem_root (for
3690 prepared statements), on an SP mem_root (for stored procedure instructions),
3691 or created on the current mem_root for short-lived uses.
3692
3693 - Call lex_start() to initialize a LEX object before use.
3694 This initializes the execution state part of the object.
3695 It also calls LEX::reset() to ensure that all members are properly inited.
3696
3697 - Parse and resolve the statement, using the LEX as a work area.
3698
3699 - Execute an SQL command: call set_exec_started() when starting to execute
3700 (actually when starting to optimize).
3701 Typically call is_exec_started() to distinguish between preparation
3702 and optimization/execution stages of SQL command execution.
3703
3704 - Call clear_execution() when execution is finished. This will clear all
3705 execution state associated with the SQL command, it also includes calling
3706 LEX::reset_exec_started().
3707
3708 @todo - Create subclasses of Sql_cmd to contain data that are local
3709 to specific commands.
3710
3711 @todo - Create a Statement context object that will hold the execution state
3712 part of struct LEX.
3713
3714 @todo - Ensure that a LEX struct is never reused, thus making e.g
3715 LEX::reset() redundant.
3716*/
3718struct LEX : public Query_tables_list {
3719 friend bool lex_start(THD *thd);
3721 Query_expression *unit; ///< Outer-most query expression
3722 /// @todo: query_block can be replaced with unit->first-select()
3723 Query_block *query_block; ///< First query block
3724 Query_block *all_query_blocks_list; ///< List of all query blocks
3725 private:
3726 /* current Query_block in parsing */
3728
3729 public:
3730 inline Query_block *current_query_block() const {
3731 return m_current_query_block;
3732 }
3733
3734 /*
3735 We want to keep current_thd out of header files, so the debug assert
3736 is moved to the .cc file.
3737 */
3739 inline void set_current_query_block(Query_block *select) {
3740#ifndef NDEBUG
3742#endif
3744 }
3745 /// @return true if this is an EXPLAIN statement
3746 bool is_explain() const { return explain_format != nullptr; }
3747 bool is_explain_analyze = false;
3748 /**
3749 Whether the currently-running query should be (attempted) executed in
3750 the hypergraph optimizer. This will not change after the query is
3751 done parsing, so you can use it in any query phase to e.g. figure out
3752 whether to inhibit some transformation that the hypergraph optimizer
3753 does not properly understand yet.
3758 char *to_log; /* For PURGE MASTER LOGS TO */
3760 // Widcard from SHOW ... LIKE <wildcard> statements.
3764 nullptr, 0}; ///< Argument of the BINLOG event statement.
3771 THD *thd;
3772
3773 /* Optimizer hints */
3776 /* maintain a list of used plugins for this LEX */
3781 /// Table being inserted into (may be a view)
3783 /// Leaf table being inserted into (always a base table)
3785
3786 /** SELECT of CREATE VIEW statement */
3788
3789 /* Partition info structure filled in by PARTITION BY parse part */
3791
3793 The definer of the object being created (view, trigger, stored routine).
3794 I.e. the value of DEFINER clause.
3804
3805 // PURGE statement-specific fields:
3807
3808 // KILL statement-specific fields:
3810
3811 // other stuff:
3813 List<Item_func_set_user_var> set_var_list; // in-query assignment list
3814 /**
3815 List of placeholders ('?') for parameters of a prepared statement. Because
3816 we append to this list during parsing, it is naturally sorted by
3817 position of the '?' in the query string. The code which fills placeholders
3818 with user-supplied values, and the code which writes a query for
3819 statement-based logging, rely on this order.
3820 This list contains only real placeholders, not the clones which originate
3821 in a re-parsed CTE definition.
3822 */
3824
3826
3827 void insert_values_map(Item_field *f1, Field *f2) {
3829 insert_update_values_map = new std::map<Item_field *, Field *>;
3830 insert_update_values_map->insert(std::make_pair(f1, f2));
3831 }
3832 void destroy_values_map() {
3834 insert_update_values_map->clear();
3836 insert_update_values_map = nullptr;
3837 }
3838 }
3839 void clear_values_map() {
3842 }
3843 }
3844 bool has_values_map() const { return insert_update_values_map != nullptr; }
3845 std::map<Item_field *, Field *>::iterator begin_values_map() {
3846 return insert_update_values_map->begin();
3847 }
3848 std::map<Item_field *, Field *>::iterator end_values_map() {
3849 return insert_update_values_map->end();
3850 }
3851
3852 private:
3853 /*
3854 With Visual Studio, an std::map will always allocate two small objects
3855 on the heap. Sometimes we put LEX objects in a MEM_ROOT, and never run
3856 the LEX DTOR. To avoid memory leaks, put this std::map on the heap,
3857 and call clear_values_map() at the end of each statement.
3858 */
3859 std::map<Item_field *, Field *> *insert_update_values_map;
3860
3861 public:
3862 /*
3863 A stack of name resolution contexts for the query. This stack is used
3864 at parse time to set local name resolution contexts for various parts
3865 of a query. For example, in a JOIN ... ON (some_condition) clause the
3866 Items in 'some_condition' must be resolved only against the operands
3867 of the the join, and not against the whole clause. Similarly, Items in
3868 subqueries should be resolved against the subqueries (and outer queries).
3869 The stack is used in the following way: when the parser detects that
3870 all Items in some clause need a local context, it creates a new context
3871 and pushes it on the stack. All newly created Items always store the
3872 top-most context in the stack. Once the parser leaves the clause that
3873 required a local context, the parser pops the top-most context.
3879 HA_CHECK_OPT check_opt; // check/repair options
3882 LEX_MASTER_INFO mi; // used by CHANGE MASTER
3887 ulong type;
3888 /**
3889 This field is used as a work field during resolving to validate
3890 the use of aggregate functions. For example in a query
3891 SELECT ... FROM ...WHERE MIN(i) == 1 GROUP BY ... HAVING MIN(i) > 2
3892 MIN(i) in the WHERE clause is not allowed since only non-aggregated data
3893 is present, whereas MIN(i) in the HAVING clause is allowed because HAVING
3894 operates on the output of a grouping operation.
3895 Each query block is assigned a nesting level. This field is a bit field
3896 that contains the value one in the position of that nesting level if
3897 aggregate functions are allowed for that query block.
3898 */
3900 /**
3901 Windowing functions are not allowed in HAVING - in contrast to group
3902 aggregates - then we need to be stricter than allow_sum_func.
3903 One bit per query block, as allow_sum_func.
3904 */
3907 /// If true: during prepare, we did a subquery transformation (IN-to-EXISTS,
3908 /// SOME/ANY) that doesn't currently work for subquery to a derived table
3909 /// transformation.
3911
3913
3914 /*
3915 Usually `expr` rule of yacc is quite reused but some commands better
3916 not support subqueries which comes standard with this rule, like
3917 KILL, HA_READ, CREATE/ALTER EVENT etc. Set this to `false` to get
3918 syntax error back.
3919 */
3921 /**
3922 If currently re-parsing a CTE's definition, this is the offset in bytes
3923 of that definition in the original statement which had the WITH
3924 clause. Otherwise this is 0.
3925 */
3927 /**
3928 If currently re-parsing a condition which is pushed down to a derived
3929 table, this will be set to true.
3930 */
3932 /**
3933 If currently re-parsing a condition that is being pushed down to a
3934 derived table, this has the positions of all the parameters that are
3935 part of that condition in the original statement. Otherwise it is empty.
3939 enum SSL_type ssl_type; /* defined in violite.h */
3945 /// QUERY ID for SHOW PROFILE
3947 uint profile_options;
3948 uint grant, grant_tot_col;
3949 /**
3950 Set to true when GRANT ... GRANT OPTION ... TO ...
3951 is used (vs. GRANT ... WITH GRANT OPTION).
3952 The flag is used by @ref mysql_grant to grant GRANT OPTION (@ref GRANT_ACL)
3953 to all dynamic privileges.
3957 int select_number; ///< Number of query block (by EXPLAIN)
3960 /**
3961 @todo ensure that correct CONTEXT_ANALYSIS_ONLY is set for all preparation
3962 code, so we can fully rely on this field.
3963 */
3965 bool drop_if_exists;
3966 /**
3967 refers to optional IF EXISTS clause in REVOKE sql. This flag when set to
3968 true will report warnings in case privilege being granted is not granted to
3969 given user/role. When set to false error is reported.
3970 */
3971 bool grant_if_exists;
3972 /**
3973 refers to optional IGNORE UNKNOWN USER clause in REVOKE sql. This flag when
3974 set to true will report warnings in case target user/role for which
3975 privilege being granted does not exists. When set to false error is
3976 reported.
3980 bool autocommit;
3982 // For show commands to show hidden columns and indexes.
3983 bool m_extended_show;
3984
3985 enum enum_yes_no_unknown tx_chain, tx_release;
3986
3987 /**
3988 Whether this query will return the same answer every time, given unchanged
3989 data. Used to be for the query cache, but is now used to find out if an
3990 expression is usable for partitioning.
3991 */
3994 private:
3995 /// True if statement references UDF functions
3996 bool m_has_udf{false};
3999 public:
4000 bool is_ignore() const { return ignore; }
4001 void set_ignore(bool ignore_param) { ignore = ignore_param; }
4002 void set_has_udf() { m_has_udf = true; }
4003 bool has_udf() const { return m_has_udf; }
4006 /* Prepared statements SQL syntax:*/
4007 LEX_CSTRING prepared_stmt_name; /* Statement name (in all queries) */
4009 Prepared statement query text or name of variable that holds the
4010 prepared statement (in PREPARE ... queries)
4011 */
4013 /* If true, prepared_stmt_code is a name of variable that holds the query */
4015 /* Names of user variables holding parameters (in EXECUTE) */
4019 bool sp_lex_in_use; /* Keep track on lex usage in SPs for error handling */
4020 bool all_privileges;
4024
4025 private:
4026 bool m_broken; ///< see mark_broken()
4027 /**
4028 Set to true when execution has started (after parsing, tables opened and
4029 query preparation is complete. Used to track arena state for SPs).
4030 */
4031 bool m_exec_started;
4032 /**
4033 Set to true when execution is completed, ie optimization has been done
4034 and execution is successful or ended in error.
4035 */
4036 bool m_exec_completed;
4037 /**
4038 Current SP parsing context.
4039 @see also sp_head::m_root_parsing_ctx.
4040 */
4043 /**
4044 Statement context for Query_block::make_active_options.
4045 */
4047
4048 public:
4049 /**
4050 Gets the options that have been set for this statement. The options are
4051 propagated to the Query_block objects and should usually be read with
4052 #Query_block::active_options().
4053
4054 @return a bit set of options set for this statement
4055 */
4057 /**
4058 Add options to values of m_statement_options. options is an ORed
4059 bit set of options defined in query_options.h
4061 @param options Add this set of options to the set already in
4062 m_statement_options
4066 }
4067 bool is_broken() const { return m_broken; }
4068 /**
4069 Certain permanent transformations (like in2exists), if they fail, may
4070 leave the LEX in an inconsistent state. They should call the
4071 following function, so that this LEX is not reused by another execution.
4073 @todo If lex_start () were a member function of LEX, the "broken"
4074 argument could always be "true" and thus could be removed.
4075 */
4076 void mark_broken(bool broken = true) {
4077 if (broken) {
4078 /*
4079 "OPEN <cursor>" cannot be re-prepared if the cursor uses no tables
4080 ("SELECT FROM DUAL"). Indeed in that case cursor_query is left empty
4081 in constructions of sp_instr_cpush, and thus
4082 sp_lex_instr::parse_expr() cannot re-prepare. So we mark the statement
4083 as broken only if tables are used.
4084 */
4085 if (is_metadata_used()) m_broken = true;
4086 } else
4087 m_broken = false;
4089
4091
4092 void cleanup(bool full) {
4093 unit->cleanup(full);
4094 if (full) {
4099
4100 bool is_exec_started() const { return m_exec_started; }
4101 void set_exec_started() { m_exec_started = true; }
4102 void reset_exec_started() {
4103 m_exec_started = false;
4104 m_exec_completed = false;
4105 }
4106 /**
4107 Check whether the statement has been executed (regardless of completion -
4108 successful or in error).
4109 Check this instead of Query_expression::is_executed() to determine
4110 the state of a complete statement.
4111 */
4112 bool is_exec_completed() const { return m_exec_completed; }
4113 void set_exec_completed() { m_exec_completed = true; }
4115
4119
4120 /// Check if the current statement uses meta-data (uses a table or a stored
4121 /// routine).
4122 bool is_metadata_used() const {
4123 return query_tables != nullptr || has_udf() ||
4124 (sroutines != nullptr && !sroutines->empty());
4125 }
4127 public:
4129
4131
4132 bool only_view; /* used for SHOW CREATE TABLE/VIEW */
4133 /*
4134 view created to be run from definer (standard behaviour)
4135 */
4137
4138 /**
4139 Intended to point to the next word after DEFINER-clause in the
4140 following statements:
4141
4142 - CREATE TRIGGER (points to "TRIGGER");
4143 - CREATE PROCEDURE (points to "PROCEDURE");
4144 - CREATE FUNCTION (points to "FUNCTION" or "AGGREGATE");
4145 - CREATE EVENT (points to "EVENT")
4147 This pointer is required to add possibly omitted DEFINER-clause to the
4148 DDL-statement before dumping it to the binlog.
4149 */
4150 const char *stmt_definition_begin;
4151 const char *stmt_definition_end;
4152
4153 /**
4154 During name resolution search only in the table list given by
4155 Name_resolution_context::first_name_resolution_table and
4156 Name_resolution_context::last_name_resolution_table
4157 (see Item_field::fix_fields()).
4158 */
4160
4161 bool is_lex_started; /* If lex_start() did run. For debugging. */
4162 /// Set to true while resolving values in ON DUPLICATE KEY UPDATE clause
4166
4167 // Maximum execution time for a statement.
4168 ulong max_execution_time;
4169
4171 To flag the current statement as dependent for binary logging
4172 on explicit_defaults_for_timestamp
4173 */
4175
4176 /**
4177 Used to inform the parser whether it should contextualize the parse
4178 tree. When we get a pure parser this will not be needed.
4179 */
4180 bool will_contextualize;
4181
4182 LEX();
4184 virtual ~LEX();
4185
4186 /// Destroy contained objects, but not the LEX object itself.
4187 void destroy() {
4188 if (unit == nullptr) return;
4189 unit->destroy();
4190 unit = nullptr;
4191 query_block = nullptr;
4192 all_query_blocks_list = nullptr;
4193 m_current_query_block = nullptr;
4195 }
4196
4197 /// Reset query context to initial state
4198 void reset();
4199
4200 /// Create an empty query block within this LEX object.
4202
4203 /// Create query expression object that contains one query block.
4204 Query_block *new_query(Query_block *curr_query_block);
4205
4206 /// Create query block and attach it to the current query expression.
4208
4209 /// Create top-level query expression and query block.
4210 bool new_top_level_query();
4211
4212 /// Create query expression and query block in existing memory objects.
4213 void new_static_query(Query_expression *sel_query_expression,
4214 Query_block *select);
4215
4216 /// Create query expression under current_query_block and a query block under
4217 /// the new query expression. The new query expression is linked in under
4218 /// current_query_block. The new query block is linked in under the new
4219 /// query expression.
4220 ///
4221 /// @param thd current session context
4222 /// @param current_query_block the root under which we create the new
4223 /// expression
4224 /// and block
4225 /// @param where_clause any where clause for the block
4226 /// @param having_clause any having clause for the block
4227 /// @param ctx the parsing context
4228 ///
4229 /// @returns the new query expression, or nullptr on error.
4231 THD *thd, Query_block *current_query_block, Item *where_clause,
4232 Item *having_clause, enum_parsing_context ctx);
4233
4234 inline bool is_ps_or_view_context_analysis() {
4237 }
4238
4239 inline bool is_view_context_analysis() {
4241 }
4242
4243 void clear_execution();
4244
4245 /**
4246 Set the current query as uncacheable.
4247
4248 @param curr_query_block Current select query block
4249 @param cause Why this query is uncacheable.
4250
4251 @details
4252 All query blocks representing subqueries, from the current one up to
4253 the outer-most one, but excluding the main query block, are also set
4254 as uncacheable.
4255 */
4256 void set_uncacheable(Query_block *curr_query_block, uint8 cause) {
4257 safe_to_cache_query = false;
4258
4259 if (m_current_query_block == nullptr) return;
4260 Query_block *sl;
4261 Query_expression *un;
4262 for (sl = curr_query_block, un = sl->master_query_expression(); un != unit;
4263 sl = sl->outer_query_block(), un = sl->master_query_expression()) {
4264 sl->uncacheable |= cause;
4265 un->uncacheable |= cause;
4266 }
4267 }
4269
4270 Table_ref *unlink_first_table(bool *link_to_local);
4271 void link_first_table_back(Table_ref *first, bool link_to_local);
4273
4275
4277 for (Table_ref *tr = insert_table->first_leaf_table(); tr != nullptr;
4278 tr = tr->next_leaf)
4279 tr->restore_properties();
4280 }
4281
4283
4284 bool can_use_merged();
4285 bool can_not_use_merged();
4286 bool need_correct_ident();
4287 /*
4288 Is this update command where 'WHITH CHECK OPTION' clause is important
4289
4290 SYNOPSIS
4291 LEX::which_check_option_applicable()
4292
4293 RETURN
4294 true have to take 'WHITH CHECK OPTION' clause into account
4295 false 'WHITH CHECK OPTION' clause do not need
4296 */
4297 inline bool which_check_option_applicable() {
4298 switch (sql_command) {
4299 case SQLCOM_UPDATE:
4301 case SQLCOM_INSERT:
4303 case SQLCOM_REPLACE:
4305 case SQLCOM_LOAD:
4306 return true;
4307 default:
4308 return false;
4309 }
4311
4313
4315 return context_stack.push_front(context);
4316 }
4317
4318 void pop_context() { context_stack.pop(); }
4319
4320 bool copy_db_to(char const **p_db, size_t *p_db_length) const;
4321
4322 bool copy_db_to(char **p_db, size_t *p_db_length) const {
4323 return copy_db_to(const_cast<const char **>(p_db), p_db_length);
4324 }
4325
4327
4330
4331 bool table_or_sp_used();
4332
4333 /**
4334 @brief check if the statement is a single-level join
4335 @return result of the check
4336 @retval true The statement doesn't contain subqueries, unions and
4337 stored procedure calls.
4338 @retval false There are subqueries, UNIONs or stored procedure calls.
4339 */
4340 bool is_single_level_stmt() {
4341 /*
4342 This check exploits the fact that the last added to all_select_list is
4343 on its top. So query_block (as the first added) will be at the tail
4344 of the list.
4345 */
4347 (sroutines == nullptr || sroutines->empty())) {
4349 return true;
4350 }
4351 return false;
4352 }
4353
4354 void release_plugins();
4355
4356 /**
4357 IS schema queries read some dynamic table statistics from SE.
4358 These statistics are cached, to avoid opening of table more
4359 than once while preparing a single output record buffer.
4360 */
4363
4364 bool accept(Select_lex_visitor *visitor);
4365
4366 bool set_wild(LEX_STRING);
4367 void clear_privileges();
4368
4369 bool make_sql_cmd(Parse_tree_root *parse_tree);
4370
4371 private:
4372 /**
4373 Context object used by secondary storage engines to store query
4374 state during optimization and execution.
4375 */
4377
4378 public:
4379 /**
4380 Gets the secondary engine execution context for this statement.
4381 */
4383 const {
4385 }
4386
4387 /**
4388 Sets the secondary engine execution context for this statement.
4389 The old context object is destroyed, if there is one. Can be set
4390 to nullptr to destroy the old context object and clear the
4391 pointer.
4392
4393 The supplied context object should be allocated on the execution
4394 MEM_ROOT, so that its memory doesn't have to be manually freed
4395 after query execution.
4396 */
4399
4400 private:
4402
4403 public:
4406 }
4407
4410 }
4411
4412 private:
4414
4415 public:
4418 }
4419
4422 }
4425
4426 private:
4427 bool rewrite_required{false};
4429 public:
4430 void set_rewrite_required() { rewrite_required = true; }
4431 void reset_rewrite_required() { rewrite_required = false; }
4432 bool is_rewrite_required() { return rewrite_required; }
4433};
4434
4436 RAII class to ease the call of LEX::mark_broken() if error.
4437 Used during preparation and optimization of DML queries.
4438*/
4440 public:
4441 Prepare_error_tracker(THD *thd_arg) : thd(thd_arg) {}
4443
4444 private:
4445 THD *const thd;
4446};
4447
4448/**
4449 The internal state of the syntax parser.
4450 This object is only available during parsing,
4451 and is private to the syntax parser implementation (sql_yacc.yy).
4452*/
4453class Yacc_state {
4454 public:
4456 reset();
4457 }
4458
4459 void reset() {
4460 if (yacc_yyss != nullptr) {
4462 yacc_yyss = nullptr;
4463 }
4464 if (yacc_yyvs != nullptr) {
4466 yacc_yyvs = nullptr;
4467 }
4468 if (yacc_yyls != nullptr) {
4470 yacc_yyls = nullptr;
4471 }
4474 }
4475
4476 ~Yacc_state();
4477
4478 /**
4479 Reset part of the state which needs resetting before parsing
4480 substatement.
4481 */
4485 }
4486
4487 /**
4488 Bison internal state stack, yyss, when dynamically allocated using
4489 my_yyoverflow().
4490 */
4492
4493 /**
4494 Bison internal semantic value stack, yyvs, when dynamically allocated using
4495 my_yyoverflow().
4496 */
4498
4499 /**
4500 Bison internal location value stack, yyls, when dynamically allocated using
4501 my_yyoverflow().
4502 */
4504
4505 /**
4506 Type of lock to be used for tables being added to the statement's
4507 table list in table_factor, table_alias_ref, single_multi and
4508 table_wild_one rules.
4509 Statements which use these rules but require lock type different
4510 from one specified by this member have to override it by using
4511 Query_block::set_lock_for_tables() method.
4512
4513 The default value of this member is TL_READ_DEFAULT. The only two
4514 cases in which we change it are:
4515 - When parsing SELECT HIGH_PRIORITY.
4516 - Rule for DELETE. In which we use this member to pass information
4517 about type of lock from delete to single_multi part of rule.
4518
4519 We should try to avoid introducing new use cases as we would like
4520 to get rid of this member eventually.
4521 */
4523
4524 /**
4525 The type of requested metadata lock for tables added to
4526 the statement table list.
4527 */
4529
4530 /*
4531 TODO: move more attributes from the LEX structure here.
4532 */
4533};
4534
4535/**
4536 Input parameters to the parser.
4537*/
4538struct Parser_input {
4539 /**
4540 True if the text parsed corresponds to an actual query,
4541 and not another text artifact.
4542 This flag is used to disable digest parsing of nested:
4543 - view definitions
4544 - table trigger definitions
4545 - table partition definitions
4546 - event scheduler event definitions
4547 */
4548 bool m_has_digest;
4549 /**
4550 True if the caller needs to compute a digest.
4551 This flag is used to request explicitly a digest computation,
4552 independently of the performance schema configuration.
4553 */
4554 bool m_compute_digest;
4555
4556 Parser_input() : m_has_digest(false), m_compute_digest(false) {}
4557};
4558
4559/**
4560 Internal state of the parser.
4561 The complete state consist of:
4562 - input parameters that control the parser behavior
4563 - state data used during lexical parsing,
4564 - state data used during syntactic parsing.
4565*/
4566class Parser_state {
4567 protected:
4568 /**
4569 Constructor for special parsers of partial SQL clauses (DD)
4570
4571 @param grammar_selector_token See Lex_input_stream::grammar_selector_token
4572 */
4573 explicit Parser_state(int grammar_selector_token)
4574 : m_input(), m_lip(grammar_selector_token), m_yacc(), m_comment(false) {}
4575
4576 public:
4577 Parser_state() : m_input(), m_lip(~0U), m_yacc(), m_comment(false) {}
4578
4579 /**
4580 Object initializer. Must be called before usage.
4582 @retval false OK
4583 @retval true Error
4584 */
4585 bool init(THD *thd, const char *buff, size_t length) {
4586 return m_lip.init(thd, buff, length);
4587 }
4588
4589 void reset(const char *found_semicolon, size_t length) {
4590 m_lip.reset(found_semicolon, length);
4592 }
4594 /// Signal that the current query has a comment
4595 void add_comment() { m_comment = true; }
4596 /// Check whether the current query has a comment
4597 bool has_comment() const { return m_comment; }
4598
4599 public:
4603 /**
4604 Current performance digest instrumentation.
4605 */
4607
4608 private:
4609 bool m_comment; ///< True if current query contains comments
4610};
4612/**
4613 Parser state for partition expression parser (.frm/DD stuff)
4614*/
4616 public:
4618
4620};
4622/**
4623 Parser state for generated column expression parser (.frm/DD stuff)
4624*/
4626 public:
4628
4630};
4632/**
4633 Parser state for single expression parser (.frm/DD stuff)
4634*/
4636 public:
4638
4639 Item *result;
4640};
4642/**
4643 Parser state for CTE subquery parser
4644*/
4646 public:
4648
4650};
4651
4653 Parser state for Derived table's condition parser.
4654 (Used in condition pushdown to derived tables)
4655*/
4657 public:
4661};
4662
4663struct st_lex_local : public LEX {
4664 static void *operator new(size_t size) noexcept {
4665 return (*THR_MALLOC)->Alloc(size);
4666 }
4667 static void *operator new(size_t size, MEM_ROOT *mem_root,
4668 const std::nothrow_t &arg
4669 [[maybe_unused]] = std::nothrow) noexcept {
4670 return mem_root->Alloc(size);
4671 }
4672 static void operator delete(void *ptr [[maybe_unused]],
4673 size_t size [[maybe_unused]]) {
4674 TRASH(ptr, size);
4675 }
4676 static void operator delete(
4677 void *, MEM_ROOT *, const std::nothrow_t &) noexcept { /* Never called */
4678 }
4679};
4680
4681extern void lex_free(void);
4682extern bool lex_start(THD *thd);
4683extern void lex_end(LEX *lex);
4684extern int my_sql_parser_lex(MY_SQL_PARSER_STYPE *, POS *, class THD *);
4685
4686extern void trim_whitespace(const CHARSET_INFO *cs, LEX_STRING *str);
4687
4688extern bool is_lex_native_function(const LEX_STRING *name);
4690bool is_keyword(const char *name, size_t len);
4691bool db_is_default_db(const char *db, size_t db_len, const THD *thd);
4692
4694
4695void print_derived_column_names(const THD *thd, String *str,
4697
4698/**
4699 @} (End of group GROUP_PARSER)
4700*/
4701
4702/**
4703 Check if the given string is invalid using the system charset.
4704
4705 @param string_val Reference to the string.
4706 @param charset_info Pointer to charset info.
4707
4708 @return true if the string has an invalid encoding using
4709 the system charset else false.
4710*/
4711
4712inline bool is_invalid_string(const LEX_CSTRING &string_val,
4713 const CHARSET_INFO *charset_info) {
4714 size_t valid_len;
4715 bool len_error;
4716
4717 if (validate_string(charset_info, string_val.str, string_val.length,
4718 &valid_len, &len_error)) {
4719 char hexbuf[7];
4720 octet2hex(
4721 hexbuf, string_val.str + valid_len,
4722 static_cast<uint>(std::min<size_t>(string_val.length - valid_len, 3)));
4723 my_error(ER_INVALID_CHARACTER_STRING, MYF(0), charset_info->csname, hexbuf);
4724 return true;
4725 }
4726 return false;
4727}
4728
4729/**
4730 Check if the given string is invalid using the system charset.
4731
4732 @param string_val Reference to the string.
4733 @param charset_info Pointer to charset info.
4734 @param[out] invalid_sub_str If string has an invalid encoding then invalid
4735 string in printable ASCII format is stored.
4736
4737 @return true if the string has an invalid encoding using
4738 the system charset else false.
4739*/
4740
4741inline bool is_invalid_string(const LEX_CSTRING &string_val,
4743 std::string &invalid_sub_str) {
4744 size_t valid_len;
4745 bool len_error;
4746
4747 if (validate_string(charset_info, string_val.str, string_val.length,
4748 &valid_len, &len_error)) {
4749 char printable_buff[32];
4751 printable_buff, sizeof(printable_buff), string_val.str + valid_len,
4752 static_cast<uint>(std::min<size_t>(string_val.length - valid_len, 3)),
4753 charset_info, 3);
4754 invalid_sub_str = printable_buff;
4755 return true;
4756 }
4757 return false;
4758}
4759
4760/**
4761 In debug mode, verify that we're not adding an item twice to the fields list
4762 with inconsistent hidden flags. Must be called before adding the item to
4763 fields.
4764 */
4766 [[maybe_unused]],
4767 Item *item [[maybe_unused]],
4768 bool hidden [[maybe_unused]]) {
4769#ifndef NDEBUG
4770 if (std::find(fields.begin(), fields.end(), item) != fields.end()) {
4771 // The item is already in the list, so we can't add it
4772 // with a different value for hidden.
4773 assert(item->hidden == hidden);
4774 }
4775#endif
4776}
4777
4778bool walk_item(Item *item, Select_lex_visitor *visitor);
4780bool accept_table(Table_ref *t, Select_lex_visitor *visitor);
4782 Select_lex_visitor *visitor);
4783Table_ref *nest_join(THD *thd, Query_block *select, Table_ref *embedding,
4784 mem_root_deque<Table_ref *> *jlist, size_t table_cnt,
4785 const char *legend);
4786void get_select_options_str(ulonglong options, std::string *str);
4787#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:204
Parser state for CTE subquery parser.
Definition: sql_lex.h:4641
Common_table_expr_parser_state()
Definition: sql_lex.cc:1207
PT_subquery * result
Definition: sql_lex.h:4645
Utility RAII class to save/modify/restore the condition_context information of a query block.
Definition: sql_lex.h:2414
enum_condition_context saved_value
Definition: sql_lex.h:2436
~Condition_context()
Definition: sql_lex.h:2430
Query_block * select
Definition: sql_lex.h:2435
Condition_context(Query_block *select_ptr, enum_condition_context new_type=enum_condition_context::NEITHER)
Definition: sql_lex.h:2416
Parser state for Derived table's condition parser.
Definition: sql_lex.h:4652
Item * result
Definition: sql_lex.h:4656
Derived_expr_parser_state()
Definition: sql_lex.cc:1210
Definition: event_parse_data.h:43
Base class for structured and hierarchical EXPLAIN output formatters.
Definition: opt_explain_format.h:505
Parser state for single expression parser (.frm/DD stuff)
Definition: sql_lex.h:4631
Expression_parser_state()
Definition: sql_lex.cc:1204
Item * result
Definition: sql_lex.h:4635
Definition: field.h:575
Parser state for generated column expression parser (.frm/DD stuff)
Definition: sql_lex.h:4621
Value_generator * result
Definition: sql_lex.h:4625
Gcol_expr_parser_state()
Definition: sql_lex.cc:1201
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:2751
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:2434
Definition: item_subselect.h:418
Definition: item.h:4231
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:1635
A wrapper Item that contains a number of aggregate items, one for each level of rollup (see Item_roll...
Definition: item_sum.h:2714
Base class that is common to all subqueries and subquery predicates.
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:853
cond_result
Definition: item.h:922
@ COND_UNDEF
Definition: item.h:922
Definition: sql_optimizer.h:132
Definition: key_spec.h:66