MySQL 8.0.29
Source Code Documentation
sql_lex.h
Go to the documentation of this file.
1/* Copyright (c) 2000, 2022, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23/**
24 @defgroup GROUP_PARSER Parser
25 @{
26*/
27
28#ifndef SQL_LEX_INCLUDED
29#define SQL_LEX_INCLUDED
30
31#include <string.h>
32#include <sys/types.h> // TODO: replace with cstdint
33
34#include <algorithm>
35#include <cstring>
36#include <functional>
37#include <map>
38#include <memory>
39#include <new>
40#include <string>
41#include <utility>
42
43#include "lex_string.h"
44#include "m_ctype.h"
45#include "m_string.h"
46#include "map_helpers.h"
47#include "mem_root_deque.h"
48#include "memory_debugging.h"
49#include "my_alloc.h" // Destroy_only
50#include "my_base.h"
51#include "my_compiler.h"
52#include "my_dbug.h"
53#include "my_inttypes.h" // TODO: replace with cstdint
54#include "my_sqlcommand.h"
55#include "my_sys.h"
56#include "my_table_map.h"
57#include "my_thread_local.h"
59#include "mysql/service_mysql_alloc.h" // my_free
60#include "mysql_com.h"
61#include "mysqld_error.h"
62#include "prealloced_array.h" // Prealloced_array
63#include "sql/dd/info_schema/table_stats.h" // dd::info_schema::Table_stati...
64#include "sql/dd/info_schema/tablespace_stats.h" // dd::info_schema::Tablesp...
65#include "sql/enum_query_type.h"
66#include "sql/handler.h"
67#include "sql/item.h" // Name_resolution_context
68#include "sql/item_subselect.h" // Subquery_strategy
72#include "sql/key_spec.h" // KEY_CREATE_INFO
73#include "sql/mdl.h"
74#include "sql/mem_root_array.h" // Mem_root_array
75#include "sql/parse_tree_node_base.h" // enum_parsing_context
76#include "sql/parser_yystype.h"
77#include "sql/query_options.h" // OPTION_NO_CONST_TABLES
78#include "sql/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_LIST
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
136
137/*
138 There are 8 different type of table access so there is no more than
139 combinations 2^8 = 256:
140
141 . STMT_READS_TRANS_TABLE
142
143 . STMT_READS_NON_TRANS_TABLE
144
145 . STMT_READS_TEMP_TRANS_TABLE
146
147 . STMT_READS_TEMP_NON_TRANS_TABLE
148
149 . STMT_WRITES_TRANS_TABLE
150
151 . STMT_WRITES_NON_TRANS_TABLE
152
153 . STMT_WRITES_TEMP_TRANS_TABLE
154
155 . STMT_WRITES_TEMP_NON_TRANS_TABLE
156
157 The unsafe conditions for each combination is represented within a byte
158 and stores the status of the option --binlog-direct-non-trans-updates,
159 whether the trx-cache is empty or not, and whether the isolation level
160 is lower than ISO_REPEATABLE_READ:
161
162 . option (OFF/ON)
163 . trx-cache (empty/not empty)
164 . isolation (>= ISO_REPEATABLE_READ / < ISO_REPEATABLE_READ)
165
166 bits 0 : . OFF, . empty, . >= ISO_REPEATABLE_READ
167 bits 1 : . OFF, . empty, . < ISO_REPEATABLE_READ
168 bits 2 : . OFF, . not empty, . >= ISO_REPEATABLE_READ
169 bits 3 : . OFF, . not empty, . < ISO_REPEATABLE_READ
170 bits 4 : . ON, . empty, . >= ISO_REPEATABLE_READ
171 bits 5 : . ON, . empty, . < ISO_REPEATABLE_READ
172 bits 6 : . ON, . not empty, . >= ISO_REPEATABLE_READ
173 bits 7 : . ON, . not empty, . < ISO_REPEATABLE_READ
174*/
175extern uint binlog_unsafe_map[256];
176/*
177 Initializes the array with unsafe combinations and its respective
178 conditions.
179*/
181
182/*
183 If we encounter a diagnostics statement (GET DIAGNOSTICS, or e.g.
184 the old SHOW WARNINGS|ERRORS, or "diagnostics variables" such as
185 @@warning_count | @@error_count, we'll set some hints so this
186 information is not lost. DA_KEEP_UNSPECIFIED is used in LEX constructor to
187 avoid leaving variables uninitialized.
188 */
190 DA_KEEP_NOTHING = 0, /**< keep nothing */
191 DA_KEEP_DIAGNOSTICS, /**< keep the diagnostics area */
192 DA_KEEP_COUNTS, /**< keep \@warning_count / \@error_count */
193 DA_KEEP_PARSE_ERROR, /**< keep diagnostics area after parse error */
194 DA_KEEP_UNSPECIFIED /**< keep semantics is unspecified */
196
202
210
211/**
212 enum_sp_type defines type codes of stored programs.
213
214 @note these codes are used when dealing with the mysql.routines system table,
215 so they must not be changed.
216
217 @note the following macros were used previously for the same purpose. Now they
218 are used for ACL only.
219*/
220enum class enum_sp_type {
221 FUNCTION = 1,
222 PROCEDURE,
223 TRIGGER,
224 EVENT,
225 /*
226 Must always be the last one.
227 Denotes an error condition.
228 */
230};
231
233 if (val >= static_cast<longlong>(enum_sp_type::FUNCTION) &&
234 val < static_cast<longlong>(enum_sp_type::INVALID_SP_TYPE))
235 return static_cast<enum_sp_type>(val);
236 else
238}
239
241 return static_cast<longlong>(val);
242}
243
244inline uint to_uint(enum_sp_type val) { return static_cast<uint>(val); }
245
246/*
247 Values for the type enum. This reflects the order of the enum declaration
248 in the CREATE TABLE command. These values are used to enumerate object types
249 for the ACL statements.
250
251 These values were also used for enumerating stored program types. However, now
252 enum_sp_type should be used for that instead of them.
253*/
254#define TYPE_ENUM_FUNCTION 1
255#define TYPE_ENUM_PROCEDURE 2
256#define TYPE_ENUM_TRIGGER 3
257#define TYPE_ENUM_PROXY 4
258
259enum class Acl_type {
260 TABLE = 0,
263};
264
266 {STRING_WITH_LEN("")},
267 {STRING_WITH_LEN("CONTAINS SQL")},
268 {STRING_WITH_LEN("NO SQL")},
269 {STRING_WITH_LEN("READS SQL DATA")},
270 {STRING_WITH_LEN("MODIFIES SQL DATA")}};
271
273 VIEW_CREATE_NEW, // check that there are not such VIEW/table
274 VIEW_ALTER, // check that VIEW with such name exists
275 VIEW_CREATE_OR_REPLACE // check only that there are not such table
276};
277
279 ALTER_USER_COMMENT_NOT_USED, // No user metadata ALTER in the AST
280 ALTER_USER_COMMENT, // A text comment is expected
281 ALTER_USER_ATTRIBUTE // A JSON object is expected
282};
283
284/* Options to add_table_to_list() */
285#define TL_OPTION_UPDATING 0x01
286#define TL_OPTION_IGNORE_LEAVES 0x02
287#define TL_OPTION_ALIAS 0x04
288
289/* Structure for db & table in sql_yacc */
290class Table_function;
291
293 public:
298
299 Table_ident(Protocol *protocol, const LEX_CSTRING &db_arg,
300 const LEX_CSTRING &table_arg, bool force);
301 Table_ident(const LEX_CSTRING &db_arg, const LEX_CSTRING &table_arg)
302 : db(db_arg), table(table_arg), sel(nullptr), table_function(nullptr) {}
303 Table_ident(const LEX_CSTRING &table_arg)
304 : table(table_arg), sel(nullptr), table_function(nullptr) {
305 db = NULL_CSTR;
306 }
307 /**
308 This constructor is used only for the case when we create a derived
309 table. A derived table has no name and doesn't belong to any database.
310 Later, if there was an alias specified for the table, it will be set
311 by add_table_to_list.
312 */
314 db = EMPTY_CSTR; /* a subject to casedn_str */
316 }
317 /*
318 This constructor is used only for the case when we create a table function.
319 It has no name and doesn't belong to any database as it exists only
320 during query execution. Later, if there was an alias specified for the
321 table, it will be set by add_table_to_list.
322 */
323 Table_ident(LEX_CSTRING &table_arg, Table_function *table_func_arg)
324 : table(table_arg), sel(nullptr), table_function(table_func_arg) {
325 /* We must have a table name here as this is used with add_table_to_list */
326 db = EMPTY_CSTR; /* a subject to casedn_str */
327 }
328 // True if we can tell from syntax that this is a table function.
329 bool is_table_function() const { return (table_function != nullptr); }
330 // True if we can tell from syntax that this is an unnamed derived table.
331 bool is_derived_table() const { return sel; }
332 void change_db(const char *db_name) {
333 db.str = db_name;
334 db.length = strlen(db_name);
335 }
336};
337
340
341/**
342 Structure to hold parameters for CHANGE MASTER, START SLAVE, and STOP SLAVE.
343
344 Remark: this should not be confused with Master_info (and perhaps
345 would better be renamed to st_lex_replication_info). Some fields,
346 e.g., delay, are saved in Relay_log_info, not in Master_info.
347*/
349 /*
350 The array of IGNORE_SERVER_IDS has a preallocation, and is not expected
351 to grow to any significant size, so no instrumentation.
352 */
354 initialize();
355 }
362 char *gtid;
363 char *view_id;
364 const char *channel; // identifier similar to database name
365 enum {
372
373 /*
374 Enum is used for making it possible to detect if the user
375 changed variable or if it should be left at old value
376 */
377 enum {
387 /*
388 Ciphersuites used for TLS 1.3 communication with the master server.
389 */
394 };
403 /**
404 Flag that is set to `true` whenever `PRIVILEGE_CHECKS_USER` is set to `NULL`
405 as a part of a `CHANGE MASTER TO` statement.
406 */
408 /**
409 Username and hostname parts of the `PRIVILEGE_CHECKS_USER`, when it's set to
410 a user.
411 */
413 /**
414 Flag indicating if row format should be enforced for this channel event
415 stream.
416 */
418
419 /**
420 Identifies what is the slave policy on primary keys in tables.
421 If set to STREAM it just replicates the value of sql_require_primary_key.
422 If set to ON it fails when the source tries to replicate a table creation
423 or alter operation that does not have a primary key.
424 If set to OFF it does not enforce any policies on the channel for primary
425 keys.
426 */
427 enum {
433
434 enum {
440
442
443 /// Initializes everything to zero/NULL/empty.
444 void initialize();
445 /// Sets all fields to their "unspecified" value.
446 void set_unspecified();
447
448 private:
449 // Not copyable or assignable.
452};
453
455 bool all;
456};
457
467
468/*
469 String names used to print a statement with index hints.
470 Keep in sync with index_hint_type.
471*/
472extern const char *index_hint_type_name[];
474
475/*
476 Bits in index_clause_map : one for each possible FOR clause in
477 USE/FORCE/IGNORE INDEX index hint specification
478*/
479#define INDEX_HINT_MASK_JOIN (1)
480#define INDEX_HINT_MASK_GROUP (1 << 1)
481#define INDEX_HINT_MASK_ORDER (1 << 2)
482
483#define INDEX_HINT_MASK_ALL \
484 (INDEX_HINT_MASK_JOIN | INDEX_HINT_MASK_GROUP | INDEX_HINT_MASK_ORDER)
485
486/* Single element of an USE/FORCE/IGNORE INDEX list specified as a SQL hint */
488 public:
489 /* The type of the hint : USE/FORCE/IGNORE */
491 /* Where the hit applies to. A bitmask of INDEX_HINT_MASK_<place> values */
493 /*
494 The index name. Empty (str=NULL) name represents an empty list
495 USE INDEX () clause
496 */
498
499 Index_hint(const char *str, uint length) {
500 key_name.str = str;
502 }
503
504 void print(const THD *thd, String *str);
505};
506
507/*
508 Class Query_expression represents a query expression.
509 Class Query_block represents a query block.
510 A query expression contains one or more query blocks (more than one means
511 that we have a UNION query).
512 These classes are connected as follows:
513 Both classes have a master, a slave, a next and a prev field.
514 For class Query_block, master and slave connect to objects of type
515 Query_expression, whereas for class Query_expression, they connect
516 to Query_block.
517 master is pointer to outer node.
518 slave is pointer to the first inner node
519
520 neighbors are two Query_block or Query_expression objects on
521 the same level.
522
523 The structures are linked with the following pointers:
524 - list of neighbors (next/prev) (prev of first element point to slave
525 pointer of outer structure)
526 - For Query_block, this is a list of query blocks.
527 - For Query_expression, this is a list of subqueries.
528
529 - pointer to outer node (master), which is
530 If this is Query_expression
531 - pointer to outer query_block.
532 If this is Query_block
533 - pointer to outer Query_expression.
534
535 - pointer to inner objects (slave), which is either:
536 If this is an Query_expression:
537 - first query block that belong to this query expression.
538 If this is an Query_block
539 - first query expression that belong to this query block (subqueries).
540
541 - list of all Query_block objects (link_next/link_prev)
542 This is to be used for things like derived tables creation, where we
543 go through this list and create the derived tables.
544
545 If query expression contain several query blocks (UNION now,
546 INTERSECT etc later) then it has a special query_block called
547 fake_query_block. It used for storing global parameters (like ORDER BY,
548 LIMIT) and executing union.
549 Subqueries used in global ORDER BY clause will be attached to this
550 fake_query_block, which will allow them to correctly resolve fields of
551 the containing UNION and outer selects.
552
553 For example for following query:
554
555 select *
556 from table1
557 where table1.field IN (select * from table1_1_1 union
558 select * from table1_1_2)
559 union
560 select *
561 from table2
562 where table2.field=(select (select f1 from table2_1_1_1_1
563 where table2_1_1_1_1.f2=table2_1_1.f3)
564 from table2_1_1
565 where table2_1_1.f1=table2.f2)
566 union
567 select * from table3;
568
569 we will have following structure:
570
571 select1: (select * from table1 ...)
572 select2: (select * from table2 ...)
573 select3: (select * from table3)
574 select1.1.1: (select * from table1_1_1)
575 ...
576
577 main unit
578 fake0
579 select1 select2 select3
580 |^^ |^
581 s||| ||master
582 l||| |+---------------------------------+
583 a||| +---------------------------------+|
584 v|||master slave ||
585 e||+-------------------------+ ||
586 V| neighbor | V|
587 unit1.1<+==================>unit1.2 unit2.1
588 fake1.1
589 select1.1.1 select 1.1.2 select1.2.1 select2.1.1
590 |^
591 ||
592 V|
593 unit2.1.1.1
594 select2.1.1.1.1
595
596
597 relation in main unit will be following:
598 (bigger picture for:
599 main unit
600 fake0
601 select1 select2 select3
602 in the above picture)
603
604 main unit
605 |^^^^|fake_query_block
606 |||||+--------------------------------------------+
607 ||||+--------------------------------------------+|
608 |||+------------------------------+ ||
609 ||+--------------+ | ||
610 slave||master | | ||
611 V| neighbor | neighbor | master|V
612 select1<========>select2<========>select3 fake0
613
614 list of all query_block will be following (as it will be constructed by
615 parser):
616
617 select1->select2->select3->select2.1.1->select 2.1.2->select2.1.1.1.1-+
618 |
619 +---------------------------------------------------------------------+
620 |
621 +->select1.1.1->select1.1.2
622
623*/
624
625/**
626 This class represents a query expression (one query block or
627 several query blocks combined with UNION).
628*/
630 /**
631 Intrusive double-linked list of all query expressions
632 immediately contained within the same query block.
633 */
636
637 /**
638 The query block wherein this query expression is contained,
639 NULL if the query block is the outer-most one.
640 */
642 /// The first query block in this query expression.
644
645 private:
646 /**
647 Marker for subqueries in WHERE, HAVING, ORDER BY, GROUP BY and
648 SELECT item lists.
649 Must be read/written when holding LOCK_query_plan.
650
651 See Item_subselect::explain_subquery_checker
652 */
654
655 bool prepared; ///< All query blocks in query expression are prepared
656 bool optimized; ///< All query blocks in query expression are optimized
657 bool executed; ///< Query expression has been executed
658
661 /// Temporary table using for appending UNION results.
662 /// Not used if we materialize directly into a parent query expression's
663 /// result table (see optimize()).
665 /// Object to which the result for this query expression is sent.
666 /// Not used if we materialize directly into a parent query expression's
667 /// result table (see optimize()).
669
670 /**
671 An iterator you can read from to get all records for this query.
672
673 May be nullptr even after create_access_paths(), or in the case of an
674 unfinished materialization (see optimize()).
675 */
678
679 /**
680 If there is an unfinished materialization (see optimize()),
681 contains one element for each query block in this query expression.
682 */
685
686 /**
687 Sets up each query block in this query expression for materialization
688 into the given table.
689
690 @param thd thread handle
691 @param dst_table the table to materialize into
692 @param union_distinct_only if true, keep only UNION DISTINCT query blocks
693 (any UNION ALL blocks are presumed handled higher up, by AppendIterator)
694 */
696 THD *thd, TABLE *dst_table, bool union_distinct_only);
697
698 /**
699 Convert the executor structures to a set of access paths, storing the result
700 in m_root_access_path.
701 */
702 void create_access_paths(THD *thd);
703
704 public:
705 /**
706 result of this query can't be cached, bit field, can be :
707 UNCACHEABLE_DEPENDENT
708 UNCACHEABLE_RAND
709 UNCACHEABLE_SIDEEFFECT
710 */
712
713 explicit Query_expression(enum_parsing_context parsing_context);
714
715 /// @return true for a query expression without UNION or multi-level ORDER
716 bool is_simple() const { return !(is_union() || fake_query_block); }
717
718 /// Values for Query_expression::cleaned
720 UC_DIRTY, ///< Unit isn't cleaned
721 UC_PART_CLEAN, ///< Unit were cleaned, except JOIN and JOIN_TABs were
722 ///< kept for possible EXPLAIN
723 UC_CLEAN ///< Unit completely cleaned, all underlying JOINs were
724 ///< freed
725 };
726 enum_clean_state cleaned; ///< cleanliness state
727
728 // list of (visible) fields which points to temporary table for union
730
731 private:
732 /*
733 list of types of items inside union (used for union & derived tables)
734
735 Item_type_holders from which this list consist may have pointers to Field,
736 pointers is valid only after preparing SELECTS of this unit and before
737 any SELECT of this unit execution
738
739 All hidden items are stripped away from this list.
740 */
742
743 public:
744 /**
745 Pointer to query block containing global parameters for query.
746 Global parameters may include ORDER BY, LIMIT and OFFSET.
747
748 If this is a union of multiple query blocks, the global parameters are
749 stored in fake_query_block. If the union doesn't use a temporary table,
750 Query_expression::prepare() nulls out fake_query_block, but saves a copy
751 in saved_fake_query_block in order to preserve the global parameters.
752
753 If this is not a union, and the query expression has no multi-level
754 ORDER BY/LIMIT, global parameters are in the single query block.
755
756 @return query block containing the global parameters
757 */
759 if (fake_query_block != nullptr)
760 return fake_query_block;
761 else if (saved_fake_query_block != nullptr)
763 return first_query_block();
764 }
765 /* LIMIT clause runtime counters */
767 /// Points to subquery if this query expression is used in one, otherwise NULL
769 /**
770 Helper query block for query expression with UNION or multi-level
771 ORDER BY/LIMIT
772 */
774 /**
775 Query_block that stores LIMIT and OFFSET for UNION ALL when no
776 fake_query_block is used.
777 */
779 /**
780 Points to last query block which has UNION DISTINCT on its left.
781 In a list of UNIONed blocks, UNION is left-associative; so UNION DISTINCT
782 eliminates duplicates in all blocks up to the first one on its right
783 included. Which is why we only need to remember that query block.
784 */
786
787 /**
788 The WITH clause which is the first part of this query expression. NULL if
789 none.
790 */
792 /**
793 If this query expression is underlying of a derived table, the derived
794 table. NULL if none.
795 */
797 /**
798 First query block (in this UNION) which references the CTE.
799 NULL if not the query expression of a recursive CTE.
800 */
802
803 /**
804 If 'this' is body of lateral derived table:
805 map of tables in the same FROM clause as this derived table, and to which
806 the derived table's body makes references.
807 In pre-resolution stages, this is OUTER_REF_TABLE_BIT, just to indicate
808 that this has LATERAL; after resolution, which has found references in the
809 body, this is the proper map (with no PSEUDO_TABLE_BITS anymore).
810 */
812 /**
813 True if the with-recursive algorithm has produced the complete result.
814 In a recursive CTE, a JOIN is executed several times in a loop, and
815 should not be cleaned up (e.g. by join_free()) before all iterations of
816 the loop are done (i.e. before the CTE's result is complete).
817 */
819
821
822 /**
823 This query expression represents a scalar subquery and we need a run-time
824 check that the cardinality doesn't exceed 1.
825 */
827
828 /// @return true if query expression can be merged into an outer query
829 bool is_mergeable() const;
830
831 /// @return true if query expression is recommended to be merged
832 bool merge_heuristic(const LEX *lex) const;
833
834 /// @return the query block this query expression belongs to as subquery
836
837 /// @return the first query block inside this query expression
839
840 /// @return the next query expression within same query block (next subquery)
842
843 /// @return the query result object in use for this query expression
845
846 RowIterator *root_iterator() const { return m_root_iterator.get(); }
848 return move(m_root_iterator);
849 }
851
852 // Asks each query block to switch to an access path with in2exists
853 // conditions removed (if they were ever added).
854 // See JOIN::change_to_access_path_without_in2exists().
856
858 m_root_access_path = nullptr;
859 m_root_iterator.reset();
860 }
861
862 /**
863 Ensures that there are iterators created for the access paths created
864 by optimize(), even if it was called with create_access_paths = false.
865 If there are already iterators, it is a no-op. optimize() must have
866 been called earlier.
867
868 The use case for this is if we have a query block that's not top-level,
869 but we figure out after the fact that we wanted to run it anyway.
870 The typical case would be that we notice that the query block can return
871 at most one row (a so-called const table), and want to run it during
872 optimization.
873 */
874 bool force_create_iterators(THD *thd);
875
876 /// See optimize().
878 return !m_query_blocks_to_materialize.empty();
879 }
880
881 /// See optimize().
884 return std::move(m_query_blocks_to_materialize);
885 }
886
887 /// Set new query result object for this query expression
889
890 /**
891 Whether there is a chance that optimize() is capable of materializing
892 directly into a result table if given one. Note that even if this function
893 returns true, optimize() can choose later not to do so, since it depends
894 on information (in particular, whether the query blocks can run under
895 the iterator executor or not) that is not available before optimize time.
896
897 TODO(sgunders): Now that all query blocks can run under the iterator
898 executor, the above may no longer be true. This needs investigation.
899 */
901
902 bool prepare(THD *thd, Query_result *result,
903 mem_root_deque<Item *> *insert_field_list,
904 ulonglong added_options, ulonglong removed_options);
905
906 /**
907 If and only if materialize_destination is non-nullptr, it means that the
908 caller intends to materialize our result into the given table. If it is
909 advantageous (in particular, if this query expression is a UNION DISTINCT),
910 optimize() will not create an iterator by itself, but rather do an
911 unfinished materialize. This means that it will collect iterators for
912 all the query blocks and prepare them for materializing into the given
913 table, but not actually create a root iterator for this query expression;
914 the caller is responsible for calling release_query_blocks_to_materialize()
915 and creating the iterator itself.
916
917 Even if materialize_destination is non-nullptr, this function may choose
918 to make a regular iterator. The caller is responsible for checking
919 unfinished_materialization() if it has given a non-nullptr table.
920
921 @param thd Thread handle.
922
923 @param materialize_destination What table to try to materialize into,
924 or nullptr if the caller does not intend to materialize the result.
925
926 @param create_iterators If false, only access paths are created,
927 not iterators. Only top level query blocks (these that we are to call
928 exec() on) should have iterators. See also force_create_iterators().
929
930 @param finalize_access_paths Relevant for the hypergraph optimizer only.
931 If false, the given access paths will _not_ be finalized, so you cannot
932 create iterators from it before finalize() is called (see
933 FinalizePlanForQueryBlock()), and create_iterators must also be false.
934 This is relevant only if you are potentially optimizing multiple times
935 (see change_to_access_path_without_in2exists()), since you are only
936 allowed to finalize a query block once. The fake_query_block, if any,
937 is always finalized.
938 */
939 bool optimize(THD *thd, TABLE *materialize_destination, bool create_iterators,
940 bool finalize_access_paths);
941
942 /**
943 For any non-finalized query block, finalize it so that we are allowed to
944 create iterators. Must be called after the final access path is chosen
945 (ie., after any calls to change_to_access_path_without_in2exists()).
946 */
947 bool finalize(THD *thd);
948
949 /**
950 Do everything that would be needed before running Init() on the root
951 iterator. In particular, clear out data from previous execution iterations,
952 if needed.
953 */
954 bool ClearForExecution(THD *thd);
955
956 bool ExecuteIteratorQuery(THD *thd);
957 bool execute(THD *thd);
958 bool explain(THD *explain_thd, const THD *query_thd);
959 void cleanup(THD *thd, bool full);
960 /**
961 Destroy contained objects, in particular temporary tables which may
962 have their own mem_roots.
963 */
964 void destroy();
965
966 void print(const THD *thd, String *str, enum_query_type query_type);
967 bool accept(Select_lex_visitor *visitor);
968
969 bool add_fake_query_block(THD *thd);
970 bool prepare_fake_query_block(THD *thd);
972 assert(!is_prepared());
973 prepared = true;
974 }
976 assert(is_prepared() && !is_optimized());
977 optimized = true;
978 }
980 // assert(is_prepared() && is_optimized() && !is_executed());
981 assert(is_prepared() && is_optimized());
982 executed = true;
983 }
984 /// Reset this query expression for repeated evaluation within same execution
986 assert(is_prepared() && is_optimized());
987 executed = false;
988 }
989 /// Clear execution state, needed before new execution of prepared statement
991 // Cannot be enforced when called from Prepared_statement::execute():
992 // assert(is_prepared());
993 optimized = false;
994 executed = false;
996 }
997 /// Check state of preparation of the contained query expression.
998 bool is_prepared() const { return prepared; }
999 /// Check state of optimization of the contained query expression.
1000 bool is_optimized() const { return optimized; }
1001 /**
1002 Check state of execution of the contained query expression.
1003 Should not be used to check the state of a complete statement, use
1004 LEX::is_exec_completed() instead.
1005 */
1006 bool is_executed() const { return executed; }
1008 Query_result_interceptor *old_result);
1009 bool set_limit(THD *thd, Query_block *provider);
1010 bool has_any_limit() const;
1011
1012 inline bool is_union() const;
1013 bool union_needs_tmp_table(LEX *lex);
1014 /// @returns true if mixes UNION DISTINCT and UNION ALL
1015 bool mixed_union_operators() const;
1016
1017 /// Include a query expression below a query block.
1018 void include_down(LEX *lex, Query_block *outer);
1019
1020 /// Exclude this unit and immediately contained query_block objects
1021 void exclude_level();
1022
1023 /// Exclude subtree of current unit from tree of SELECTs
1024 void exclude_tree(THD *thd);
1025
1026 /// Renumber query blocks of a query expression according to supplied LEX
1027 void renumber_selects(LEX *lex);
1028
1030 bool save_cmd_properties(THD *thd);
1031
1032 friend class Query_block;
1033
1036 size_t num_visible_fields() const;
1037
1038 // If we are doing a query with global LIMIT but without fake_query_block,
1039 // we need somewhere to store the record count for FOUND_ROWS().
1040 // It can't be in any of the JOINs, since they may have their own
1041 // LimitOffsetIterators, which will write to join->send_records
1042 // whenever there is an OFFSET. (It also can't be in saved_fake_query_block,
1043 // which has no join.) Thus, we'll keep it here instead.
1044 //
1045 // If we have a fake_query_block, we use its send_records instead
1046 // (since its LimitOffsetIterator will write there), and if we don't
1047 // have a UNION, FOUND_ROWS() refers to the (single) JOIN, and thus,
1048 // we use its send_records.
1050
1053 void set_explain_marker_from(THD *thd, const Query_expression *u);
1054
1055#ifndef NDEBUG
1056 /**
1057 Asserts that none of {this unit and its children units} is fully cleaned
1058 up.
1059 */
1061#else
1062 void assert_not_fully_clean() {}
1063#endif
1064 void invalidate();
1065
1066 bool is_recursive() const { return first_recursive != nullptr; }
1067
1069
1071
1072 void fix_after_pullout(Query_block *parent_query_block,
1073 Query_block *removed_query_block);
1074
1075 /**
1076 If unit is a subquery, which forms an object of the upper level (an
1077 Item_subselect, a derived TABLE_LIST), adds to this object a map
1078 of tables of the upper level which the unit references.
1079 */
1081
1082 /**
1083 If unit is a subquery, which forms an object of the upper level (an
1084 Item_subselect, a derived TABLE_LIST), returns the place of this object
1085 in the upper level query block.
1086 */
1088
1089 bool walk(Item_processor processor, enum_walk walk, uchar *arg);
1090
1091 /*
1092 An exception: this is the only function that needs to adjust
1093 explain_marker.
1094 */
1095 friend bool parse_view_definition(THD *thd, TABLE_LIST *view_ref);
1096};
1097
1100
1101/**
1102 Query_block type enum
1103*/
1105 EXPLAIN_NONE = 0,
1113 // Total:
1114 EXPLAIN_total ///< fake type, total number of all valid types
1115
1116 // Don't insert new types below this line!
1117};
1118
1119/**
1120 This class represents a query block, aka a query specification, which is
1121 a query consisting of a SELECT keyword, followed by a table list,
1122 optionally followed by a WHERE clause, a GROUP BY, etc.
1123*/
1125 public:
1126 /**
1127 @note the group_by and order_by lists below will probably be added to the
1128 constructor when the parser is converted into a true bottom-up design.
1129
1130 //SQL_I_LIST<ORDER> *group_by, SQL_I_LIST<ORDER> order_by
1131 */
1133
1134 Item *where_cond() const { return m_where_cond; }
1136 void set_where_cond(Item *cond) { m_where_cond = cond; }
1137 Item *having_cond() const { return m_having_cond; }
1139 void set_having_cond(Item *cond) { m_having_cond = cond; }
1142 bool change_query_result(THD *thd, Query_result_interceptor *new_result,
1143 Query_result_interceptor *old_result);
1144
1145 /// Set base options for a query block (and active options too)
1146 void set_base_options(ulonglong options_arg) {
1147 DBUG_EXECUTE_IF("no_const_tables", options_arg |= OPTION_NO_CONST_TABLES;);
1148
1149 // Make sure we do not overwrite options by accident
1150 assert(m_base_options == 0 && m_active_options == 0);
1151 m_base_options = options_arg;
1152 m_active_options = options_arg;
1153 }
1154
1155 /// Add base options to a query block, also update active options
1157 assert(first_execution);
1160 }
1161
1162 /**
1163 Remove base options from a query block.
1164 Active options are also updated, and we assume here that "extra" options
1165 cannot override removed base options.
1166 */
1168 assert(first_execution);
1171 }
1172
1173 /// Make active options from base options, supplied options and environment:
1174 void make_active_options(ulonglong added_options, ulonglong removed_options);
1175
1176 /// Adjust the active option set
1178
1179 /// @return the active query options
1181
1182 /**
1183 Set associated tables as read_only, ie. they cannot be inserted into,
1184 updated or deleted from during this statement.
1185 Commonly used for query blocks that are part of derived tables or
1186 views that are materialized.
1187 */
1189 // Set all referenced base tables as read only.
1190 for (TABLE_LIST *tr = leaf_tables; tr != nullptr; tr = tr->next_leaf)
1191 tr->set_readonly();
1192 }
1193
1194 /// @returns a map of all tables references in the query block
1195 table_map all_tables_map() const { return (1ULL << leaf_table_count) - 1; }
1196
1197 void remove_derived(THD *thd, TABLE_LIST *tl);
1198 bool remove_aggregates(THD *thd, Query_block *select);
1199
1203 Query_block *next_query_block() const { return next; }
1204
1206
1207 /**
1208 @return true If STRAIGHT_JOIN applies to all tables.
1209 @return false Else.
1210 */
1212 bool straight_join = true;
1213 /// false for exmaple in t1 STRAIGHT_JOIN t2 JOIN t3.
1214 for (TABLE_LIST *tbl = leaf_tables->next_leaf; tbl; tbl = tbl->next_leaf)
1215 straight_join &= tbl->straight;
1216 return straight_join || (active_options() & SELECT_STRAIGHT_JOIN);
1217 }
1218
1220 Query_block *mylast = this;
1221 for (; mylast->next_query_block(); mylast = mylast->next_query_block()) {
1222 }
1223 return mylast;
1224 }
1225
1227
1228 void mark_as_dependent(Query_block *last, bool aggregate);
1229
1230 /// @return true if query block is explicitly grouped (non-empty GROUP BY)
1231 bool is_explicitly_grouped() const { return group_list.elements > 0; }
1232
1233 /**
1234 @return true if this query block is implicitly grouped, ie it is not
1235 explicitly grouped but contains references to set functions.
1236 The query will return max. 1 row (@see also is_single_grouped()).
1237 */
1239 return m_agg_func_used && group_list.elements == 0;
1240 }
1241
1242 /**
1243 True if this query block is implicitly grouped.
1244
1245 @note Not reliable before name resolution.
1246
1247 @return true if this query block is implicitly grouped and returns exactly
1248 one row, which happens when it does not have a HAVING clause.
1249
1250 @remark This function is currently unused.
1251 */
1252 bool is_single_grouped() const {
1253 return m_agg_func_used && group_list.elements == 0 &&
1254 m_having_cond == nullptr;
1255 }
1256
1257 /**
1258 @return true if this query block is explicitly or implicitly grouped.
1259 @note a query with DISTINCT is not considered to be aggregated.
1260 @note in standard SQL, a query with HAVING is defined as grouped, however
1261 MySQL allows HAVING without any aggregation to be the same as WHERE.
1262 */
1263 bool is_grouped() const { return group_list.elements > 0 || m_agg_func_used; }
1264
1265 /// @return true if this query block contains DISTINCT at start of select list
1266 bool is_distinct() const { return active_options() & SELECT_DISTINCT; }
1267
1268 /**
1269 @return true if this query block contains an ORDER BY clause.
1270
1271 @note returns false if ORDER BY has been eliminated, e.g if the query
1272 can return max. 1 row.
1273 */
1274 bool is_ordered() const { return order_list.elements > 0; }
1275
1276 /**
1277 Based on the structure of the query at resolution time, it is possible to
1278 conclude that DISTINCT is useless and remove it.
1279 This is the case if:
1280 - all GROUP BY expressions are in SELECT list, so resulting group rows are
1281 distinct,
1282 - and ROLLUP is not specified, so it adds no row for NULLs.
1283
1284 @returns true if we can remove DISTINCT.
1285
1286 @todo could refine this to if ROLLUP were specified and all GROUP
1287 expressions were non-nullable, because ROLLUP then adds only NULL values.
1288 Currently, ROLLUP+DISTINCT is rejected because executor cannot handle
1289 it in all cases.
1290 */
1291 bool can_skip_distinct() const {
1292 return is_grouped() && hidden_group_field_count == 0 &&
1294 }
1295
1296 /// @return true if this query block has a LIMIT clause
1297 bool has_limit() const { return select_limit != nullptr; }
1298
1299 /// @return true if query block references full-text functions
1300 bool has_ft_funcs() const { return ftfunc_list->elements > 0; }
1301
1302 /// @returns true if query block is a recursive member of a recursive unit
1303 bool is_recursive() const { return recursive_reference != nullptr; }
1304
1305 bool is_in_select_list(Item *i);
1306
1307 /**
1308 Finds a group expression matching the given item, or nullptr if
1309 none. When there are multiple candidates, ones that match in name are
1310 given priority (such that “a AS c GROUP BY a,b,c” resolves to c, not a);
1311 if there is still a tie, the leftmost is given priority.
1312
1313 @param item The item to search for.
1314 @param [out] rollup_level If not nullptr, will be set to the group
1315 expression's index (0-based).
1316 */
1317 ORDER *find_in_group_list(Item *item, int *rollup_level) const;
1318 int group_list_size() const;
1319
1320 /// @returns true if query block contains window functions
1321 bool has_windows() const { return m_windows.elements > 0; }
1322
1323 void invalidate();
1324
1326
1327 bool add_item_to_list(Item *item);
1329 void add_order_to_list(ORDER *order);
1330 TABLE_LIST *add_table_to_list(THD *thd, Table_ident *table, const char *alias,
1331 ulong table_options,
1333 enum_mdl_type mdl_type = MDL_SHARED_READ,
1334 List<Index_hint> *hints = nullptr,
1335 List<String> *partition_names = nullptr,
1336 LEX_STRING *option = nullptr,
1337 Parse_context *pc = nullptr);
1338
1339 /**
1340 Add item to the hidden part of select list
1341
1342 @param item item to add
1343
1344 @return Pointer to reference of the added item
1345 */
1346 Item **add_hidden_item(Item *item);
1347
1348 /// Remove hidden items from select list
1349 void remove_hidden_items();
1350
1351 TABLE_LIST *get_table_list() const { return table_list.first; }
1352 bool init_nested_join(THD *thd);
1354 TABLE_LIST *nest_last_join(THD *thd, size_t table_cnt = 2);
1355 bool add_joined_table(TABLE_LIST *table);
1357
1358 /// Wrappers over fields / get_fields_list() that hide items where
1359 /// item->hidden, meant for range-based for loops. See sql/visible_fields.h.
1361 auto visible_fields() const { return VisibleFields(fields); }
1362
1363 /// Check privileges for views that are merged into query block
1364 bool check_view_privileges(THD *thd, ulong want_privilege_first,
1365 ulong want_privilege_next);
1366 /// Check privileges for all columns referenced from query block
1367 bool check_column_privileges(THD *thd);
1368
1369 /// Check privileges for column references in subqueries of a query block
1371
1372 /// Resolve and prepare information about tables for one query block
1373 bool setup_tables(THD *thd, TABLE_LIST *tables, bool select_insert);
1374
1375 /// Resolve OFFSET and LIMIT clauses
1376 bool resolve_limits(THD *thd);
1377
1378 /// Resolve derived table, view, table function information for a query block
1379 bool resolve_placeholder_tables(THD *thd, bool apply_semijoin);
1380
1381 /// Propagate exclusion from table uniqueness test into subqueries
1383
1384 /// Merge name resolution context objects of a subquery into its parent
1385 void merge_contexts(Query_block *inner);
1386
1387 /// Merge derived table into query block
1388 bool merge_derived(THD *thd, TABLE_LIST *derived_table);
1389
1390 bool flatten_subqueries(THD *thd);
1391
1392 /**
1393 Update available semijoin strategies for semijoin nests.
1394
1395 Available semijoin strategies needs to be updated on every execution since
1396 optimizer_switch setting may have changed.
1397
1398 @param thd Pointer to THD object for session.
1399 Used to access optimizer_switch
1400 */
1402
1403 /**
1404 Returns which subquery execution strategies can be used for this query
1405 block.
1406
1407 @param thd Pointer to THD object for session.
1408 Used to access optimizer_switch
1409
1410 @retval SUBQ_MATERIALIZATION Subquery Materialization should be used
1411 @retval SUBQ_EXISTS In-to-exists execution should be used
1412 @retval CANDIDATE_FOR_IN2EXISTS_OR_MAT A cost-based decision should be made
1413 */
1414 Subquery_strategy subquery_strategy(const THD *thd) const;
1415
1416 /**
1417 Returns whether semi-join is enabled for this query block
1418
1419 @see @c Opt_hints_qb::semijoin_enabled for details on how hints
1420 affect this decision. If there are no hints for this query block,
1421 optimizer_switch setting determines whether semi-join is used.
1422
1423 @param thd Pointer to THD object for session.
1424 Used to access optimizer_switch
1425
1426 @return true if semijoin is enabled,
1427 false otherwise
1428 */
1429 bool semijoin_enabled(const THD *thd) const;
1430
1432 sj_candidates = sj_cand;
1433 }
1434
1435 bool has_sj_candidates() const {
1436 return sj_candidates != nullptr && !sj_candidates->empty();
1437 }
1438
1439 /// Add full-text function elements from a list into this query block
1441
1442 void set_lock_for_table(const Lock_descriptor &descriptor, TABLE_LIST *table);
1443
1444 void set_lock_for_tables(thr_lock_type lock_type);
1445
1446 inline void init_order() {
1447 assert(order_list.elements == 0);
1448 order_list.elements = 0;
1449 order_list.first = nullptr;
1450 order_list.next = &order_list.first;
1451 }
1452 /*
1453 This method created for reiniting LEX in mysql_admin_table() and can be
1454 used only if you are going remove all Query_block & units except belonger
1455 to LEX (LEX::unit & LEX::select, for other purposes use
1456 Query_expression::exclude_level()
1457 */
1458 void cut_subtree() { slave = nullptr; }
1459 bool test_limit();
1460 /**
1461 Get offset for LIMIT.
1462
1463 Evaluate offset item if necessary.
1464
1465 @return Number of rows to skip.
1466
1467 @todo Integrate better with Query_expression::set_limit()
1468 */
1469 ha_rows get_offset(THD *thd);
1470 /**
1471 Get limit.
1472
1473 Evaluate limit item if necessary.
1474
1475 @return Limit of rows in result.
1476
1477 @todo Integrate better with Query_expression::set_limit()
1478 */
1479 ha_rows get_limit(THD *thd);
1480
1481 /// Assign a default name resolution object for this query block.
1482 bool set_context(Name_resolution_context *outer_context);
1483
1484 /// Setup the array containing references to base items
1485 bool setup_base_ref_items(THD *thd);
1486 void print(const THD *thd, String *str, enum_query_type query_type);
1487
1488 /**
1489 Print detail of the Query_block object.
1490
1491 @param thd Thread handler
1492 @param query_type Options to print out string output
1493 @param[out] str String of output.
1494 */
1495 void print_query_block(const THD *thd, String *str,
1496 enum_query_type query_type);
1497
1498 /**
1499 Print detail of the UPDATE statement.
1500
1501 @param thd Thread handler
1502 @param[out] str String of output
1503 @param query_type Options to print out string output
1504 */
1505 void print_update(const THD *thd, String *str, enum_query_type query_type);
1506
1507 /**
1508 Print detail of the DELETE statement.
1509
1510 @param thd Thread handler
1511 @param[out] str String of output
1512 @param query_type Options to print out string output
1513 */
1514 void print_delete(const THD *thd, String *str, enum_query_type query_type);
1515
1516 /**
1517 Print detail of the INSERT statement.
1518
1519 @param thd Thread handler
1520 @param[out] str String of output
1521 @param query_type Options to print out string output
1522 */
1523 void print_insert(const THD *thd, String *str, enum_query_type query_type);
1524
1525 /**
1526 Print detail of Hints.
1527
1528 @param thd Thread handler
1529 @param[out] str String of output
1530 @param query_type Options to print out string output
1531 */
1532 void print_hints(const THD *thd, String *str, enum_query_type query_type);
1533
1534 /**
1535 Print error.
1536
1537 @param thd Thread handler
1538 @param[out] str String of output
1539
1540 @retval false If there is no error
1541 @retval true else
1542 */
1543 bool print_error(const THD *thd, String *str);
1544
1545 /**
1546 Print select options.
1547
1548 @param[out] str String of output
1549 */
1551
1552 /**
1553 Print UPDATE options.
1554
1555 @param[out] str String of output
1556 */
1558
1559 /**
1560 Print DELETE options.
1561
1562 @param[out] str String of output
1563 */
1565
1566 /**
1567 Print INSERT options.
1568
1569 @param[out] str String of output
1570 */
1572
1573 /**
1574 Print list of tables.
1575
1576 @param thd Thread handler
1577 @param[out] str String of output
1578 @param table_list TABLE_LIST object
1579 @param query_type Options to print out string output
1580 */
1581 void print_table_references(const THD *thd, String *str,
1583 enum_query_type query_type);
1584
1585 /**
1586 Print list of items in Query_block object.
1587
1588 @param thd Thread handle
1589 @param[out] str String of output
1590 @param query_type Options to print out string output
1591 */
1592 void print_item_list(const THD *thd, String *str, enum_query_type query_type);
1593
1594 /**
1595 Print assignments list. Used in UPDATE and
1596 INSERT ... ON DUPLICATE KEY UPDATE ...
1597
1598 @param thd Thread handle
1599 @param[out] str String of output
1600 @param query_type Options to print out string output
1601 @param fields List columns to be assigned.
1602 @param values List of values.
1603 */
1604 void print_update_list(const THD *thd, String *str,
1605 enum_query_type query_type,
1607 const mem_root_deque<Item *> &values);
1608
1609 /**
1610 Print column list to be inserted into. Used in INSERT.
1611
1612 @param thd Thread handle
1613 @param[out] str String of output
1614 @param query_type Options to print out string output
1615 */
1616 void print_insert_fields(const THD *thd, String *str,
1617 enum_query_type query_type);
1618
1619 /**
1620 Print list of values, used in INSERT and for general VALUES clause.
1621
1622 @param thd Thread handle
1623 @param[out] str String of output
1624 @param query_type Options to print out string output
1625 @param values List of values
1626 @param prefix Prefix to print before each row in value list
1627 = nullptr: No prefix wanted
1628 */
1629 void print_values(const THD *thd, String *str, enum_query_type query_type,
1630 const mem_root_deque<mem_root_deque<Item *> *> &values,
1631 const char *prefix);
1632
1633 /**
1634 Print list of tables in FROM clause.
1635
1636 @param thd Thread handler
1637 @param[out] str String of output
1638 @param query_type Options to print out string output
1639 */
1640 void print_from_clause(const THD *thd, String *str,
1641 enum_query_type query_type);
1642
1643 /**
1644 Print list of conditions in WHERE clause.
1645
1646 @param thd Thread handle
1647 @param[out] str String of output
1648 @param query_type Options to print out string output
1649 */
1650 void print_where_cond(const THD *thd, String *str,
1651 enum_query_type query_type);
1652
1653 /**
1654 Print list of items in GROUP BY clause.
1655
1656 @param thd Thread handle
1657 @param[out] str String of output
1658 @param query_type Options to print out string output
1659 */
1660 void print_group_by(const THD *thd, String *str, enum_query_type query_type);
1661
1662 /**
1663 Print list of items in HAVING clause.
1664
1665 @param thd Thread handle
1666 @param[out] str String of output
1667 @param query_type Options to print out string output
1668 */
1669 void print_having(const THD *thd, String *str, enum_query_type query_type);
1670
1671 /**
1672 Print details of Windowing functions.
1673
1674 @param thd Thread handler
1675 @param[out] str String of output
1676 @param query_type Options to print out string output
1677 */
1678 void print_windows(const THD *thd, String *str, enum_query_type query_type);
1679
1680 /**
1681 Print list of items in ORDER BY clause.
1682
1683 @param thd Thread handle
1684 @param[out] str String of output
1685 @param query_type Options to print out string output
1686 */
1687 void print_order_by(const THD *thd, String *str, enum_query_type query_type);
1688
1689 static void print_order(const THD *thd, String *str, ORDER *order,
1690 enum_query_type query_type);
1691 void print_limit(const THD *thd, String *str, enum_query_type query_type);
1692 bool save_properties(THD *thd);
1693
1694 /**
1695 Accept function for SELECT and DELETE.
1696
1697 @param visitor Select_lex_visitor Object
1698 */
1699 bool accept(Select_lex_visitor *visitor);
1700
1701 /**
1702 Cleanup this subtree (this Query_block and all nested Query_blockes and
1703 Query_expressions).
1704 @param thd thread handle
1705 @param full if false only partial cleanup is done, JOINs and JOIN_TABs are
1706 kept to provide info for EXPLAIN CONNECTION; if true, complete cleanup is
1707 done, all JOINs are freed.
1708 */
1709 void cleanup(THD *thd, bool full);
1710 /*
1711 Recursively cleanup the join of this select lex and of all nested
1712 select lexes. This is not a full cleanup.
1713 */
1714 void cleanup_all_joins();
1715 /**
1716 Destroy contained objects, in particular temporary tables which may
1717 have their own mem_roots.
1718 */
1719 void destroy();
1720
1721 /// Return true if this query block is part of a UNION
1722 bool is_part_of_union() const {
1724 }
1725
1726 /**
1727 @return true if query block is found during preparation to produce no data.
1728 Notice that if query is implicitly grouped, an aggregation row will
1729 still be returned.
1730 */
1731 bool is_empty_query() const { return m_empty_query; }
1732
1733 /// Set query block as returning no data
1734 /// @todo This may also be set when we have an always false WHERE clause
1736 assert(join == nullptr);
1737 m_empty_query = true;
1738 }
1739 /*
1740 For MODE_ONLY_FULL_GROUP_BY we need to know if
1741 this query block is the aggregation query of at least one aggregate
1742 function.
1743 */
1744 bool agg_func_used() const { return m_agg_func_used; }
1746
1747 void set_agg_func_used(bool val) { m_agg_func_used = val; }
1748
1750
1751 bool right_joins() const { return m_right_joins; }
1753
1754 /// Lookup for Query_block type
1756
1757 /// Lookup for a type string
1758 const char *get_type_str() { return type_str[static_cast<int>(type())]; }
1760 return type_str[static_cast<int>(type)];
1761 }
1762
1764 bool is_cacheable() const { return !uncacheable; }
1765
1766 /// @returns true if this query block outputs at most one row.
1768 return (table_list.size() == 0 &&
1769 (!is_table_value_constructor || row_value_list->size() == 1));
1770 }
1771
1772 /// Include query block inside a query expression.
1773 void include_down(LEX *lex, Query_expression *outer);
1774
1775 /// Include a query block next to another query block.
1776 void include_neighbour(LEX *lex, Query_block *before);
1777
1778 /// Include query block inside a query expression, but do not link.
1780
1781 /// Include query block into global list.
1782 void include_in_global(Query_block **plink);
1783
1784 /// Include chain of query blocks into global list.
1786
1787 /// Renumber query blocks of contained query expressions
1788 void renumber(LEX *lex);
1789
1790 /**
1791 Does permanent transformations which are local to a query block (which do
1792 not merge it to another block).
1793 */
1794 bool apply_local_transforms(THD *thd, bool prune);
1795
1796 /// Pushes parts of the WHERE condition of this query block to materialized
1797 /// derived tables.
1799
1800 bool get_optimizable_conditions(THD *thd, Item **new_where,
1801 Item **new_having);
1802
1803 bool validate_outermost_option(LEX *lex, const char *wrong_option) const;
1804 bool validate_base_options(LEX *lex, ulonglong options) const;
1805
1806 bool walk(Item_processor processor, enum_walk walk, uchar *arg);
1807
1808 bool add_tables(THD *thd, const Mem_root_array<Table_ident *> *tables,
1809 ulong table_options, thr_lock_type lock_type,
1810 enum_mdl_type mdl_type);
1811
1812 bool resolve_rollup_wfs(THD *thd);
1813
1814 bool setup_conds(THD *thd);
1815 bool prepare(THD *thd, mem_root_deque<Item *> *insert_field_list);
1816 bool optimize(THD *thd, bool finalize_access_paths);
1818
1819 bool change_group_ref_for_func(THD *thd, Item *func, bool *changed);
1820 bool change_group_ref_for_cond(THD *thd, Item_cond *cond, bool *changed);
1821
1822 // If the query block has exactly one single visible field, returns it.
1823 // If not, returns nullptr.
1824 Item *single_visible_field() const;
1825 size_t num_visible_fields() const;
1826
1827 // Whether the SELECT list is empty (hidden fields are ignored).
1828 // Typically used to distinguish INSERT INTO ... SELECT queries
1829 // from INSERT INTO ... VALUES queries.
1830 bool field_list_is_empty() const;
1831
1832 void remove_hidden_fields();
1833 /// Creates a clone for the given expression by re-parsing the
1834 /// expression. Used in condition pushdown to derived tables.
1835 Item *clone_expression(THD *thd, Item *item, bool is_system_view);
1836 /// Returns an expression from the select list of the query block
1837 /// using the field's index in a derived table.
1838 Item *get_derived_expr(uint expr_index);
1839
1840 // ************************************************
1841 // * Members (most of these should not be public) *
1842 // ************************************************
1843
1845 /**
1846 All expressions needed after join and filtering, ie., select list,
1847 group by list, having clause, window clause, order by clause,
1848 including hidden fields.
1849 Does not include join conditions nor where clause.
1850
1851 This should ideally be changed into Mem_root_array<Item *>, but
1852 find_order_in_list() depends on pointer stability (it stores a pointer
1853 to an element in referenced_by[]). Similarly, there are some instances
1854 of thd->change_item_tree() that store pointers to elements in this list.
1855
1856 Because of this, adding or removing elements in the middle is not allowed;
1857 std::deque guarantees pointer stability only in the face of adding
1858 or removing elements from either end, ie., {push,pop}_{front_back}.
1859
1860 Currently, all hidden items must be before all visible items.
1861 This is primarily due to the requirement for pointer stability
1862 (remove_hidden_fields() runs during cleanup), but also because
1863 change_to_use_tmp_fields() depends on it when mapping items to
1864 ref_item_array indexes. It would be good to get rid of this
1865 requirement in the future.
1866 */
1868
1869 /**
1870 All windows defined on the select, both named and inlined
1871 */
1873
1874 /**
1875 Usually a pointer to ftfunc_list_alloc, but in UNION this is used to create
1876 fake query_block that consolidates result fields of UNION
1877 */
1880
1881 /// The VALUES items of a table value constructor.
1883
1884 /// List of semi-join nests generated for this query block
1886
1887 /// List of tables in FROM clause - use TABLE_LIST::next_local to traverse
1889
1890 /**
1891 ORDER BY clause.
1892 This list may be mutated during optimization (by remove_const()),
1893 so for prepared statements, we keep a copy of the ORDER.next pointers in
1894 order_list_ptrs, and re-establish the original list before each execution.
1895 */
1898
1899 /**
1900 GROUP BY clause.
1901 This list may be mutated during optimization (by remove_const()),
1902 so for prepared statements, we keep a copy of the ORDER.next pointers in
1903 group_list_ptrs, and re-establish the original list before each execution.
1904 */
1907
1908 // Used so that AggregateIterator knows which items to signal when the rollup
1909 // level changes. Obviously only used in the presence of rollup.
1914
1915 /// Query-block-level hints, for this query block
1917
1918 char *db{nullptr};
1919
1920 /**
1921 If this query block is a recursive member of a recursive unit: the
1922 TABLE_LIST, in this recursive member, referencing the query
1923 name.
1924 */
1926
1927 /// Reference to LEX that this query block belongs to
1928 LEX *parent_lex{nullptr};
1929
1930 /**
1931 The set of those tables whose fields are referenced in the select list of
1932 this select level.
1933 */
1935 table_map outer_join{0}; ///< Bitmap of all inner tables from outer joins
1936
1937 /**
1938 Context for name resolution for all column references except columns
1939 from joined tables.
1940 */
1942
1943 /**
1944 Pointer to first object in list of Name res context objects that have
1945 this query block as the base query block.
1946 Includes field "context" which is embedded in this query block.
1947 */
1949
1950 /**
1951 After optimization it is pointer to corresponding JOIN. This member
1952 should be changed only when THD::LOCK_query_plan mutex is taken.
1953 */
1954 JOIN *join{nullptr};
1955 /// join list of the top level
1957 /// list for the currently parsed join
1959 /// table embedding the above list
1961 /**
1962 Points to first leaf table of query block. After setup_tables() is done,
1963 this is a list of base tables and derived tables. After derived tables
1964 processing is done, this is a list of base tables only.
1965 Use TABLE_LIST::next_leaf to traverse the list.
1966 */
1968 // Last table for LATERAL join, used by table functions
1970
1971 /// LIMIT clause, NULL if no limit is given
1973 /// LIMIT ... OFFSET clause, NULL if no offset is given
1975
1976 /**
1977 Circular linked list of aggregate functions in nested query blocks.
1978 This is needed if said aggregate functions depend on outer values
1979 from this query block; if so, we want to add them as hidden items
1980 in our own field list, to be able to evaluate them.
1981 @see Item_sum::check_sum_func
1982 */
1984
1985 /**
1986 Array of pointers to "base" items; one each for every selected expression
1987 and referenced item in the query block. All references to fields are to
1988 buffers associated with the primary input tables.
1989 */
1991
1992 uint select_number{0}; ///< Query block number (used for EXPLAIN)
1993
1994 /**
1995 Saved values of the WHERE and HAVING clauses. Allowed values are:
1996 - COND_UNDEF if the condition was not specified in the query or if it
1997 has not been optimized yet
1998 - COND_TRUE if the condition is always true
1999 - COND_FALSE if the condition is impossible
2000 - COND_OK otherwise
2001 */
2004
2005 /// Parse context: indicates where the current expression is being parsed
2007 /// Parse context: is inside a set function if this is positive
2009
2010 /**
2011 Three fields used by semi-join transformations to know when semi-join is
2012 possible, and in which condition tree the subquery predicate is located.
2013 */
2022 RESOLVE_NONE}; ///< Indicates part of query being resolved
2023
2024 /**
2025 Number of fields used in select list or where clause of current select
2026 and all inner subselects.
2027 */
2029 /**
2030 number of items in select_list and HAVING clause used to get number
2031 bigger then can be number of entries that will be added to all item
2032 list during split_sum_func
2033 */
2035 uint cond_count{0}; ///< number of arguments of and/or/xor in where/having/on
2036 uint between_count{0}; ///< number of between predicates in where/having/on
2038 0}; ///< maximal number of elements in multiple equalities
2039
2040 /**
2041 Number of Item_sum-derived objects in this SELECT. Keeps count of
2042 aggregate functions and window functions(to allocate items in ref array).
2043 See Query_block::setup_base_ref_items.
2044 */
2046 /// Number of Item_sum-derived objects in children and descendant SELECTs
2048
2049 /// Keep track for allocation of base_ref_items: scalar subqueries may be
2050 /// replaced by a field during scalar_to_derived transformation
2052
2053 /// Number of materialized derived tables and views in this query block.
2055 /// Number of partitioned tables
2057
2058 /**
2059 Number of wildcards used in the SELECT list. For example,
2060 SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
2061 has 3 wildcards.
2062 */
2064
2065 /// Number of leaf tables in this query block.
2067 /// Number of derived tables and views in this query block.
2069 /// Number of table functions in this query block
2071
2072 /**
2073 Nesting level of query block, outer-most query block has level 0,
2074 its subqueries have level 1, etc. @see also sql/item_sum.h.
2075 */
2077
2078 /// Indicates whether this query block contains the WITH ROLLUP clause
2080
2081 /// @see enum_condition_context
2083
2084 /// If set, the query block is of the form VALUES row_list.
2086
2087 /// Describes context of this query block (e.g if it is a derived table).
2089
2090 /**
2091 result of this query can't be cached, bit field, can be :
2092 UNCACHEABLE_DEPENDENT
2093 UNCACHEABLE_RAND
2094 UNCACHEABLE_SIDEEFFECT
2095 */
2097
2098 void update_used_tables();
2100 bool save_cmd_properties(THD *thd);
2101
2102 /**
2103 This variable is required to ensure proper work of subqueries and
2104 stored procedures. Generally, one should use the states of
2105 Query_arena to determine if it's a statement prepare or first
2106 execution of a stored procedure. However, in case when there was an
2107 error during the first execution of a stored procedure, the SP body
2108 is not expelled from the SP cache. Therefore, a deeply nested
2109 subquery might be left unoptimized. So we need this per-subquery
2110 variable to inidicate the optimization/execution state of every
2111 subquery. Prepared statements work OK in that regard, as in
2112 case of an error during prepare the PS is not created.
2113 */
2115
2116 /// True when semi-join pull-out processing is complete
2117 bool sj_pullout_done{false};
2118
2119 /// Used by nested scalar_to_derived transformations
2121
2122 /// True: skip local transformations during prepare() call (used by INSERT)
2124
2126
2127 /// true when having fix field called in processing of this query block
2128 bool having_fix_field{false};
2129 /// true when GROUP BY fix field called in processing of this query block
2130 bool group_fix_field{false};
2131
2132 /**
2133 True if contains or aggregates set functions.
2134 @note this is wrong when a locally found set function is aggregated
2135 in an outer query block.
2136 */
2137 bool with_sum_func{false};
2138
2139 /**
2140 HAVING clause contains subquery => we can't close tables before
2141 query processing end even if we use temporary table
2142 */
2144
2145 /**
2146 If true, use select_limit to limit number of rows selected.
2147 Applicable when no explicit limit is supplied, and only for the
2148 outermost query block of a SELECT statement.
2149 */
2151
2152 /// If true, limit object is added internally
2153 bool m_internal_limit{false};
2154
2155 /// exclude this query block from unique_table() check
2157
2158 bool no_table_names_allowed{false}; ///< used for global order by
2159
2160 /// Hidden items added during optimization
2161 /// @note that using this means we modify resolved data during optimization
2163
2164 private:
2165 friend class Query_expression;
2166 friend class Condition_context;
2167
2168 /// Helper for save_properties()
2170 Group_list_ptrs **list_ptrs);
2171
2174 bool top, bool in_sj, Item **new_conds,
2175 uint *changelog = nullptr);
2176 /// Remove semijoin condition for this query block
2177 void clear_sj_expressions(NESTED_JOIN *nested_join);
2178 /// Build semijoin condition for th query block
2179 bool build_sj_cond(THD *thd, NESTED_JOIN *nested_join,
2180 Query_block *subq_query_block, table_map outer_tables_map,
2181 Item **sj_cond);
2183 TABLE_LIST *join_nest);
2184
2187 Item *join_cond, bool left_outer,
2188 bool use_inner_join);
2189 bool transform_subquery_to_derived(THD *thd, TABLE_LIST **out_tl,
2190 Query_expression *subs_query_expression,
2191 Item_subselect *subq, bool use_inner_join,
2192 bool reject_multiple_rows,
2193 Item *join_condition,
2194 Item *lifted_where_cond);
2196 THD *thd, Item_exists_subselect *subq_pred);
2198 THD *thd, TABLE_LIST *derived, Item *lifted_where,
2199 Lifted_fields_map *lifted_where_fields, bool *added_card_check);
2201 THD *thd, TABLE_LIST *derived, Lifted_fields_map *lifted_where_fields,
2202 bool added_card_check);
2203 void remap_tables(THD *thd);
2204 bool resolve_subquery(THD *thd);
2206 Item *resolve_rollup_item(THD *thd, Item *item);
2207 bool resolve_rollup(THD *thd);
2208
2209 bool setup_wild(THD *thd);
2210 bool setup_order_final(THD *thd);
2211 bool setup_group(THD *thd);
2212 void fix_after_pullout(Query_block *parent_query_block,
2213 Query_block *removed_query_block);
2217 void empty_order_list(Query_block *sl);
2219 bool in_update);
2220 bool find_common_table_expr(THD *thd, Table_ident *table_id, TABLE_LIST *tl,
2221 Parse_context *pc, bool *found);
2222 /**
2223 Transform eligible scalar subqueries in the SELECT list, WHERE condition,
2224 HAVING condition or JOIN conditions of a query block[*] to an equivalent
2225 derived table of a LEFT OUTER join, e.g. as shown in this uncorrelated
2226 subquery:
2227
2228 [*] a.k.a "transformed query block" throughout this method and its minions.
2229
2230 <pre>
2231 SELECT * FROM t1
2232 WHERE t1.a > (SELECT COUNT(a) AS cnt FROM t2); ->
2233
2234 SELECT t1.* FROM t1 LEFT OUTER JOIN
2235 (SELECT COUNT(a) AS cnt FROM t2) AS derived
2236 ON TRUE WHERE t1.a > derived.cnt;
2237 </pre>
2238
2239 Grouping in the transformed query block may necessitate the grouping to be
2240 moved down to another derived table, cf. transform_grouped_to_derived.
2241
2242 Limitations:
2243 - only implicitly grouped subqueries (guaranteed to have cardinality one)
2244 are identified as scalar subqueries.
2245 _ Correlated subqueries are not handled
2246
2247 @param[in,out] thd the session context
2248 @returns true on error
2249 */
2252 Item **lifted_where);
2253 bool transform_grouped_to_derived(THD *thd, bool *break_off);
2254 bool replace_subquery_in_expr(THD *thd, Item::Css_info *subquery,
2255 TABLE_LIST *tr, Item **expr);
2256 bool nest_derived(THD *thd, Item *join_cond,
2258 TABLE_LIST *new_derived_table);
2259
2261
2262 // Delete unused columns from merged derived tables
2264
2265 /// Helper for fix_prepare_information()
2267 Group_list_ptrs **list_ptrs);
2268
2269 bool prepare_values(THD *thd);
2270 bool check_only_full_group_by(THD *thd);
2272
2273 //
2274 // Members:
2275 //
2276
2277 /**
2278 Pointer to collection of subqueries candidate for semi/antijoin
2279 conversion.
2280 Template parameter is "true": no need to run DTORs on pointers.
2281 */
2283
2284 /// How many expressions are part of the order by but not select list.
2286
2287 /**
2288 Intrusive double-linked list of all query blocks within the same
2289 query expression.
2290 */
2292 Query_block **prev{nullptr};
2293
2294 /// The query expression containing this query block.
2296 /// The first query expression contained within this query block.
2298
2299 /// Intrusive double-linked global list of query blocks.
2302
2303 /// Result of this query block
2305
2306 /**
2307 Options assigned from parsing and throughout resolving,
2308 should not be modified after resolving is done.
2309 */
2311 /**
2312 Active options. Derived from base options, modifiers added during
2313 resolving and values from session variable option_bits. Since the latter
2314 may change, active options are refreshed per execution of a statement.
2315 */
2317
2319 nullptr}; ///< Used when resolving outer join condition
2320
2321 /**
2322 Condition to be evaluated after all tables in a query block are joined.
2323 After all permanent transformations have been conducted by
2324 Query_block::prepare(), this condition is "frozen", any subsequent changes
2325 to it must be done with change_item_tree(), unless they only modify AND/OR
2326 items and use a copy created by Query_block::get_optimizable_conditions().
2327 Same is true for 'having_cond'.
2328 */
2330
2331 /// Condition to be evaluated on grouped rows after grouping.
2333
2334 /// Number of GROUP BY expressions added to all_fields
2336
2337 /**
2338 True if query block has semi-join nests merged into it. Notice that this
2339 is updated earlier than sj_nests, so check this if info is needed
2340 before the full resolver process is complete.
2341 */
2342 bool has_sj_nests{false};
2343 bool has_aj_nests{false}; ///< @see has_sj_nests; counts antijoin nests.
2344 bool m_right_joins{false}; ///< True if query block has right joins
2345
2346 /// Allow merge of immediate unnamed derived tables
2348
2349 bool m_agg_func_used{false};
2351
2352 /**
2353 True if query block does not generate any rows before aggregation,
2354 determined during preparation (not optimization).
2355 */
2356 bool m_empty_query{false};
2357
2358 static const char
2360};
2361
2362inline bool Query_expression::is_union() const {
2363 return first_query_block()->next_query_block() &&
2365}
2366
2367/// Utility RAII class to save/modify/restore the condition_context information
2368/// of a query block. @see enum_condition_context.
2370 public:
2372 Query_block *select_ptr,
2374 : select(nullptr), saved_value() {
2375 if (select_ptr) {
2376 select = select_ptr;
2378 // More restrictive wins over less restrictive:
2379 if (new_type == enum_condition_context::NEITHER ||
2380 (new_type == enum_condition_context::ANDS_ORS &&
2382 select->condition_context = new_type;
2383 }
2384 }
2387 }
2388
2389 private:
2392};
2393
2395 std::function<bool(TABLE_LIST *)> action);
2396
2397/**
2398 Base class for secondary engine execution context objects. Secondary
2399 storage engines may create classes derived from this one which
2400 contain state they need to preserve between optimization and
2401 execution of statements. The context objects should be allocated on
2402 the execution MEM_ROOT.
2403*/
2405 public:
2406 /**
2407 Destructs the secondary engine execution context object. It is
2408 called after the query execution has completed. Secondary engines
2409 may override the destructor in subclasses and add code that
2410 performs cleanup tasks that are needed after query execution.
2411 */
2413};
2414
2416 char *user;
2420
2421 void reset();
2423
2429};
2430
2431extern const LEX_STRING null_lex_str;
2432
2436
2437 /**
2438 FOLLOWS or PRECEDES as specified in the CREATE TRIGGER statement.
2439 */
2441
2442 /**
2443 Trigger name referenced in the FOLLOWS/PRECEDES clause of the CREATE TRIGGER
2444 statement.
2445 */
2447};
2448
2450
2451/*
2452 Class representing list of all tables used by statement and other
2453 information which is necessary for opening and locking its tables,
2454 like SQL command for this statement.
2455
2456 Also contains information about stored functions used by statement
2457 since during its execution we may have to add all tables used by its
2458 stored functions/triggers to this list in order to pre-open and lock
2459 them.
2460
2461 Also used by LEX::reset_n_backup/restore_backup_query_tables_list()
2462 methods to save and restore this information.
2463*/
2464
2466 public:
2468
2469 /**
2470 SQL command for this statement. Part of this class since the
2471 process of opening and locking tables for the statement needs
2472 this information to determine correct type of lock for some of
2473 the tables.
2474 */
2476 /* Global list of all tables used by this statement */
2478 /* Pointer to next_global member of last element in the previous list. */
2480 /*
2481 If non-0 then indicates that query requires prelocking and points to
2482 next_global member of last own element in query table list (i.e. last
2483 table which was not added to it as part of preparation to prelocking).
2484 0 - indicates that this query does not need prelocking.
2485 */
2487 /*
2488 Set of stored routines called by statement.
2489 (Note that we use lazy-initialization for this hash).
2490
2491 See Sroutine_hash_entry for explanation why this hash uses binary
2492 key comparison.
2493 */
2495 std::unique_ptr<malloc_unordered_map<std::string, Sroutine_hash_entry *>>
2497 /*
2498 List linking elements of 'sroutines' set. Allows you to add new elements
2499 to this set as you iterate through the list of existing elements.
2500 'sroutines_list_own_last' is pointer to ::next member of last element of
2501 this list which represents routine which is explicitly used by query.
2502 'sroutines_list_own_elements' number of explicitly used routines.
2503 We use these two members for restoring of 'sroutines_list' to the state
2504 in which it was right after query parsing.
2505 */
2509
2510 /**
2511 Locking state of tables in this particular statement.
2512
2513 If we under LOCK TABLES or in prelocked mode we consider tables
2514 for the statement to be "locked" if there was a call to lock_tables()
2515 (which called handler::start_stmt()) for tables of this statement
2516 and there was no matching close_thread_tables() call.
2517
2518 As result this state may differ significantly from one represented
2519 by Open_tables_state::lock/locked_tables_mode more, which are always
2520 "on" under LOCK TABLES or in prelocked mode.
2521 */
2525 return (lock_tables_state == LTS_LOCKED);
2526 }
2527
2528 /**
2529 Number of tables which were open by open_tables() and to be locked
2530 by lock_tables().
2531 Note that we set this member only in some cases, when this value
2532 needs to be passed from open_tables() to lock_tables() which are
2533 separated by some amount of code.
2534 */
2536
2537 /*
2538 These constructor and destructor serve for creation/destruction
2539 of Query_tables_list instances which are used as backup storage.
2540 */
2543
2544 /* Initializes (or resets) Query_tables_list object for "real" use. */
2545 void reset_query_tables_list(bool init);
2548 *this = std::move(*state);
2549 }
2550
2551 /*
2552 Direct addition to the list of query tables.
2553 If you are using this function, you must ensure that the table
2554 object, in particular table->db member, is initialized.
2555 */
2557 *(table->prev_global = query_tables_last) = table;
2559 }
2562 query_tables_own_last = tables_own_last;
2563 }
2564 /* Return pointer to first not-own table in query-tables or 0 */
2566 return (query_tables_own_last ? *query_tables_own_last : nullptr);
2567 }
2570 *query_tables_own_last = nullptr;
2572 query_tables_own_last = nullptr;
2573 }
2574 }
2575
2576 /**
2577 All types of unsafe statements.
2578
2579 @note The int values of the enum elements are used to point to
2580 bits in two bitmaps in two different places:
2581
2582 - Query_tables_list::binlog_stmt_flags
2583 - THD::binlog_unsafe_warning_flags
2584
2585 Hence in practice this is not an enum at all, but a map from
2586 symbols to bit indexes.
2587
2588 The ordering of elements in this enum must correspond to the order of
2589 elements in the array binlog_stmt_unsafe_errcode.
2590 */
2592 /**
2593 SELECT..LIMIT is unsafe because the set of rows returned cannot
2594 be predicted.
2595 */
2597 /**
2598 Access to log tables is unsafe because slave and master probably
2599 log different things.
2600 */
2602 /**
2603 Inserting into an autoincrement column in a stored routine is unsafe.
2604 Even with just one autoincrement column, if the routine is invoked more
2605 than once slave is not guaranteed to execute the statement graph same way
2606 as the master. And since it's impossible to estimate how many times a
2607 routine can be invoked at the query pre-execution phase (see lock_tables),
2608 the statement is marked pessimistically unsafe.
2609 */
2611 /**
2612 Using a UDF (user-defined function) is unsafe.
2613 */
2615 /**
2616 Using most system variables is unsafe, because slave may run
2617 with different options than master.
2618 */
2620 /**
2621 Using some functions is unsafe (e.g., UUID).
2622 */
2624
2625 /**
2626 Mixing transactional and non-transactional statements are unsafe if
2627 non-transactional reads or writes are occur after transactional
2628 reads or writes inside a transaction.
2629 */
2631
2632 /**
2633 Mixing self-logging and non-self-logging engines in a statement
2634 is unsafe.
2635 */
2637
2638 /**
2639 Statements that read from both transactional and non-transactional
2640 tables and write to any of them are unsafe.
2641 */
2643
2644 /**
2645 INSERT...IGNORE SELECT is unsafe because which rows are ignored depends
2646 on the order that rows are retrieved by SELECT. This order cannot be
2647 predicted and may differ on master and the slave.
2648 */
2650
2651 /**
2652 INSERT...SELECT...UPDATE is unsafe because which rows are updated depends
2653 on the order that rows are retrieved by SELECT. This order cannot be
2654 predicted and may differ on master and the slave.
2655 */
2657
2658 /**
2659 Query that writes to a table with auto_inc column after selecting from
2660 other tables are unsafe as the order in which the rows are retrieved by
2661 select may differ on master and slave.
2662 */
2664
2665 /**
2666 INSERT...REPLACE SELECT is unsafe because which rows are replaced depends
2667 on the order that rows are retrieved by SELECT. This order cannot be
2668 predicted and may differ on master and the slave.
2669 */
2671
2672 /**
2673 CREATE TABLE... IGNORE... SELECT is unsafe because which rows are ignored
2674 depends on the order that rows are retrieved by SELECT. This order cannot
2675 be predicted and may differ on master and the slave.
2676 */
2678
2679 /**
2680 CREATE TABLE...REPLACE... SELECT is unsafe because which rows are replaced
2681 depends on the order that rows are retrieved from SELECT. This order
2682 cannot be predicted and may differ on master and the slave
2683 */
2685
2686 /**
2687 CREATE TABLE...SELECT on a table with auto-increment column is unsafe
2688 because which rows are replaced depends on the order that rows are
2689 retrieved from SELECT. This order cannot be predicted and may differ on
2690 master and the slave
2691 */
2693
2694 /**
2695 UPDATE...IGNORE is unsafe because which rows are ignored depends on the
2696 order that rows are updated. This order cannot be predicted and may differ
2697 on master and the slave.
2698 */
2700
2701 /**
2702 INSERT... ON DUPLICATE KEY UPDATE on a table with more than one
2703 UNIQUE KEYS is unsafe.
2704 */
2706
2707 /**
2708 INSERT into auto-inc field which is not the first part in composed
2709 primary key.
2710 */
2712
2713 /**
2714 Using a plugin is unsafe.
2715 */
2719
2720 /**
2721 XA transactions and statements.
2722 */
2724
2725 /**
2726 If a substatement inserts into or updates a table that has a column with
2727 an unsafe DEFAULT expression, it may not have the same effect on the
2728 slave.
2729 */
2731
2732 /**
2733 DML or DDL statement that reads a ACL table is unsafe, because the row
2734 are read without acquiring SE row locks. This would allow ACL tables to
2735 be updated by concurrent thread. It would not have the same effect on the
2736 slave.
2737 */
2739
2740 /* the last element of this enumeration type. */
2743 /**
2744 This has all flags from 0 (inclusive) to BINLOG_STMT_FLAG_COUNT
2745 (exclusive) set.
2746 */
2748 ((1 << BINLOG_STMT_UNSAFE_COUNT) - 1);
2749
2750 /**
2751 Maps elements of enum_binlog_stmt_unsafe to error codes.
2752 */
2754
2755 /**
2756 Determine if this statement is marked as unsafe.
2757
2758 @retval 0 if the statement is not marked as unsafe.
2759 @retval nonzero if the statement is marked as unsafe.
2760 */
2761 inline bool is_stmt_unsafe() const { return get_stmt_unsafe_flags() != 0; }
2762
2764 return binlog_stmt_flags & (1 << unsafe);
2765 }
2766
2767 /**
2768 Flag the current (top-level) statement as unsafe.
2769 The flag will be reset after the statement has finished.
2770
2771 @param unsafe_type The type of unsafety: one of the @c
2772 BINLOG_STMT_FLAG_UNSAFE_* flags in @c enum_binlog_stmt_flag.
2773 */
2774 inline void set_stmt_unsafe(enum_binlog_stmt_unsafe unsafe_type) {
2775 DBUG_TRACE;
2776 assert(unsafe_type >= 0 && unsafe_type < BINLOG_STMT_UNSAFE_COUNT);
2777 binlog_stmt_flags |= (1U << unsafe_type);
2778 return;
2779 }
2780
2781 /**
2782 Set the bits of binlog_stmt_flags determining the type of
2783 unsafeness of the current statement. No existing bits will be
2784 cleared, but new bits may be set.
2785
2786 @param flags A binary combination of zero or more bits, (1<<flag)
2787 where flag is a member of enum_binlog_stmt_unsafe.
2788 */
2790 DBUG_TRACE;
2791 assert((flags & ~BINLOG_STMT_UNSAFE_ALL_FLAGS) == 0);
2793 return;
2794 }
2795
2796 /**
2797 Return a binary combination of all unsafe warnings for the
2798 statement. If the statement has been marked as unsafe by the
2799 'flag' member of enum_binlog_stmt_unsafe, then the return value
2800 from this function has bit (1<<flag) set to 1.
2801 */
2803 DBUG_TRACE;
2805 }
2806
2807 /**
2808 Determine if this statement is a row injection.
2809
2810 @retval 0 if the statement is not a row injection
2811 @retval nonzero if the statement is a row injection
2812 */
2813 inline bool is_stmt_row_injection() const {
2814 return binlog_stmt_flags &
2816 }
2817
2818 /**
2819 Flag the statement as a row injection. A row injection is either
2820 a BINLOG statement, or a row event in the relay log executed by
2821 the slave SQL thread.
2822 */
2824 DBUG_TRACE;
2827 return;
2828 }
2829
2831 /*
2832 If a transactional table is about to be read. Note that
2833 a write implies a read.
2834 */
2836 /*
2837 If a non-transactional table is about to be read. Note that
2838 a write implies a read.
2839 */
2841 /*
2842 If a temporary transactional table is about to be read. Note
2843 that a write implies a read.
2844 */
2846 /*
2847 If a temporary non-transactional table is about to be read. Note
2848 that a write implies a read.
2849 */
2851 /*
2852 If a transactional table is about to be updated.
2853 */
2855 /*
2856 If a non-transactional table is about to be updated.
2857 */
2859 /*
2860 If a temporary transactional table is about to be updated.
2861 */
2863 /*
2864 If a temporary non-transactional table is about to be updated.
2865 */
2867 /*
2868 The last element of the enumeration. Please, if necessary add
2869 anything before this.
2870 */
2873
2874#ifndef NDEBUG
2875 static inline const char *stmt_accessed_table_string(
2876 enum_stmt_accessed_table accessed_table) {
2877 switch (accessed_table) {
2879 return "STMT_READS_TRANS_TABLE";
2880 break;
2882 return "STMT_READS_NON_TRANS_TABLE";
2883 break;
2885 return "STMT_READS_TEMP_TRANS_TABLE";
2886 break;
2888 return "STMT_READS_TEMP_NON_TRANS_TABLE";
2889 break;
2891 return "STMT_WRITES_TRANS_TABLE";
2892 break;
2894 return "STMT_WRITES_NON_TRANS_TABLE";
2895 break;
2897 return "STMT_WRITES_TEMP_TRANS_TABLE";
2898 break;
2900 return "STMT_WRITES_TEMP_NON_TRANS_TABLE";
2901 break;
2903 default:
2904 assert(0);
2905 break;
2906 }
2908 return "";
2909 }
2910#endif /* DBUG */
2911
2912#define BINLOG_DIRECT_ON \
2913 0xF0 /* unsafe when \
2914 --binlog-direct-non-trans-updates \
2915 is ON */
2916
2917#define BINLOG_DIRECT_OFF \
2918 0xF /* unsafe when \
2919 --binlog-direct-non-trans-updates \
2920 is OFF */
2921
2922#define TRX_CACHE_EMPTY 0x33 /* unsafe when trx-cache is empty */
2923
2924#define TRX_CACHE_NOT_EMPTY 0xCC /* unsafe when trx-cache is not empty */
2925
2926#define IL_LT_REPEATABLE 0xAA /* unsafe when < ISO_REPEATABLE_READ */
2927
2928#define IL_GTE_REPEATABLE 0x55 /* unsafe when >= ISO_REPEATABLE_READ */
2929
2930 /**
2931 Sets the type of table that is about to be accessed while executing a
2932 statement.
2934 @param accessed_table Enumeration type that defines the type of table,
2935 e.g. temporary, transactional, non-transactional.
2936 */
2937 inline void set_stmt_accessed_table(enum_stmt_accessed_table accessed_table) {
2938 DBUG_TRACE;
2939
2940 assert(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
2941 stmt_accessed_table_flag |= (1U << accessed_table);
2942
2943 return;
2944 }
2945
2946 /**
2947 Checks if a type of table is about to be accessed while executing a
2948 statement.
2949
2950 @param accessed_table Enumeration type that defines the type of table,
2951 e.g. temporary, transactional, non-transactional.
2953 @retval true if the type of the table is about to be accessed
2954 @retval false otherwise
2955 */
2956 inline bool stmt_accessed_table(enum_stmt_accessed_table accessed_table) {
2957 DBUG_TRACE;
2958
2959 assert(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
2960
2961 return (stmt_accessed_table_flag & (1U << accessed_table)) != 0;
2962 }
2963
2964 /*
2965 Checks if a mixed statement is unsafe.
2966
2967
2968 @param in_multi_stmt_transaction_mode defines if there is an on-going
2969 multi-transactional statement.
2970 @param binlog_direct defines if --binlog-direct-non-trans-updates is
2971 active.
2972 @param trx_cache_is_not_empty defines if the trx-cache is empty or not.
2973 @param trx_isolation defines the isolation level.
2974
2975 @return
2976 @retval true if the mixed statement is unsafe
2977 @retval false otherwise
2978 */
2979 inline bool is_mixed_stmt_unsafe(bool in_multi_stmt_transaction_mode,
2980 bool binlog_direct,
2981 bool trx_cache_is_not_empty,
2982 uint tx_isolation) {
2983 bool unsafe = false;
2984
2985 if (in_multi_stmt_transaction_mode) {
2986 uint condition =
2987 (binlog_direct ? BINLOG_DIRECT_ON : BINLOG_DIRECT_OFF) &
2988 (trx_cache_is_not_empty ? TRX_CACHE_NOT_EMPTY : TRX_CACHE_EMPTY) &
2989 (tx_isolation >= ISO_REPEATABLE_READ ? IL_GTE_REPEATABLE
2991
2992 unsafe = (binlog_unsafe_map[stmt_accessed_table_flag] & condition);
2993
2994#if !defined(NDEBUG)
2995 DBUG_PRINT("LEX::is_mixed_stmt_unsafe",
2996 ("RESULT %02X %02X %02X\n", condition,
2999
3000 int type_in = 0;
3001 for (; type_in < STMT_ACCESS_TABLE_COUNT; type_in++) {
3003 DBUG_PRINT("LEX::is_mixed_stmt_unsafe",
3004 ("ACCESSED %s ", stmt_accessed_table_string(
3005 (enum_stmt_accessed_table)type_in)));
3006 }
3007#endif
3008 }
3009
3012 tx_isolation < ISO_REPEATABLE_READ)
3013 unsafe = true;
3016 tx_isolation < ISO_REPEATABLE_READ)
3017 unsafe = true;
3018
3019 return (unsafe);
3020 }
3021
3022 /**
3023 true if the parsed tree contains references to stored procedures
3024 or functions, false otherwise
3026 bool uses_stored_routines() const { return sroutines_list.elements != 0; }
3028 void set_using_match() { using_match = true; }
3029 bool get_using_match() { return using_match; }
3030
3032 bool is_stmt_unsafe_with_mixed_mode() const {
3034 }
3035
3036 private:
3037 /**
3038 Enumeration listing special types of statements.
3039
3040 Currently, the only possible type is ROW_INJECTION.
3041 */
3043 /**
3044 The statement is a row injection (i.e., either a BINLOG
3045 statement or a row event executed by the slave SQL thread).
3046 */
3048
3049 /** The last element of this enumeration type. */
3051 };
3052
3053 /**
3054 Bit field indicating the type of statement.
3055
3056 There are two groups of bits:
3057
3058 - The low BINLOG_STMT_UNSAFE_COUNT bits indicate the types of
3059 unsafeness that the current statement has.
3060
3061 - The next BINLOG_STMT_TYPE_COUNT bits indicate if the statement
3062 is of some special type.
3063
3064 This must be a member of LEX, not of THD: each stored procedure
3065 needs to remember its unsafeness state between calls and each
3066 stored procedure has its own LEX object (but no own THD object).
3067 */
3069
3070 /**
3071 Bit field that determines the type of tables that are about to be
3072 be accessed while executing a statement.
3073 */
3076 /**
3077 It will be set true if 'MATCH () AGAINST' is used in the statement.
3078 */
3079 bool using_match;
3080
3081 /**
3082 This flag is set to true if statement is unsafe to be binlogged in STATEMENT
3083 format, when in MIXED mode.
3084 Currently this flag is set to true if stored program used in statement has
3085 CREATE/DROP temporary table operation(s) as sub-statement(s).
3086 */
3087 bool stmt_unsafe_with_mixed_mode{false};
3088};
3089
3090/*
3091 st_parsing_options contains the flags for constructions that are
3092 allowed in the current statement.
3094
3096 bool allows_variable;
3097 bool allows_select_into;
3098
3099 st_parsing_options() { reset(); }
3100 void reset();
3101};
3103/**
3104 The state of the lexical parser, when parsing comments.
3105*/
3107 /**
3108 Not parsing comments.
3109 */
3110 NO_COMMENT,
3111
3112 /**
3113 Parsing comments that need to be preserved.
3114 (Copy '/' '*' and '*' '/' sequences to the preprocessed buffer.)
3115 Typically, these are user comments '/' '*' ... '*' '/'.
3116 */
3118
3119 /**
3120 Parsing comments that need to be discarded.
3121 (Don't copy '/' '*' '!' and '*' '/' sequences to the preprocessed buffer.)
3122 Typically, these are special comments '/' '*' '!' ... '*' '/',
3123 or '/' '*' '!' 'M' 'M' 'm' 'm' 'm' ... '*' '/', where the comment
3124 markers should not be expanded.
3125 */
3127};
3128
3129/**
3130 This class represents the character input stream consumed during lexical
3131 analysis.
3132
3133 In addition to consuming the input stream, this class performs some comment
3134 pre processing, by filtering out out-of-bound special text from the query
3135 input stream.
3136
3137 Two buffers, with pointers inside each, are maintained in parallel. The
3138 'raw' buffer is the original query text, which may contain out-of-bound
3139 comments. The 'cpp' (for comments pre processor) is the pre-processed buffer
3140 that contains only the query text that should be seen once out-of-bound data
3141 is removed.
3142*/
3143
3144class Lex_input_stream {
3145 public:
3146 /**
3147 Constructor
3149 @param grammar_selector_token_arg See grammar_selector_token.
3150 */
3151
3152 explicit Lex_input_stream(uint grammar_selector_token_arg)
3153 : grammar_selector_token(grammar_selector_token_arg) {}
3154
3155 /**
3156 Object initializer. Must be called before usage.
3157
3158 @retval false OK
3159 @retval true Error
3160 */
3161 bool init(THD *thd, const char *buff, size_t length);
3162
3163 void reset(const char *buff, size_t length);
3164
3165 /**
3166 Set the echo mode.
3167
3168 When echo is true, characters parsed from the raw input stream are
3169 preserved. When false, characters parsed are silently ignored.
3170 @param echo the echo mode.
3171 */
3172 void set_echo(bool echo) { m_echo = echo; }
3173
3174 void save_in_comment_state() {
3177 }
3178
3182 }
3183
3184 /**
3185 Skip binary from the input stream.
3186 @param n number of bytes to accept.
3187 */
3188 void skip_binary(int n) {
3189 assert(m_ptr + n <= m_end_of_query);
3190 if (m_echo) {
3191 memcpy(m_cpp_ptr, m_ptr, n);
3192 m_cpp_ptr += n;
3193 }
3194 m_ptr += n;
3195 }
3196
3197 /**
3198 Get a character, and advance in the stream.
3199 @return the next character to parse.
3200 */
3201 unsigned char yyGet() {
3202 assert(m_ptr <= m_end_of_query);
3203 char c = *m_ptr++;
3204 if (m_echo) *m_cpp_ptr++ = c;
3205 return c;
3206 }
3207
3208 /**
3209 Get the last character accepted.
3210 @return the last character accepted.
3211 */
3212 unsigned char yyGetLast() const { return m_ptr[-1]; }
3214 /**
3215 Look at the next character to parse, but do not accept it.
3216 */
3217 unsigned char yyPeek() const {
3218 assert(m_ptr <= m_end_of_query);
3219 return m_ptr[0];
3220 }
3221
3222 /**
3223 Look ahead at some character to parse.
3224 @param n offset of the character to look up
3225 */
3226 unsigned char yyPeekn(int n) const {
3227 assert(m_ptr + n <= m_end_of_query);
3228 return m_ptr[n];
3229 }
3230
3231 /**
3232 Cancel the effect of the last yyGet() or yySkip().
3233 Note that the echo mode should not change between calls to yyGet / yySkip
3234 and yyUnget. The caller is responsible for ensuring that.
3235 */
3236 void yyUnget() {
3237 m_ptr--;
3238 if (m_echo) m_cpp_ptr--;
3239 }
3241 /**
3242 Accept a character, by advancing the input stream.
3243 */
3244 void yySkip() {
3245 assert(m_ptr <= m_end_of_query);
3246 if (m_echo)
3247 *m_cpp_ptr++ = *m_ptr++;
3248 else
3249 m_ptr++;
3250 }
3251
3252 /**
3253 Accept multiple characters at once.
3254 @param n the number of characters to accept.
3255 */
3256 void yySkipn(int n) {
3257 assert(m_ptr + n <= m_end_of_query);
3258 if (m_echo) {
3259 memcpy(m_cpp_ptr, m_ptr, n);
3260 m_cpp_ptr += n;
3261 }
3262 m_ptr += n;
3263 }
3264
3265 /**
3266 Puts a character back into the stream, canceling
3267 the effect of the last yyGet() or yySkip().
3268 Note that the echo mode should not change between calls
3269 to unput, get, or skip from the stream.
3270 */
3271 char *yyUnput(char ch) {
3272 *--m_ptr = ch;
3273 if (m_echo) m_cpp_ptr--;
3274 return m_ptr;
3275 }
3276
3277 /**
3278 Inject a character into the pre-processed stream.
3279
3280 Note, this function is used to inject a space instead of multi-character
3281 C-comment. Thus there is no boundary checks here (basically, we replace
3282 N-chars by 1-char here).
3283 */
3284 char *cpp_inject(char ch) {
3285 *m_cpp_ptr = ch;
3286 return ++m_cpp_ptr;
3287 }
3288
3289 /**
3290 End of file indicator for the query text to parse.
3291 @return true if there are no more characters to parse
3292 */
3293 bool eof() const { return (m_ptr >= m_end_of_query); }
3294
3295 /**
3296 End of file indicator for the query text to parse.
3297 @param n number of characters expected
3298 @return true if there are less than n characters to parse
3300 bool eof(int n) const { return ((m_ptr + n) >= m_end_of_query); }
3301
3302 /** Get the raw query buffer. */
3303 const char *get_buf() const { return m_buf; }
3304
3305 /** Get the pre-processed query buffer. */
3306 const char *get_cpp_buf() const { return m_cpp_buf; }
3307
3308 /** Get the end of the raw query buffer. */
3309 const char *get_end_of_query() const { return m_end_of_query; }
3310
3311 /** Mark the stream position as the start of a new token. */
3312 void start_token() {
3314 m_tok_end = m_ptr;
3315
3318 }
3319
3320 /**
3321 Adjust the starting position of the current token.
3322 This is used to compensate for starting whitespace.
3323 */
3324 void restart_token() {
3327 }
3328
3329 /** Get the token start position, in the raw buffer. */
3330 const char *get_tok_start() const { return m_tok_start; }
3331
3332 /** Get the token start position, in the pre-processed buffer. */
3333 const char *get_cpp_tok_start() const { return m_cpp_tok_start; }
3334
3335 /** Get the token end position, in the raw buffer. */
3336 const char *get_tok_end() const { return m_tok_end; }
3337
3338 /** Get the token end position, in the pre-processed buffer. */
3339 const char *get_cpp_tok_end() const { return m_cpp_tok_end; }
3340
3341 /** Get the current stream pointer, in the raw buffer. */
3342 const char *get_ptr() const { return m_ptr; }
3343
3344 /** Get the current stream pointer, in the pre-processed buffer. */
3345 const char *get_cpp_ptr() const { return m_cpp_ptr; }
3346
3347 /** Get the length of the current token, in the raw buffer. */
3348 uint yyLength() const {
3349 /*
3350 The assumption is that the lexical analyser is always 1 character ahead,
3351 which the -1 account for.
3352 */
3353 assert(m_ptr > m_tok_start);
3354 return (uint)((m_ptr - m_tok_start) - 1);
3355 }
3356
3357 /** Get the utf8-body string. */
3358 const char *get_body_utf8_str() const { return m_body_utf8; }
3359
3360 /** Get the utf8-body length. */
3361 uint get_body_utf8_length() const {
3362 return (uint)(m_body_utf8_ptr - m_body_utf8);
3363 }
3364
3365 void body_utf8_start(THD *thd, const char *begin_ptr);
3366 void body_utf8_append(const char *ptr);
3367 void body_utf8_append(const char *ptr, const char *end_ptr);
3368 void body_utf8_append_literal(THD *thd, const LEX_STRING *txt,
3369 const CHARSET_INFO *txt_cs,
3370 const char *end_ptr);
3372 uint get_lineno(const char *raw_ptr) const;
3373
3374 /** Current thread. */
3375 THD *m_thd;
3376
3377 /** Current line number. */
3378 uint yylineno;
3379
3380 /** Length of the last token parsed. */
3381 uint yytoklen;
3382
3383 /** Interface with bison, value of the last token parsed. */
3385
3386 /**
3387 LALR(2) resolution, look ahead token.
3388 Value of the next token to return, if any,
3389 or -1, if no token was parsed in advance.
3390 Note: 0 is a legal token, and represents YYEOF.
3392 int lookahead_token;
3393
3394 /** LALR(2) resolution, value of the look ahead token.*/
3396
3397 /// Skip adding of the current token's digest since it is already added
3398 ///
3399 /// Usually we calculate a digest token by token at the top-level function
3400 /// of the lexer: MYSQLlex(). However, some complex ("hintable") tokens break
3401 /// that data flow: for example, the `SELECT /*+ HINT(t) */` is the single
3402 /// token from the main parser's point of view, and we add the "SELECT"
3403 /// keyword to the digest buffer right after the lex_one_token() call,
3404 /// but the "/*+ HINT(t) */" is a sequence of separate tokens from the hint
3405 /// parser's point of view, and we add those tokens to the digest buffer
3406 /// *inside* the lex_one_token() call. Thus, the usual data flow adds
3407 /// tokens from the "/*+ HINT(t) */" string first, and only than it appends
3408 /// the "SELECT" keyword token to that stream: "/*+ HINT(t) */ SELECT".
3409 /// This is not acceptable, since we use the digest buffer to restore
3410 /// query strings in their normalized forms, so the order of added tokens is
3411 /// important. Thus, we add tokens of "hintable" keywords to a digest buffer
3412 /// right in the hint parser and skip adding of them at the caller with the
3413 /// help of skip_digest flag.
3414 bool skip_digest;
3415
3417
3418 void reduce_digest_token(uint token_left, uint token_right);
3419
3420 /**
3421 True if this scanner tokenizes a partial query (partition expression,
3422 generated column expression etc.)
3423
3424 @return true if parsing a partial query, otherwise false.
3425 */
3426 bool is_partial_parser() const { return grammar_selector_token >= 0; }
3427
3428 /**
3429 Outputs warnings on deprecated charsets in complete SQL statements
3431 @param [in] cs The character set/collation to check for a deprecation.
3432 @param [in] alias The name/alias of @p cs.
3433 */
3435 const char *alias) const {
3436 if (!is_partial_parser()) {
3438 }
3439 }
3440
3441 /**
3442 Outputs warnings on deprecated collations in complete SQL statements
3443
3444 @param [in] collation The collation to check for a deprecation.
3445 */
3447 if (!is_partial_parser()) {
3449 }
3450 }
3451
3453
3454 private:
3455 /** Pointer to the current position in the raw input stream. */
3456 char *m_ptr;
3457
3458 /** Starting position of the last token parsed, in the raw buffer. */
3459 const char *m_tok_start;
3460
3461 /** Ending position of the previous token parsed, in the raw buffer. */
3462 const char *m_tok_end;
3463
3464 /** End of the query text in the input stream, in the raw buffer. */
3465 const char *m_end_of_query;
3466
3467 /** Begining of the query text in the input stream, in the raw buffer. */
3468 const char *m_buf;
3469
3470 /** Length of the raw buffer. */
3472
3473 /** Echo the parsed stream to the pre-processed buffer. */
3475 bool m_echo_saved;
3476
3477 /** Pre-processed buffer. */
3478 char *m_cpp_buf;
3479
3480 /** Pointer to the current position in the pre-processed input stream. */
3481 char *m_cpp_ptr;
3482
3483 /**
3484 Starting position of the last token parsed,
3485 in the pre-processed buffer.
3486 */
3487 const char *m_cpp_tok_start;
3488
3489 /**
3490 Ending position of the previous token parsed,
3491 in the pre-processed buffer.
3493 const char *m_cpp_tok_end;
3494
3495 /** UTF8-body buffer created during parsing. */
3496 char *m_body_utf8;
3497
3498 /** Pointer to the current position in the UTF8-body buffer. */
3499 char *m_body_utf8_ptr;
3500
3501 /**
3502 Position in the pre-processed buffer. The query from m_cpp_buf to
3503 m_cpp_utf_processed_ptr is converted to UTF8-body.
3504 */
3506
3507 public:
3508 /** Current state of the lexical analyser. */
3510
3511 /**
3512 Position of ';' in the stream, to delimit multiple queries.
3513 This delimiter is in the raw buffer.
3515 const char *found_semicolon;
3516
3517 /** Token character bitmaps, to detect 7bit strings. */
3519
3520 /** SQL_MODE = IGNORE_SPACE. */
3521 bool ignore_space;
3522
3523 /**
3524 true if we're parsing a prepared statement: in this mode
3525 we should allow placeholders.
3526 */
3528 /**
3529 true if we should allow multi-statements.
3532
3533 /** State of the lexical analyser for comments. */
3536
3537 /**
3538 Starting position of the TEXT_STRING or IDENT in the pre-processed
3539 buffer.
3540
3541 NOTE: this member must be used within MYSQLlex() function only.
3542 */
3543 const char *m_cpp_text_start;
3544
3545 /**
3546 Ending position of the TEXT_STRING or IDENT in the pre-processed
3547 buffer.
3548
3549 NOTE: this member must be used within MYSQLlex() function only.
3550 */
3551 const char *m_cpp_text_end;
3552
3553 /**
3554 Character set specified by the character-set-introducer.
3555
3556 NOTE: this member must be used within MYSQLlex() function only.
3557 */
3560 /**
3561 Current statement digest instrumentation.
3562 */
3564
3565 /**
3566 The synthetic 1st token to prepend token stream with.
3567
3568 This token value tricks parser to simulate multiple %start-ing points.
3569 Currently the grammar is aware of 4 such synthetic tokens:
3570 1. GRAMMAR_SELECTOR_PART for partitioning stuff from DD,
3571 2. GRAMMAR_SELECTOR_GCOL for generated column stuff from DD,
3572 3. GRAMMAR_SELECTOR_EXPR for generic single expressions from DD/.frm.
3573 4. GRAMMAR_SELECTOR_CTE for generic subquery expressions from CTEs.
3574 5. -1 when parsing with the main grammar (no grammar selector available).
3575
3576 @note yylex() is expected to return the value of type int:
3577 0 is for EOF and everything else for real token numbers.
3578 Bison, in its turn, generates positive token numbers.
3579 So, the negative grammar_selector_token means "not a token".
3580 In other words, -1 is "empty value".
3581 */
3582 const int grammar_selector_token;
3584 bool text_string_is_7bit() const { return !(tok_bitmap & 0x80); }
3588 public:
3589 String column;
3590 uint rights;
3591 LEX_COLUMN(const String &x, const uint &y) : column(x), rights(y) {}
3592};
3593
3594enum class role_enum;
3596/*
3597 This structure holds information about grantor's context
3598*/
3599class LEX_GRANT_AS {
3600 public:
3602 void cleanup();
3604 public:
3605 bool grant_as_used;
3607 LEX_USER *user;
3609};
3610
3611/**
3612 The LEX object currently serves three different purposes:
3613
3614 - It contains some universal properties of an SQL command, such as
3615 sql_command, presence of IGNORE in data change statement syntax, and list
3616 of tables (query_tables).
3617
3618 - It contains some execution state variables, like m_exec_started
3619 (set to true when execution is started), plugins (list of plugins used
3620 by statement), insert_update_values_map (a map of objects used by certain
3621 INSERT statements), etc.
3622
3623 - It contains a number of members that should be local to subclasses of
3624 Sql_cmd, like purge_value_list (for the PURGE command), kill_value_list
3625 (for the KILL command).
3626
3627 The LEX object is strictly a part of class Sql_cmd, for those SQL commands
3628 that are represented by an Sql_cmd class. For the remaining SQL commands,
3629 it is a standalone object linked to the current THD.
3630
3631 The lifecycle of a LEX object is as follows:
3632
3633 - The LEX object is constructed either on the execution mem_root
3634 (for regular statements), on a Prepared_statement mem_root (for
3635 prepared statements), on an SP mem_root (for stored procedure instructions),
3636 or created on the current mem_root for short-lived uses.
3637
3638 - Call lex_start() to initialize a LEX object before use.
3639 This initializes the execution state part of the object.
3640 It also calls LEX::reset() to ensure that all members are properly inited.
3641
3642 - Parse and resolve the statement, using the LEX as a work area.
3643
3644 - Execute an SQL command: call set_exec_started() when starting to execute
3645 (actually when starting to optimize).
3646 Typically call is_exec_started() to distinguish between preparation
3647 and optimization/execution stages of SQL command execution.
3648
3649 - Call clear_execution() when execution is finished. This will clear all
3650 execution state associated with the SQL command, it also includes calling
3651 LEX::reset_exec_started().
3652
3653 @todo - Create subclasses of Sql_cmd to contain data that are local
3654 to specific commands.
3655
3656 @todo - Create a Statement context object that will hold the execution state
3657 part of struct LEX.
3658
3659 @todo - Ensure that a LEX struct is never reused, thus making e.g
3660 LEX::reset() redundant.
3661*/
3663struct LEX : public Query_tables_list {
3664 friend bool lex_start(THD *thd);
3666 Query_expression *unit; ///< Outer-most query expression
3667 /// @todo: query_block can be replaced with unit->first-select()
3668 Query_block *query_block; ///< First query block
3669 Query_block *all_query_blocks_list; ///< List of all query blocks
3670 private:
3671 /* current Query_block in parsing */
3673
3674 public:
3675 inline Query_block *current_query_block() const {
3676 return m_current_query_block;
3677 }
3678
3679 /*
3680 We want to keep current_thd out of header files, so the debug assert
3681 is moved to the .cc file.
3682 */
3684 inline void set_current_query_block(Query_block *select) {
3685#ifndef NDEBUG
3687#endif
3689 }
3690 /// @return true if this is an EXPLAIN statement
3691 bool is_explain() const { return explain_format != nullptr; }
3692 bool is_explain_analyze = false;
3693 /**
3694 Whether the currently-running query should be (attempted) executed in
3695 the hypergraph optimizer. This will not change after the query is
3696 done parsing, so you can use it in any query phase to e.g. figure out
3697 whether to inhibit some transformation that the hypergraph optimizer
3698 does not properly understand yet.
3703 char *to_log; /* For PURGE MASTER LOGS TO */
3705 // Widcard from SHOW ... LIKE <wildcard> statements.
3709 nullptr, 0}; ///< Argument of the BINLOG event statement.
3716 THD *thd;
3717
3718 /* Optimizer hints */
3721 /* maintain a list of used plugins for this LEX */
3726 /// Table being inserted into (may be a view)
3728 /// Leaf table being inserted into (always a base table)
3730
3731 /** SELECT of CREATE VIEW statement */
3733
3734 /* Partition info structure filled in by PARTITION BY parse part */
3736
3738 The definer of the object being created (view, trigger, stored routine).
3739 I.e. the value of DEFINER clause.
3749
3750 // PURGE statement-specific fields:
3752
3753 // KILL statement-specific fields:
3755
3756 // other stuff:
3758 List<Item_func_set_user_var> set_var_list; // in-query assignment list
3759 /**
3760 List of placeholders ('?') for parameters of a prepared statement. Because
3761 we append to this list during parsing, it is naturally sorted by
3762 position of the '?' in the query string. The code which fills placeholders
3763 with user-supplied values, and the code which writes a query for
3764 statement-based logging, rely on this order.
3765 This list contains only real placeholders, not the clones which originate
3766 in a re-parsed CTE definition.
3767 */
3769
3771
3772 void insert_values_map(Item_field *f1, Field *f2) {
3774 insert_update_values_map = new std::map<Item_field *, Field *>;
3775 insert_update_values_map->insert(std::make_pair(f1, f2));
3776 }
3777 void destroy_values_map() {
3779 insert_update_values_map->clear();
3781 insert_update_values_map = nullptr;
3782 }
3783 }
3784 void clear_values_map() {
3787 }
3788 }
3789 bool has_values_map() const { return insert_update_values_map != nullptr; }
3790 std::map<Item_field *, Field *>::iterator begin_values_map() {
3791 return insert_update_values_map->begin();
3792 }
3793 std::map<Item_field *, Field *>::iterator end_values_map() {
3794 return insert_update_values_map->end();
3795 }
3796
3797 private:
3798 /*
3799 With Visual Studio, an std::map will always allocate two small objects
3800 on the heap. Sometimes we put LEX objects in a MEM_ROOT, and never run
3801 the LEX DTOR. To avoid memory leaks, put this std::map on the heap,
3802 and call clear_values_map() at the end of each statement.
3803 */
3804 std::map<Item_field *, Field *> *insert_update_values_map;
3805
3806 public:
3807 /*
3808 A stack of name resolution contexts for the query. This stack is used
3809 at parse time to set local name resolution contexts for various parts
3810 of a query. For example, in a JOIN ... ON (some_condition) clause the
3811 Items in 'some_condition' must be resolved only against the operands
3812 of the the join, and not against the whole clause. Similarly, Items in
3813 subqueries should be resolved against the subqueries (and outer queries).
3814 The stack is used in the following way: when the parser detects that
3815 all Items in some clause need a local context, it creates a new context
3816 and pushes it on the stack. All newly created Items always store the
3817 top-most context in the stack. Once the parser leaves the clause that
3818 required a local context, the parser pops the top-most context.
3824 HA_CHECK_OPT check_opt; // check/repair options
3827 LEX_MASTER_INFO mi; // used by CHANGE MASTER
3832 ulong type;
3833 /**
3834 This field is used as a work field during resolving to validate
3835 the use of aggregate functions. For example in a query
3836 SELECT ... FROM ...WHERE MIN(i) == 1 GROUP BY ... HAVING MIN(i) > 2
3837 MIN(i) in the WHERE clause is not allowed since only non-aggregated data
3838 is present, whereas MIN(i) in the HAVING clause is allowed because HAVING
3839 operates on the output of a grouping operation.
3840 Each query block is assigned a nesting level. This field is a bit field
3841 that contains the value one in the position of that nesting level if
3842 aggregate functions are allowed for that query block.
3843 */
3845 /**
3846 Windowing functions are not allowed in HAVING - in contrast to group
3847 aggregates - then we need to be stricter than allow_sum_func.
3848 One bit per query block, as allow_sum_func.
3849 */
3852 /// If true: during prepare, we did a subquery transformation (IN-to-EXISTS,
3853 /// SOME/ANY) that doesn't currently work for subquery to a derived table
3854 /// transformation.
3856
3858
3859 /*
3860 Usually `expr` rule of yacc is quite reused but some commands better
3861 not support subqueries which comes standard with this rule, like
3862 KILL, HA_READ, CREATE/ALTER EVENT etc. Set this to `false` to get
3863 syntax error back.
3864 */
3866 /**
3867 If currently re-parsing a CTE's definition, this is the offset in bytes
3868 of that definition in the original statement which had the WITH
3869 clause. Otherwise this is 0.
3870 */
3872 /**
3873 If currently re-parsing a condition which is pushed down to a derived
3874 table, this will be set to true.
3875 */
3877 /**
3878 If currently re-parsing a condition that is being pushed down to a
3879 derived table, this has the positions of all the parameters that are
3880 part of that condition in the original statement. Otherwise it is empty.
3884 enum SSL_type ssl_type; /* defined in violite.h */
3890 /// QUERY ID for SHOW PROFILE
3894 /**
3895 Set to true when GRANT ... GRANT OPTION ... TO ...
3896 is used (vs. GRANT ... WITH GRANT OPTION).
3897 The flag is used by @ref mysql_grant to grant GRANT OPTION (@ref GRANT_ACL)
3898 to all dynamic privileges.
3902 int select_number; ///< Number of query block (by EXPLAIN)
3905 /**
3906 @todo ensure that correct CONTEXT_ANALYSIS_ONLY is set for all preparation
3907 code, so we can fully rely on this field.
3910 bool drop_if_exists;
3912 bool autocommit;
3914 // For show commands to show hidden columns and indexes.
3915 bool m_extended_show;
3916
3917 enum enum_yes_no_unknown tx_chain, tx_release;
3918
3919 /**
3920 Whether this query will return the same answer every time, given unchanged
3921 data. Used to be for the query cache, but is now used to find out if an
3922 expression is usable for partitioning.
3923 */
3926 private:
3927 /// True if statement references UDF functions
3928 bool m_has_udf{false};
3931 public:
3932 bool is_ignore() const { return ignore; }
3933 void set_ignore(bool ignore_param) { ignore = ignore_param; }
3934 void set_has_udf() { m_has_udf = true; }
3935 bool has_udf() const { return m_has_udf; }
3938 /* Prepared statements SQL syntax:*/
3939 LEX_CSTRING prepared_stmt_name; /* Statement name (in all queries) */
3941 Prepared statement query text or name of variable that holds the
3942 prepared statement (in PREPARE ... queries)
3943 */
3945 /* If true, prepared_stmt_code is a name of variable that holds the query */
3947 /* Names of user variables holding parameters (in EXECUTE) */
3951 bool sp_lex_in_use; /* Keep track on lex usage in SPs for error handling */
3952 bool all_privileges;
3956
3957 private:
3958 bool m_broken; ///< see mark_broken()
3959 /**
3960 Set to true when execution has started (after parsing, tables opened and
3961 query preparation is complete. Used to track arena state for SPs).
3962 */
3963 bool m_exec_started;
3964 /**
3965 Set to true when execution is completed, ie optimization has been done
3966 and execution is successful or ended in error.
3967 */
3968 bool m_exec_completed;
3969 /**
3970 Current SP parsing context.
3971 @see also sp_head::m_root_parsing_ctx.
3972 */
3975 /**
3976 Statement context for Query_block::make_active_options.
3977 */
3979
3980 public:
3981 /**
3982 Gets the options that have been set for this statement. The options are
3983 propagated to the Query_block objects and should usually be read with
3984 #Query_block::active_options().
3985
3986 @return a bit set of options set for this statement
3987 */
3989 /**
3990 Add options to values of m_statement_options. options is an ORed
3991 bit set of options defined in query_options.h
3993 @param options Add this set of options to the set already in
3994 m_statement_options
3998 }
3999 bool is_broken() const { return m_broken; }
4000 /**
4001 Certain permanent transformations (like in2exists), if they fail, may
4002 leave the LEX in an inconsistent state. They should call the
4003 following function, so that this LEX is not reused by another execution.
4005 @todo If lex_start () were a member function of LEX, the "broken"
4006 argument could always be "true" and thus could be removed.
4007 */
4008 void mark_broken(bool broken = true) {
4009 if (broken) {
4010 /*
4011 "OPEN <cursor>" cannot be re-prepared if the cursor uses no tables
4012 ("SELECT FROM DUAL"). Indeed in that case cursor_query is left empty
4013 in constructions of sp_instr_cpush, and thus
4014 sp_lex_instr::parse_expr() cannot re-prepare. So we mark the statement
4015 as broken only if tables are used.
4016 */
4017 if (is_metadata_used()) m_broken = true;
4018 } else
4019 m_broken = false;
4021
4023
4024 void cleanup(THD *thd, bool full) {
4025 unit->cleanup(thd, full);
4026 if (full) {
4031
4032 bool is_exec_started() const { return m_exec_started; }
4033 void set_exec_started() { m_exec_started = true; }
4034 void reset_exec_started() {
4035 m_exec_started = false;
4036 m_exec_completed = false;
4037 }
4038 /**
4039 Check whether the statement has been executed (regardless of completion -
4040 successful or in error).
4041 Check this instead of Query_expression::is_executed() to determine
4042 the state of a complete statement.
4043 */
4044 bool is_exec_completed() const { return m_exec_completed; }
4045 void set_exec_completed() { m_exec_completed = true; }
4047
4051
4052 /// Check if the current statement uses meta-data (uses a table or a stored
4053 /// routine).
4054 bool is_metadata_used() const {
4055 return query_tables != nullptr || has_udf() ||
4056 (sroutines != nullptr && !sroutines->empty());
4057 }
4059 public:
4061
4063
4064 bool only_view; /* used for SHOW CREATE TABLE/VIEW */
4065 /*
4066 view created to be run from definer (standard behaviour)
4067 */
4069
4070 /**
4071 Intended to point to the next word after DEFINER-clause in the
4072 following statements:
4073
4074 - CREATE TRIGGER (points to "TRIGGER");
4075 - CREATE PROCEDURE (points to "PROCEDURE");
4076 - CREATE FUNCTION (points to "FUNCTION" or "AGGREGATE");
4077 - CREATE EVENT (points to "EVENT")
4079 This pointer is required to add possibly omitted DEFINER-clause to the
4080 DDL-statement before dumping it to the binlog.
4081 */
4082 const char *stmt_definition_begin;
4083 const char *stmt_definition_end;
4084
4085 /**
4086 During name resolution search only in the table list given by
4087 Name_resolution_context::first_name_resolution_table and
4088 Name_resolution_context::last_name_resolution_table
4089 (see Item_field::fix_fields()).
4090 */
4092
4093 bool is_lex_started; /* If lex_start() did run. For debugging. */
4094 /// Set to true while resolving values in ON DUPLICATE KEY UPDATE clause
4098
4099 // Maximum execution time for a statement.
4100 ulong max_execution_time;
4101
4103 To flag the current statement as dependent for binary logging
4104 on explicit_defaults_for_timestamp
4105 */
4107
4108 /**
4109 Used to inform the parser whether it should contextualize the parse
4110 tree. When we get a pure parser this will not be needed.
4111 */
4112 bool will_contextualize;
4113
4114 LEX();
4116 virtual ~LEX();
4117
4118 /// Destroy contained objects, but not the LEX object itself.
4119 void destroy() {
4120 if (unit == nullptr) return;
4121 unit->destroy();
4122 unit = nullptr;
4123 query_block = nullptr;
4124 all_query_blocks_list = nullptr;
4125 m_current_query_block = nullptr;
4127 }
4128
4129 /// Reset query context to initial state
4130 void reset();
4131
4132 /// Create an empty query block within this LEX object.
4134
4135 /// Create query expression object that contains one query block.
4136 Query_block *new_query(Query_block *curr_query_block);
4137
4138 /// Create query block and attach it to the current query expression.
4139 Query_block *new_union_query(Query_block *curr_query_block, bool distinct);
4140
4141 /// Create top-level query expression and query block.
4142 bool new_top_level_query();
4143
4144 /// Create query expression and query block in existing memory objects.
4145 void new_static_query(Query_expression *sel_query_expression,
4146 Query_block *select);
4147
4148 /// Create query expression under current_query_block and a query block under
4149 /// the new query expression. The new query expression is linked in under
4150 /// current_query_block. The new query block is linked in under the new
4151 /// query expression.
4152 ///
4153 /// @param thd current session context
4154 /// @param current_query_block the root under which we create the new
4155 /// expression
4156 /// and block
4157 /// @param where_clause any where clause for the block
4158 /// @param having_clause any having clause for the block
4159 /// @param ctx the parsing context
4160 ///
4161 /// @returns the new query expression, or nullptr on error.
4163 THD *thd, Query_block *current_query_block, Item *where_clause,
4164 Item *having_clause, enum_parsing_context ctx);
4165
4166 inline bool is_ps_or_view_context_analysis() {
4169 }
4170
4171 inline bool is_view_context_analysis() {
4173 }
4174
4175 void clear_execution();
4176
4177 /**
4178 Set the current query as uncacheable.
4179
4180 @param curr_query_block Current select query block
4181 @param cause Why this query is uncacheable.
4182
4183 @details
4184 All query blocks representing subqueries, from the current one up to
4185 the outer-most one, but excluding the main query block, are also set
4186 as uncacheable.
4187 */
4188 void set_uncacheable(Query_block *curr_query_block, uint8 cause) {
4189 safe_to_cache_query = false;
4190
4191 if (m_current_query_block == nullptr) return;
4192 Query_block *sl;
4193 Query_expression *un;
4194 for (sl = curr_query_block, un = sl->master_query_expression(); un != unit;
4195 sl = sl->outer_query_block(), un = sl->master_query_expression()) {
4196 sl->uncacheable |= cause;
4197 un->uncacheable |= cause;
4198 }
4199 }
4201
4202 TABLE_LIST *unlink_first_table(bool *link_to_local);
4203 void link_first_table_back(TABLE_LIST *first, bool link_to_local);
4205
4207
4209 for (TABLE_LIST *tr = insert_table->first_leaf_table(); tr != nullptr;
4210 tr = tr->next_leaf)
4211 tr->restore_properties();
4212 }
4213
4215
4216 bool can_use_merged();
4217 bool can_not_use_merged();
4218 bool need_correct_ident();
4219 /*
4220 Is this update command where 'WHITH CHECK OPTION' clause is important
4221
4222 SYNOPSIS
4223 LEX::which_check_option_applicable()
4224
4225 RETURN
4226 true have to take 'WHITH CHECK OPTION' clause into account
4227 false 'WHITH CHECK OPTION' clause do not need
4228 */
4229 inline bool which_check_option_applicable() {
4230 switch (sql_command) {
4231 case SQLCOM_UPDATE:
4233 case SQLCOM_INSERT:
4235 case SQLCOM_REPLACE:
4237 case SQLCOM_LOAD:
4238 return true;
4239 default:
4240 return false;
4241 }
4243
4245
4247 return context_stack.push_front(context);
4248 }
4249
4250 void pop_context() { context_stack.pop(); }
4251
4252 bool copy_db_to(char const **p_db, size_t *p_db_length) const;
4253
4254 bool copy_db_to(char **p_db, size_t *p_db_length) const {
4255 return copy_db_to(const_cast<const char **>(p_db), p_db_length);
4256 }
4257
4259
4262
4263 bool table_or_sp_used();
4264
4265 /**
4266 @brief check if the statement is a single-level join
4267 @return result of the check
4268 @retval true The statement doesn't contain subqueries, unions and
4269 stored procedure calls.
4270 @retval false There are subqueries, UNIONs or stored procedure calls.
4271 */
4272 bool is_single_level_stmt() {
4273 /*
4274 This check exploits the fact that the last added to all_select_list is
4275 on its top. So query_block (as the first added) will be at the tail
4276 of the list.
4277 */
4279 (sroutines == nullptr || sroutines->empty())) {
4281 return true;
4282 }
4283 return false;
4284 }
4285
4286 void release_plugins();
4287
4288 /**
4289 IS schema queries read some dynamic table statistics from SE.
4290 These statistics are cached, to avoid opening of table more
4291 than once while preparing a single output record buffer.
4292 */
4295
4296 bool accept(Select_lex_visitor *visitor);
4297
4298 bool set_wild(LEX_STRING);
4299 void clear_privileges();
4300
4301 bool make_sql_cmd(Parse_tree_root *parse_tree);
4302
4303 private:
4304 /**
4305 Context object used by secondary storage engines to store query
4306 state during optimization and execution.
4307 */
4309
4310 public:
4311 /**
4312 Gets the secondary engine execution context for this statement.
4313 */
4315 const {
4317 }
4318
4319 /**
4320 Sets the secondary engine execution context for this statement.
4321 The old context object is destroyed, if there is one. Can be set
4322 to nullptr to destroy the old context object and clear the
4323 pointer.
4324
4325 The supplied context object should be allocated on the execution
4326 MEM_ROOT, so that its memory doesn't have to be manually freed
4327 after query execution.
4328 */
4331
4332 private:
4334
4335 public:
4338 }
4339
4342 }
4343
4344 private:
4346
4347 public:
4350 }
4351
4354 }
4357
4358 private:
4359 bool rewrite_required{false};
4361 public:
4362 void set_rewrite_required() { rewrite_required = true; }
4363 void reset_rewrite_required() { rewrite_required = false; }
4364 bool is_rewrite_required() { return rewrite_required; }
4365};
4366
4368 RAII class to ease the call of LEX::mark_broken() if error.
4369 Used during preparation and optimization of DML queries.
4370*/
4372 public:
4373 Prepare_error_tracker(THD *thd_arg) : thd(thd_arg) {}
4375
4376 private:
4377 THD *const thd;
4378};
4379
4380/**
4381 The internal state of the syntax parser.
4382 This object is only available during parsing,
4383 and is private to the syntax parser implementation (sql_yacc.yy).
4384*/
4385class Yacc_state {
4386 public:
4388 reset();
4389 }
4390
4391 void reset() {
4392 if (yacc_yyss != nullptr) {
4394 yacc_yyss = nullptr;
4395 }
4396 if (yacc_yyvs != nullptr) {
4398 yacc_yyvs = nullptr;
4399 }
4400 if (yacc_yyls != nullptr) {
4402 yacc_yyls = nullptr;
4403 }
4406 }
4407
4408 ~Yacc_state();
4409
4410 /**
4411 Reset part of the state which needs resetting before parsing
4412 substatement.
4413 */
4417 }
4418
4419 /**
4420 Bison internal state stack, yyss, when dynamically allocated using
4421 my_yyoverflow().
4422 */
4424
4425 /**
4426 Bison internal semantic value stack, yyvs, when dynamically allocated using
4427 my_yyoverflow().
4428 */
4430
4431 /**
4432 Bison internal location value stack, yyls, when dynamically allocated using
4433 my_yyoverflow().
4434 */
4436
4437 /**
4438 Type of lock to be used for tables being added to the statement's
4439 table list in table_factor, table_alias_ref, single_multi and
4440 table_wild_one rules.
4441 Statements which use these rules but require lock type different
4442 from one specified by this member have to override it by using
4443 Query_block::set_lock_for_tables() method.
4444
4445 The default value of this member is TL_READ_DEFAULT. The only two
4446 cases in which we change it are:
4447 - When parsing SELECT HIGH_PRIORITY.
4448 - Rule for DELETE. In which we use this member to pass information
4449 about type of lock from delete to single_multi part of rule.
4451 We should try to avoid introducing new use cases as we would like
4452 to get rid of this member eventually.
4453 */
4455
4456 /**
4457 The type of requested metadata lock for tables added to
4458 the statement table list.
4459 */
4461
4462 /*
4463 TODO: move more attributes from the LEX structure here.
4464 */
4465};
4467/**
4468 Input parameters to the parser.
4469*/
4470struct Parser_input {
4471 /**
4472 True if the text parsed corresponds to an actual query,
4473 and not another text artifact.
4474 This flag is used to disable digest parsing of nested:
4475 - view definitions
4476 - table trigger definitions
4477 - table partition definitions
4478 - event scheduler event definitions
4479 */
4480 bool m_has_digest;
4481 /**
4482 True if the caller needs to compute a digest.
4483 This flag is used to request explicitly a digest computation,
4484 independently of the performance schema configuration.
4485 */
4486 bool m_compute_digest;
4487
4488 Parser_input() : m_has_digest(false), m_compute_digest(false) {}
4489};
4490
4491/**
4492 Internal state of the parser.
4493 The complete state consist of:
4494 - input parameters that control the parser behavior
4495 - state data used during lexical parsing,
4496 - state data used during syntactic parsing.
4497*/
4498class Parser_state {
4499 protected:
4500 /**
4501 Constructor for special parsers of partial SQL clauses (DD)
4502
4503 @param grammar_selector_token See Lex_input_stream::grammar_selector_token
4504 */
4505 explicit Parser_state(int grammar_selector_token)
4506 : m_input(), m_lip(grammar_selector_token), m_yacc(), m_comment(false) {}
4507
4508 public:
4509 Parser_state() : m_input(), m_lip(~0U), m_yacc(), m_comment(false) {}
4510
4511 /**
4512 Object initializer. Must be called before usage.
4514 @retval false OK
4515 @retval true Error
4516 */
4517 bool init(THD *thd, const char *buff, size_t length) {
4518 return m_lip.init(thd, buff, length);
4519 }
4520
4521 void reset(const char *found_semicolon, size_t length) {
4522 m_lip.reset(found_semicolon, length);
4524 }
4526 /// Signal that the current query has a comment
4527 void add_comment() { m_comment = true; }
4528 /// Check whether the current query has a comment
4529 bool has_comment() const { return m_comment; }
4531 public:
4535 /**
4536 Current performance digest instrumentation.
4539
4540 private:
4541 bool m_comment; ///< True if current query contains comments
4542};
4544/**
4545 Parser state for partition expression parser (.frm/DD stuff)
4546*/
4548 public:
4550
4552};
4554/**
4555 Parser state for generated column expression parser (.frm/DD stuff)
4556*/
4558 public:
4560
4562};
4564/**
4565 Parser state for single expression parser (.frm/DD stuff)
4566*/
4568 public:
4570
4571 Item *result;
4572};
4574/**
4575 Parser state for CTE subquery parser
4576*/
4578 public:
4580
4582};
4583
4585 Parser state for Derived table's condition parser.
4586 (Used in condition pushdown to derived tables)
4587*/
4589 public:
4593};
4594
4595struct st_lex_local : public LEX {
4596 static void *operator new(size_t size) noexcept {
4597 return (*THR_MALLOC)->Alloc(size);
4598 }
4599 static void *operator new(size_t size, MEM_ROOT *mem_root,
4600 const std::nothrow_t &arg
4601 [[maybe_unused]] = std::nothrow) noexcept {
4602 return mem_root->Alloc(size);
4603 }
4604 static void operator delete(void *ptr [[maybe_unused]],
4605 size_t size [[maybe_unused]]) {
4606 TRASH(ptr, size);
4607 }
4608 static void operator delete(
4609 void *, MEM_ROOT *, const std::nothrow_t &) noexcept { /* Never called */
4610 }
4611};
4612
4613extern bool lex_init(void);
4614extern void lex_free(void);
4615extern bool lex_start(THD *thd);
4616extern void lex_end(LEX *lex);
4617extern int MYSQLlex(union YYSTYPE *, struct YYLTYPE *, class THD *);
4618
4619extern void trim_whitespace(const CHARSET_INFO *cs, LEX_STRING *str);
4620
4621extern bool is_lex_native_function(const LEX_STRING *name);
4623bool is_keyword(const char *name, size_t len);
4624bool db_is_default_db(const char *db, size_t db_len, const THD *thd);
4625
4627
4628void print_derived_column_names(const THD *thd, String *str,
4630
4631/**
4632 @} (End of group GROUP_PARSER)
4633*/
4634
4635/**
4636 Check if the given string is invalid using the system charset.
4637
4638 @param string_val Reference to the string.
4639 @param charset_info Pointer to charset info.
4640
4641 @return true if the string has an invalid encoding using
4642 the system charset else false.
4643*/
4644
4645inline bool is_invalid_string(const LEX_CSTRING &string_val,
4646 const CHARSET_INFO *charset_info) {
4647 size_t valid_len;
4648 bool len_error;
4649
4650 if (validate_string(charset_info, string_val.str, string_val.length,
4651 &valid_len, &len_error)) {
4652 char hexbuf[7];
4653 octet2hex(
4654 hexbuf, string_val.str + valid_len,
4655 static_cast<uint>(std::min<size_t>(string_val.length - valid_len, 3)));
4656 my_error(ER_INVALID_CHARACTER_STRING, MYF(0), charset_info->csname, hexbuf);
4657 return true;
4658 }
4659 return false;
4660}
4661
4662/**
4663 Check if the given string is invalid using the system charset.
4664
4665 @param string_val Reference to the string.
4666 @param charset_info Pointer to charset info.
4667 @param[out] invalid_sub_str If string has an invalid encoding then invalid
4668 string in printable ASCII format is stored.
4669
4670 @return true if the string has an invalid encoding using
4671 the system charset else false.
4672*/
4673
4674inline bool is_invalid_string(const LEX_CSTRING &string_val,
4676 std::string &invalid_sub_str) {
4677 size_t valid_len;
4678 bool len_error;
4679
4680 if (validate_string(charset_info, string_val.str, string_val.length,
4681 &valid_len, &len_error)) {
4682 char printable_buff[32];
4684 printable_buff, sizeof(printable_buff), string_val.str + valid_len,
4685 static_cast<uint>(std::min<size_t>(string_val.length - valid_len, 3)),
4686 charset_info, 3);
4687 invalid_sub_str = printable_buff;
4688 return true;
4689 }
4690 return false;
4691}
4692
4693/**
4694 In debug mode, verify that we're not adding an item twice to the fields list
4695 with inconsistent hidden flags. Must be called before adding the item to
4696 fields.
4697 */
4699 [[maybe_unused]],
4700 Item *item [[maybe_unused]],
4701 bool hidden [[maybe_unused]]) {
4702#ifndef NDEBUG
4703 if (std::find(fields.begin(), fields.end(), item) != fields.end()) {
4704 // The item is already in the list, so we can't add it
4705 // with a different value for hidden.
4706 assert(item->hidden == hidden);
4707 }
4708#endif
4709}
4710
4711bool walk_item(Item *item, Select_lex_visitor *visitor);
4713bool accept_table(TABLE_LIST *t, Select_lex_visitor *visitor);
4715 Select_lex_visitor *visitor);
4716TABLE_LIST *nest_join(THD *thd, Query_block *select, TABLE_LIST *embedding,
4717 mem_root_deque<TABLE_LIST *> *jlist, size_t table_cnt,
4718 const char *legend);
4719#endif /* SQL_LEX_INCLUDED */
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:570
Data describing the table being created by CREATE TABLE or altered by ALTER TABLE.
Definition: sql_alter.h:204
Parser state for CTE subquery parser.
Definition: sql_lex.h:4573
Common_table_expr_parser_state()
Definition: sql_lex.cc:1126
PT_subquery * result
Definition: sql_lex.h:4577
Utility RAII class to save/modify/restore the condition_context information of a query block.
Definition: sql_lex.h:2369
enum_condition_context saved_value
Definition: sql_lex.h:2391
~Condition_context()
Definition: sql_lex.h:2385
Query_block * select
Definition: sql_lex.h:2390
Condition_context(Query_block *select_ptr, enum_condition_context new_type=enum_condition_context::NEITHER)
Definition: sql_lex.h:2371
Parser state for Derived table's condition parser.
Definition: sql_lex.h:4584
Item * result
Definition: sql_lex.h:4588
Derived_expr_parser_state()
Definition: sql_lex.cc:1129
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:4563
Expression_parser_state()
Definition: sql_lex.cc:1123
Item * result
Definition: sql_lex.h:4567
Definition: field.h:573
Parser state for generated column expression parser (.frm/DD stuff)
Definition: sql_lex.h:4553
Value_generator * result
Definition: sql_lex.h:4557
Gcol_expr_parser_state()
Definition: sql_lex.cc:1120
Definition: sql_lex.h:487
LEX_CSTRING key_name
Definition: sql_lex.h:497
void print(const THD *thd, String *str)
Print an index hint.
Definition: sql_lex.cc:2617
index_clause_map clause
Definition: sql_lex.h:492
enum index_hint_type type
Definition: sql_lex.h:490
Index_hint(const char *str, uint length)
Definition: sql_lex.h:499
Definition: item_cmpfunc.h:2355
Definition: item_subselect.h:412
Definition: item.h:4043
Definition: item_func.h:3402
Definition: item_func.h:3447
This class is used to implement operations like SET @variable or @variable:= expression.
Definition: item_func.h:3212
A wrapper Item that normally returns its parameter, but becomes NULL when processing rows for rollup.
Definition: item_func.h:1577
A wrapper Item that contains a number of aggregate items, one for each level of rollup (see Item_roll...
Definition: item_sum.h:2698
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:802
cond_result
Definition: item.h:871
@ COND_UNDEF
Definition: item.h:871
Definition: sql_optimizer.h:125
Definition: key_spec.h:66
Definition: sql_lex.h:3583
LEX_COLUMN(const String &x, const uint &y)
Definition: sql_lex.h:3587
uint rights
Definition: sql_lex.h:3586
String column
Definition: sql_lex.h:3585
Definition: sql_lex.h:3595
List< LEX_USER > * role_list
Definition: sql_lex.h:3604
void cleanup()
Definition: sql_lex.cc:5046
bool grant_as_used
Definition: sql_lex.h:3601
role_enum role_type
Definition: sql_lex.h:3602
LEX_USER * user
Definition: sql_lex.h:3603
LEX_GRANT_AS()
Definition: sql_lex.cc:5053
This class represents the character input stream consumed during lexical analysis.
Definition: sql_lex.h:3140
const char * m_end_of_query
End of the query text in the input stream, in the raw buffer.
Definition: sql_lex.h:3461
void restart_token()
Adjust the starting position of the current token.
Definition: sql_lex.h:3320
void body_utf8_start(THD *thd, const char *begin_ptr)
The operation is called from the parser in order to 1) designate the intention to have utf8 body; 1) ...
Definition: sql_lex.cc:279
const int grammar_selector_token
The synthetic 1st token to prepend token stream with.
Definition: sql_lex.h:3578
void skip_binary(int n)
Skip binary from the input stream.
Definition: sql_lex.h:3184
bool skip_digest
Skip adding of the current token's digest since it is already added.
Definition: sql_lex.h:3410
const char * get_end_of_query() const
Get the end of the raw query buffer.
Definition: sql_lex.h:3305
const char * get_cpp_tok_start() const
Get the token start position, in the pre-processed buffer.
Definition: sql_lex.h:3329
const char * m_tok_start
Starting position of the last token parsed, in the raw buffer.
Definition: sql_lex.h:3455
void body_utf8_append(const char *ptr)
The operation appends unprocessed part of the pre-processed buffer till the given pointer (ptr) and s...
Definition: sql_lex.cc:340
uchar tok_bitmap
Token character bitmaps, to detect 7bit strings.
Definition: sql_lex.h:3514
char * m_cpp_ptr
Pointer to the current position in the pre-processed input stream.
Definition: sql_lex.h:3477
void restore_in_comment_state()
Definition: sql_lex.h:3175
const char * get_tok_start() const
Get the token start position, in the raw buffer.
Definition: sql_lex.h:3326
const char * get_buf() const
Get the raw query buffer.
Definition: sql_lex.h:3299
bool multi_statements
true if we should allow multi-statements.
Definition: sql_lex.h:3527
const char * m_cpp_tok_start
Starting position of the last token parsed, in the pre-processed buffer.
Definition: sql_lex.h:3483
char * yyUnput(char ch)
Puts a character back into the stream, canceling the effect of the last yyGet() or yySkip().
Definition: sql_lex.h:3267
uint get_body_utf8_length() const
Get the utf8-body length.
Definition: sql_lex.h:3357
bool m_echo_saved
Definition: sql_lex.h:3471
char * cpp_inject(char ch)
Inject a character into the pre-processed stream.
Definition: sql_lex.h:3280
const char * m_tok_end
Ending position of the previous token parsed, in the raw buffer.
Definition: sql_lex.h:3458
void warn_on_deprecated_collation(const CHARSET_INFO *collation) const
Outputs warnings on deprecated collations in complete SQL statements.
Definition: sql_lex.h:3442
bool init(THD *thd, const char *buff, size_t length)
Object initializer.
Definition: sql_lex.cc:199
enum my_lex_states next_state
Current state of the lexical analyser.
Definition: sql_lex.h:3505
Lexer_yystype * yylval
Interface with bison, value of the last token parsed.
Definition: sql_lex.h:3380
const char * get_tok_end() const
Get the token end position, in the raw buffer.
Def