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