MySQL 8.0.30
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 example 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 /**
2741 Generating invisible primary key for a table created using CREATE TABLE...
2742 SELECT... is unsafe because order in which rows are retrieved by the
2743 SELECT determines which (if any) rows are inserted. This order cannot be
2744 predicted and values for generated invisible primary key column may
2745 differ on source and replica when @@session.binlog_format=STATEMENT.
2746 */
2748
2749 /* the last element of this enumeration type. */
2752 /**
2753 This has all flags from 0 (inclusive) to BINLOG_STMT_FLAG_COUNT
2754 (exclusive) set.
2755 */
2757 ((1 << BINLOG_STMT_UNSAFE_COUNT) - 1);
2758
2759 /**
2760 Maps elements of enum_binlog_stmt_unsafe to error codes.
2761 */
2763
2764 /**
2765 Determine if this statement is marked as unsafe.
2766
2767 @retval 0 if the statement is not marked as unsafe.
2768 @retval nonzero if the statement is marked as unsafe.
2769 */
2770 inline bool is_stmt_unsafe() const { return get_stmt_unsafe_flags() != 0; }
2771
2773 return binlog_stmt_flags & (1 << unsafe);
2774 }
2775
2776 /**
2777 Flag the current (top-level) statement as unsafe.
2778 The flag will be reset after the statement has finished.
2779
2780 @param unsafe_type The type of unsafety: one of the @c
2781 BINLOG_STMT_FLAG_UNSAFE_* flags in @c enum_binlog_stmt_flag.
2782 */
2783 inline void set_stmt_unsafe(enum_binlog_stmt_unsafe unsafe_type) {
2784 DBUG_TRACE;
2785 assert(unsafe_type >= 0 && unsafe_type < BINLOG_STMT_UNSAFE_COUNT);
2786 binlog_stmt_flags |= (1U << unsafe_type);
2787 return;
2788 }
2789
2790 /**
2791 Set the bits of binlog_stmt_flags determining the type of
2792 unsafeness of the current statement. No existing bits will be
2793 cleared, but new bits may be set.
2794
2795 @param flags A binary combination of zero or more bits, (1<<flag)
2796 where flag is a member of enum_binlog_stmt_unsafe.
2797 */
2799 DBUG_TRACE;
2800 assert((flags & ~BINLOG_STMT_UNSAFE_ALL_FLAGS) == 0);
2802 return;
2803 }
2804
2805 /**
2806 Return a binary combination of all unsafe warnings for the
2807 statement. If the statement has been marked as unsafe by the
2808 'flag' member of enum_binlog_stmt_unsafe, then the return value
2809 from this function has bit (1<<flag) set to 1.
2810 */
2812 DBUG_TRACE;
2814 }
2815
2816 /**
2817 Determine if this statement is a row injection.
2818
2819 @retval 0 if the statement is not a row injection
2820 @retval nonzero if the statement is a row injection
2821 */
2822 inline bool is_stmt_row_injection() const {
2823 return binlog_stmt_flags &
2825 }
2826
2827 /**
2828 Flag the statement as a row injection. A row injection is either
2829 a BINLOG statement, or a row event in the relay log executed by
2830 the slave SQL thread.
2831 */
2833 DBUG_TRACE;
2836 return;
2837 }
2838
2840 /*
2841 If a transactional table is about to be read. Note that
2842 a write implies a read.
2843 */
2845 /*
2846 If a non-transactional table is about to be read. Note that
2847 a write implies a read.
2848 */
2850 /*
2851 If a temporary transactional table is about to be read. Note
2852 that a write implies a read.
2853 */
2855 /*
2856 If a temporary non-transactional table is about to be read. Note
2857 that a write implies a read.
2858 */
2860 /*
2861 If a transactional table is about to be updated.
2862 */
2864 /*
2865 If a non-transactional table is about to be updated.
2866 */
2868 /*
2869 If a temporary transactional table is about to be updated.
2870 */
2872 /*
2873 If a temporary non-transactional table is about to be updated.
2874 */
2876 /*
2877 The last element of the enumeration. Please, if necessary add
2878 anything before this.
2879 */
2882
2883#ifndef NDEBUG
2884 static inline const char *stmt_accessed_table_string(
2885 enum_stmt_accessed_table accessed_table) {
2886 switch (accessed_table) {
2888 return "STMT_READS_TRANS_TABLE";
2889 break;
2891 return "STMT_READS_NON_TRANS_TABLE";
2892 break;
2894 return "STMT_READS_TEMP_TRANS_TABLE";
2895 break;
2897 return "STMT_READS_TEMP_NON_TRANS_TABLE";
2898 break;
2900 return "STMT_WRITES_TRANS_TABLE";
2901 break;
2903 return "STMT_WRITES_NON_TRANS_TABLE";
2904 break;
2906 return "STMT_WRITES_TEMP_TRANS_TABLE";
2907 break;
2909 return "STMT_WRITES_TEMP_NON_TRANS_TABLE";
2910 break;
2912 default:
2913 assert(0);
2914 break;
2915 }
2917 return "";
2918 }
2919#endif /* DBUG */
2920
2921#define BINLOG_DIRECT_ON \
2922 0xF0 /* unsafe when \
2923 --binlog-direct-non-trans-updates \
2924 is ON */
2925
2926#define BINLOG_DIRECT_OFF \
2927 0xF /* unsafe when \
2928 --binlog-direct-non-trans-updates \
2929 is OFF */
2930
2931#define TRX_CACHE_EMPTY 0x33 /* unsafe when trx-cache is empty */
2932
2933#define TRX_CACHE_NOT_EMPTY 0xCC /* unsafe when trx-cache is not empty */
2934
2935#define IL_LT_REPEATABLE 0xAA /* unsafe when < ISO_REPEATABLE_READ */
2936
2937#define IL_GTE_REPEATABLE 0x55 /* unsafe when >= ISO_REPEATABLE_READ */
2938
2939 /**
2940 Sets the type of table that is about to be accessed while executing a
2941 statement.
2943 @param accessed_table Enumeration type that defines the type of table,
2944 e.g. temporary, transactional, non-transactional.
2945 */
2946 inline void set_stmt_accessed_table(enum_stmt_accessed_table accessed_table) {
2947 DBUG_TRACE;
2948
2949 assert(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
2950 stmt_accessed_table_flag |= (1U << accessed_table);
2951
2952 return;
2953 }
2954
2955 /**
2956 Checks if a type of table is about to be accessed while executing a
2957 statement.
2958
2959 @param accessed_table Enumeration type that defines the type of table,
2960 e.g. temporary, transactional, non-transactional.
2962 @retval true if the type of the table is about to be accessed
2963 @retval false otherwise
2964 */
2965 inline bool stmt_accessed_table(enum_stmt_accessed_table accessed_table) {
2966 DBUG_TRACE;
2967
2968 assert(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
2969
2970 return (stmt_accessed_table_flag & (1U << accessed_table)) != 0;
2971 }
2972
2973 /*
2974 Checks if a mixed statement is unsafe.
2975
2976
2977 @param in_multi_stmt_transaction_mode defines if there is an on-going
2978 multi-transactional statement.
2979 @param binlog_direct defines if --binlog-direct-non-trans-updates is
2980 active.
2981 @param trx_cache_is_not_empty defines if the trx-cache is empty or not.
2982 @param trx_isolation defines the isolation level.
2983
2984 @return
2985 @retval true if the mixed statement is unsafe
2986 @retval false otherwise
2987 */
2988 inline bool is_mixed_stmt_unsafe(bool in_multi_stmt_transaction_mode,
2989 bool binlog_direct,
2990 bool trx_cache_is_not_empty,
2991 uint tx_isolation) {
2992 bool unsafe = false;
2993
2994 if (in_multi_stmt_transaction_mode) {
2995 uint condition =
2996 (binlog_direct ? BINLOG_DIRECT_ON : BINLOG_DIRECT_OFF) &
2997 (trx_cache_is_not_empty ? TRX_CACHE_NOT_EMPTY : TRX_CACHE_EMPTY) &
2998 (tx_isolation >= ISO_REPEATABLE_READ ? IL_GTE_REPEATABLE
3000
3001 unsafe = (binlog_unsafe_map[stmt_accessed_table_flag] & condition);
3002
3003#if !defined(NDEBUG)
3004 DBUG_PRINT("LEX::is_mixed_stmt_unsafe",
3005 ("RESULT %02X %02X %02X\n", condition,
3008
3009 int type_in = 0;
3010 for (; type_in < STMT_ACCESS_TABLE_COUNT; type_in++) {
3012 DBUG_PRINT("LEX::is_mixed_stmt_unsafe",
3013 ("ACCESSED %s ", stmt_accessed_table_string(
3014 (enum_stmt_accessed_table)type_in)));
3015 }
3016#endif
3017 }
3018
3021 tx_isolation < ISO_REPEATABLE_READ)
3022 unsafe = true;
3025 tx_isolation < ISO_REPEATABLE_READ)
3026 unsafe = true;
3027
3028 return (unsafe);
3029 }
3030
3031 /**
3032 true if the parsed tree contains references to stored procedures
3033 or functions, false otherwise
3035 bool uses_stored_routines() const { return sroutines_list.elements != 0; }
3037 void set_using_match() { using_match = true; }
3038 bool get_using_match() { return using_match; }
3039
3041 bool is_stmt_unsafe_with_mixed_mode() const {
3043 }
3044
3045 private:
3046 /**
3047 Enumeration listing special types of statements.
3048
3049 Currently, the only possible type is ROW_INJECTION.
3050 */
3052 /**
3053 The statement is a row injection (i.e., either a BINLOG
3054 statement or a row event executed by the slave SQL thread).
3055 */
3057
3058 /** The last element of this enumeration type. */
3060 };
3061
3062 /**
3063 Bit field indicating the type of statement.
3064
3065 There are two groups of bits:
3066
3067 - The low BINLOG_STMT_UNSAFE_COUNT bits indicate the types of
3068 unsafeness that the current statement has.
3069
3070 - The next BINLOG_STMT_TYPE_COUNT bits indicate if the statement
3071 is of some special type.
3072
3073 This must be a member of LEX, not of THD: each stored procedure
3074 needs to remember its unsafeness state between calls and each
3075 stored procedure has its own LEX object (but no own THD object).
3076 */
3078
3079 /**
3080 Bit field that determines the type of tables that are about to be
3081 be accessed while executing a statement.
3082 */
3085 /**
3086 It will be set true if 'MATCH () AGAINST' is used in the statement.
3087 */
3088 bool using_match;
3089
3090 /**
3091 This flag is set to true if statement is unsafe to be binlogged in STATEMENT
3092 format, when in MIXED mode.
3093 Currently this flag is set to true if stored program used in statement has
3094 CREATE/DROP temporary table operation(s) as sub-statement(s).
3095 */
3096 bool stmt_unsafe_with_mixed_mode{false};
3097};
3098
3099/*
3100 st_parsing_options contains the flags for constructions that are
3101 allowed in the current statement.
3103
3105 bool allows_variable;
3106 bool allows_select_into;
3107
3108 st_parsing_options() { reset(); }
3109 void reset();
3110};
3112/**
3113 The state of the lexical parser, when parsing comments.
3114*/
3116 /**
3117 Not parsing comments.
3118 */
3119 NO_COMMENT,
3120
3121 /**
3122 Parsing comments that need to be preserved.
3123 (Copy '/' '*' and '*' '/' sequences to the preprocessed buffer.)
3124 Typically, these are user comments '/' '*' ... '*' '/'.
3125 */
3127
3128 /**
3129 Parsing comments that need to be discarded.
3130 (Don't copy '/' '*' '!' and '*' '/' sequences to the preprocessed buffer.)
3131 Typically, these are special comments '/' '*' '!' ... '*' '/',
3132 or '/' '*' '!' 'M' 'M' 'm' 'm' 'm' ... '*' '/', where the comment
3133 markers should not be expanded.
3134 */
3136};
3137
3138/**
3139 This class represents the character input stream consumed during lexical
3140 analysis.
3141
3142 In addition to consuming the input stream, this class performs some comment
3143 pre processing, by filtering out out-of-bound special text from the query
3144 input stream.
3145
3146 Two buffers, with pointers inside each, are maintained in parallel. The
3147 'raw' buffer is the original query text, which may contain out-of-bound
3148 comments. The 'cpp' (for comments pre processor) is the pre-processed buffer
3149 that contains only the query text that should be seen once out-of-bound data
3150 is removed.
3151*/
3152
3153class Lex_input_stream {
3154 public:
3155 /**
3156 Constructor
3158 @param grammar_selector_token_arg See grammar_selector_token.
3159 */
3160
3161 explicit Lex_input_stream(uint grammar_selector_token_arg)
3162 : grammar_selector_token(grammar_selector_token_arg) {}
3163
3164 /**
3165 Object initializer. Must be called before usage.
3166
3167 @retval false OK
3168 @retval true Error
3169 */
3170 bool init(THD *thd, const char *buff, size_t length);
3171
3172 void reset(const char *buff, size_t length);
3173
3174 /**
3175 Set the echo mode.
3176
3177 When echo is true, characters parsed from the raw input stream are
3178 preserved. When false, characters parsed are silently ignored.
3179 @param echo the echo mode.
3180 */
3181 void set_echo(bool echo) { m_echo = echo; }
3182
3183 void save_in_comment_state() {
3186 }
3187
3191 }
3192
3193 /**
3194 Skip binary from the input stream.
3195 @param n number of bytes to accept.
3196 */
3197 void skip_binary(int n) {
3198 assert(m_ptr + n <= m_end_of_query);
3199 if (m_echo) {
3200 memcpy(m_cpp_ptr, m_ptr, n);
3201 m_cpp_ptr += n;
3202 }
3203 m_ptr += n;
3204 }
3205
3206 /**
3207 Get a character, and advance in the stream.
3208 @return the next character to parse.
3209 */
3210 unsigned char yyGet() {
3211 assert(m_ptr <= m_end_of_query);
3212 char c = *m_ptr++;
3213 if (m_echo) *m_cpp_ptr++ = c;
3214 return c;
3215 }
3216
3217 /**
3218 Get the last character accepted.
3219 @return the last character accepted.
3220 */
3221 unsigned char yyGetLast() const { return m_ptr[-1]; }
3223 /**
3224 Look at the next character to parse, but do not accept it.
3225 */
3226 unsigned char yyPeek() const {
3227 assert(m_ptr <= m_end_of_query);
3228 return m_ptr[0];
3229 }
3230
3231 /**
3232 Look ahead at some character to parse.
3233 @param n offset of the character to look up
3234 */
3235 unsigned char yyPeekn(int n) const {
3236 assert(m_ptr + n <= m_end_of_query);
3237 return m_ptr[n];
3238 }
3239
3240 /**
3241 Cancel the effect of the last yyGet() or yySkip().
3242 Note that the echo mode should not change between calls to yyGet / yySkip
3243 and yyUnget. The caller is responsible for ensuring that.
3244 */
3245 void yyUnget() {
3246 m_ptr--;
3247 if (m_echo) m_cpp_ptr--;
3248 }
3250 /**
3251 Accept a character, by advancing the input stream.
3252 */
3253 void yySkip() {
3254 assert(m_ptr <= m_end_of_query);
3255 if (m_echo)
3256 *m_cpp_ptr++ = *m_ptr++;
3257 else
3258 m_ptr++;
3259 }
3260
3261 /**
3262 Accept multiple characters at once.
3263 @param n the number of characters to accept.
3264 */
3265 void yySkipn(int n) {
3266 assert(m_ptr + n <= m_end_of_query);
3267 if (m_echo) {
3268 memcpy(m_cpp_ptr, m_ptr, n);
3269 m_cpp_ptr += n;
3270 }
3271 m_ptr += n;
3272 }
3273
3274 /**
3275 Puts a character back into the stream, canceling
3276 the effect of the last yyGet() or yySkip().
3277 Note that the echo mode should not change between calls
3278 to unput, get, or skip from the stream.
3279 */
3280 char *yyUnput(char ch) {
3281 *--m_ptr = ch;
3282 if (m_echo) m_cpp_ptr--;
3283 return m_ptr;
3284 }
3285
3286 /**
3287 Inject a character into the pre-processed stream.
3288
3289 Note, this function is used to inject a space instead of multi-character
3290 C-comment. Thus there is no boundary checks here (basically, we replace
3291 N-chars by 1-char here).
3292 */
3293 char *cpp_inject(char ch) {
3294 *m_cpp_ptr = ch;
3295 return ++m_cpp_ptr;
3296 }
3297
3298 /**
3299 End of file indicator for the query text to parse.
3300 @return true if there are no more characters to parse
3301 */
3302 bool eof() const { return (m_ptr >= m_end_of_query); }
3303
3304 /**
3305 End of file indicator for the query text to parse.
3306 @param n number of characters expected
3307 @return true if there are less than n characters to parse
3309 bool eof(int n) const { return ((m_ptr + n) >= m_end_of_query); }
3310
3311 /** Get the raw query buffer. */
3312 const char *get_buf() const { return m_buf; }
3313
3314 /** Get the pre-processed query buffer. */
3315 const char *get_cpp_buf() const { return m_cpp_buf; }
3316
3317 /** Get the end of the raw query buffer. */
3318 const char *get_end_of_query() const { return m_end_of_query; }
3319
3320 /** Mark the stream position as the start of a new token. */
3321 void start_token() {
3323 m_tok_end = m_ptr;
3324
3327 }
3328
3329 /**
3330 Adjust the starting position of the current token.
3331 This is used to compensate for starting whitespace.
3332 */
3333 void restart_token() {
3336 }
3337
3338 /** Get the token start position, in the raw buffer. */
3339 const char *get_tok_start() const { return m_tok_start; }
3340
3341 /** Get the token start position, in the pre-processed buffer. */
3342 const char *get_cpp_tok_start() const { return m_cpp_tok_start; }
3343
3344 /** Get the token end position, in the raw buffer. */
3345 const char *get_tok_end() const { return m_tok_end; }
3346
3347 /** Get the token end position, in the pre-processed buffer. */
3348 const char *get_cpp_tok_end() const { return m_cpp_tok_end; }
3349
3350 /** Get the current stream pointer, in the raw buffer. */
3351 const char *get_ptr() const { return m_ptr; }
3352
3353 /** Get the current stream pointer, in the pre-processed buffer. */
3354 const char *get_cpp_ptr() const { return m_cpp_ptr; }
3355
3356 /** Get the length of the current token, in the raw buffer. */
3357 uint yyLength() const {
3358 /*
3359 The assumption is that the lexical analyser is always 1 character ahead,
3360 which the -1 account for.
3361 */
3362 assert(m_ptr > m_tok_start);
3363 return (uint)((m_ptr - m_tok_start) - 1);
3364 }
3365
3366 /** Get the utf8-body string. */
3367 const char *get_body_utf8_str() const { return m_body_utf8; }
3368
3369 /** Get the utf8-body length. */
3370 uint get_body_utf8_length() const {
3371 return (uint)(m_body_utf8_ptr - m_body_utf8);
3372 }
3373
3374 void body_utf8_start(THD *thd, const char *begin_ptr);
3375 void body_utf8_append(const char *ptr);
3376 void body_utf8_append(const char *ptr, const char *end_ptr);
3377 void body_utf8_append_literal(THD *thd, const LEX_STRING *txt,
3378 const CHARSET_INFO *txt_cs,
3379 const char *end_ptr);
3381 uint get_lineno(const char *raw_ptr) const;
3382
3383 /** Current thread. */
3384 THD *m_thd;
3385
3386 /** Current line number. */
3387 uint yylineno;
3388
3389 /** Length of the last token parsed. */
3390 uint yytoklen;
3391
3392 /** Interface with bison, value of the last token parsed. */
3394
3395 /**
3396 LALR(2) resolution, look ahead token.
3397 Value of the next token to return, if any,
3398 or -1, if no token was parsed in advance.
3399 Note: 0 is a legal token, and represents YYEOF.
3401 int lookahead_token;
3402
3403 /** LALR(2) resolution, value of the look ahead token.*/
3405
3406 /// Skip adding of the current token's digest since it is already added
3407 ///
3408 /// Usually we calculate a digest token by token at the top-level function
3409 /// of the lexer: MYSQLlex(). However, some complex ("hintable") tokens break
3410 /// that data flow: for example, the `SELECT /*+ HINT(t) */` is the single
3411 /// token from the main parser's point of view, and we add the "SELECT"
3412 /// keyword to the digest buffer right after the lex_one_token() call,
3413 /// but the "/*+ HINT(t) */" is a sequence of separate tokens from the hint
3414 /// parser's point of view, and we add those tokens to the digest buffer
3415 /// *inside* the lex_one_token() call. Thus, the usual data flow adds
3416 /// tokens from the "/*+ HINT(t) */" string first, and only than it appends
3417 /// the "SELECT" keyword token to that stream: "/*+ HINT(t) */ SELECT".
3418 /// This is not acceptable, since we use the digest buffer to restore
3419 /// query strings in their normalized forms, so the order of added tokens is
3420 /// important. Thus, we add tokens of "hintable" keywords to a digest buffer
3421 /// right in the hint parser and skip adding of them at the caller with the
3422 /// help of skip_digest flag.
3423 bool skip_digest;
3424
3426
3427 void reduce_digest_token(uint token_left, uint token_right);
3428
3429 /**
3430 True if this scanner tokenizes a partial query (partition expression,
3431 generated column expression etc.)
3432
3433 @return true if parsing a partial query, otherwise false.
3434 */
3435 bool is_partial_parser() const { return grammar_selector_token >= 0; }
3436
3437 /**
3438 Outputs warnings on deprecated charsets in complete SQL statements
3440 @param [in] cs The character set/collation to check for a deprecation.
3441 @param [in] alias The name/alias of @p cs.
3442 */
3444 const char *alias) const {
3445 if (!is_partial_parser()) {
3447 }
3448 }
3449
3450 /**
3451 Outputs warnings on deprecated collations in complete SQL statements
3452
3453 @param [in] collation The collation to check for a deprecation.
3454 */
3456 if (!is_partial_parser()) {
3458 }
3459 }
3460
3462
3463 private:
3464 /** Pointer to the current position in the raw input stream. */
3465 char *m_ptr;
3466
3467 /** Starting position of the last token parsed, in the raw buffer. */
3468 const char *m_tok_start;
3469
3470 /** Ending position of the previous token parsed, in the raw buffer. */
3471 const char *m_tok_end;
3472
3473 /** End of the query text in the input stream, in the raw buffer. */
3474 const char *m_end_of_query;
3475
3476 /** Beginning of the query text in the input stream, in the raw buffer. */
3477 const char *m_buf;
3478
3479 /** Length of the raw buffer. */
3481
3482 /** Echo the parsed stream to the pre-processed buffer. */
3484 bool m_echo_saved;
3485
3486 /** Pre-processed buffer. */
3487 char *m_cpp_buf;
3488
3489 /** Pointer to the current position in the pre-processed input stream. */
3490 char *m_cpp_ptr;
3491
3492 /**
3493 Starting position of the last token parsed,
3494 in the pre-processed buffer.
3495 */
3496 const char *m_cpp_tok_start;
3497
3498 /**
3499 Ending position of the previous token parsed,
3500 in the pre-processed buffer.
3502 const char *m_cpp_tok_end;
3503
3504 /** UTF8-body buffer created during parsing. */
3505 char *m_body_utf8;
3506
3507 /** Pointer to the current position in the UTF8-body buffer. */
3508 char *m_body_utf8_ptr;
3509
3510 /**
3511 Position in the pre-processed buffer. The query from m_cpp_buf to
3512 m_cpp_utf_processed_ptr is converted to UTF8-body.
3513 */
3515
3516 public:
3517 /** Current state of the lexical analyser. */
3519
3520 /**
3521 Position of ';' in the stream, to delimit multiple queries.
3522 This delimiter is in the raw buffer.
3524 const char *found_semicolon;
3525
3526 /** Token character bitmaps, to detect 7bit strings. */
3528
3529 /** SQL_MODE = IGNORE_SPACE. */
3530 bool ignore_space;
3531
3532 /**
3533 true if we're parsing a prepared statement: in this mode
3534 we should allow placeholders.
3535 */
3537 /**
3538 true if we should allow multi-statements.
3541
3542 /** State of the lexical analyser for comments. */
3545
3546 /**
3547 Starting position of the TEXT_STRING or IDENT in the pre-processed
3548 buffer.
3549
3550 NOTE: this member must be used within MYSQLlex() function only.
3551 */
3552 const char *m_cpp_text_start;
3553
3554 /**
3555 Ending position of the TEXT_STRING or IDENT in the pre-processed
3556 buffer.
3557
3558 NOTE: this member must be used within MYSQLlex() function only.
3559 */
3560 const char *m_cpp_text_end;
3561
3562 /**
3563 Character set specified by the character-set-introducer.
3564
3565 NOTE: this member must be used within MYSQLlex() function only.
3566 */
3569 /**
3570 Current statement digest instrumentation.
3571 */
3573
3574 /**
3575 The synthetic 1st token to prepend token stream with.
3576
3577 This token value tricks parser to simulate multiple %start-ing points.
3578 Currently the grammar is aware of 4 such synthetic tokens:
3579 1. GRAMMAR_SELECTOR_PART for partitioning stuff from DD,
3580 2. GRAMMAR_SELECTOR_GCOL for generated column stuff from DD,
3581 3. GRAMMAR_SELECTOR_EXPR for generic single expressions from DD/.frm.
3582 4. GRAMMAR_SELECTOR_CTE for generic subquery expressions from CTEs.
3583 5. -1 when parsing with the main grammar (no grammar selector available).
3584
3585 @note yylex() is expected to return the value of type int:
3586 0 is for EOF and everything else for real token numbers.
3587 Bison, in its turn, generates positive token numbers.
3588 So, the negative grammar_selector_token means "not a token".
3589 In other words, -1 is "empty value".
3590 */
3591 const int grammar_selector_token;
3593 bool text_string_is_7bit() const { return !(tok_bitmap & 0x80); }
3597 public:
3598 String column;
3599 uint rights;
3600 LEX_COLUMN(const String &x, const uint &y) : column(x), rights(y) {}
3601};
3602
3603enum class role_enum;
3605/*
3606 This structure holds information about grantor's context
3607*/
3608class LEX_GRANT_AS {
3609 public:
3611 void cleanup();
3613 public:
3614 bool grant_as_used;
3616 LEX_USER *user;
3618};
3619
3620/**
3621 The LEX object currently serves three different purposes:
3622
3623 - It contains some universal properties of an SQL command, such as
3624 sql_command, presence of IGNORE in data change statement syntax, and list
3625 of tables (query_tables).
3626
3627 - It contains some execution state variables, like m_exec_started
3628 (set to true when execution is started), plugins (list of plugins used
3629 by statement), insert_update_values_map (a map of objects used by certain
3630 INSERT statements), etc.
3631
3632 - It contains a number of members that should be local to subclasses of
3633 Sql_cmd, like purge_value_list (for the PURGE command), kill_value_list
3634 (for the KILL command).
3635
3636 The LEX object is strictly a part of class Sql_cmd, for those SQL commands
3637 that are represented by an Sql_cmd class. For the remaining SQL commands,
3638 it is a standalone object linked to the current THD.
3639
3640 The lifecycle of a LEX object is as follows:
3641
3642 - The LEX object is constructed either on the execution mem_root
3643 (for regular statements), on a Prepared_statement mem_root (for
3644 prepared statements), on an SP mem_root (for stored procedure instructions),
3645 or created on the current mem_root for short-lived uses.
3646
3647 - Call lex_start() to initialize a LEX object before use.
3648 This initializes the execution state part of the object.
3649 It also calls LEX::reset() to ensure that all members are properly inited.
3650
3651 - Parse and resolve the statement, using the LEX as a work area.
3652
3653 - Execute an SQL command: call set_exec_started() when starting to execute
3654 (actually when starting to optimize).
3655 Typically call is_exec_started() to distinguish between preparation
3656 and optimization/execution stages of SQL command execution.
3657
3658 - Call clear_execution() when execution is finished. This will clear all
3659 execution state associated with the SQL command, it also includes calling
3660 LEX::reset_exec_started().
3661
3662 @todo - Create subclasses of Sql_cmd to contain data that are local
3663 to specific commands.
3664
3665 @todo - Create a Statement context object that will hold the execution state
3666 part of struct LEX.
3667
3668 @todo - Ensure that a LEX struct is never reused, thus making e.g
3669 LEX::reset() redundant.
3670*/
3672struct LEX : public Query_tables_list {
3673 friend bool lex_start(THD *thd);
3675 Query_expression *unit; ///< Outer-most query expression
3676 /// @todo: query_block can be replaced with unit->first-select()
3677 Query_block *query_block; ///< First query block
3678 Query_block *all_query_blocks_list; ///< List of all query blocks
3679 private:
3680 /* current Query_block in parsing */
3682
3683 public:
3684 inline Query_block *current_query_block() const {
3685 return m_current_query_block;
3686 }
3687
3688 /*
3689 We want to keep current_thd out of header files, so the debug assert
3690 is moved to the .cc file.
3691 */
3693 inline void set_current_query_block(Query_block *select) {
3694#ifndef NDEBUG
3696#endif
3698 }
3699 /// @return true if this is an EXPLAIN statement
3700 bool is_explain() const { return explain_format != nullptr; }
3701 bool is_explain_analyze = false;
3702 /**
3703 Whether the currently-running query should be (attempted) executed in
3704 the hypergraph optimizer. This will not change after the query is
3705 done parsing, so you can use it in any query phase to e.g. figure out
3706 whether to inhibit some transformation that the hypergraph optimizer
3707 does not properly understand yet.
3712 char *to_log; /* For PURGE MASTER LOGS TO */
3714 // Widcard from SHOW ... LIKE <wildcard> statements.
3718 nullptr, 0}; ///< Argument of the BINLOG event statement.
3725 THD *thd;
3726
3727 /* Optimizer hints */
3730 /* maintain a list of used plugins for this LEX */
3735 /// Table being inserted into (may be a view)
3737 /// Leaf table being inserted into (always a base table)
3739
3740 /** SELECT of CREATE VIEW statement */
3742
3743 /* Partition info structure filled in by PARTITION BY parse part */
3745
3747 The definer of the object being created (view, trigger, stored routine).
3748 I.e. the value of DEFINER clause.
3758
3759 // PURGE statement-specific fields:
3761
3762 // KILL statement-specific fields:
3764
3765 // other stuff:
3767 List<Item_func_set_user_var> set_var_list; // in-query assignment list
3768 /**
3769 List of placeholders ('?') for parameters of a prepared statement. Because
3770 we append to this list during parsing, it is naturally sorted by
3771 position of the '?' in the query string. The code which fills placeholders
3772 with user-supplied values, and the code which writes a query for
3773 statement-based logging, rely on this order.
3774 This list contains only real placeholders, not the clones which originate
3775 in a re-parsed CTE definition.
3776 */
3778
3780
3781 void insert_values_map(Item_field *f1, Field *f2) {
3783 insert_update_values_map = new std::map<Item_field *, Field *>;
3784 insert_update_values_map->insert(std::make_pair(f1, f2));
3785 }
3786 void destroy_values_map() {
3788 insert_update_values_map->clear();
3790 insert_update_values_map = nullptr;
3791 }
3792 }
3793 void clear_values_map() {
3796 }
3797 }
3798 bool has_values_map() const { return insert_update_values_map != nullptr; }
3799 std::map<Item_field *, Field *>::iterator begin_values_map() {
3800 return insert_update_values_map->begin();
3801 }
3802 std::map<Item_field *, Field *>::iterator end_values_map() {
3803 return insert_update_values_map->end();
3804 }
3805
3806 private:
3807 /*
3808 With Visual Studio, an std::map will always allocate two small objects
3809 on the heap. Sometimes we put LEX objects in a MEM_ROOT, and never run
3810 the LEX DTOR. To avoid memory leaks, put this std::map on the heap,
3811 and call clear_values_map() at the end of each statement.
3812 */
3813 std::map<Item_field *, Field *> *insert_update_values_map;
3814
3815 public:
3816 /*
3817 A stack of name resolution contexts for the query. This stack is used
3818 at parse time to set local name resolution contexts for various parts
3819 of a query. For example, in a JOIN ... ON (some_condition) clause the
3820 Items in 'some_condition' must be resolved only against the operands
3821 of the the join, and not against the whole clause. Similarly, Items in
3822 subqueries should be resolved against the subqueries (and outer queries).
3823 The stack is used in the following way: when the parser detects that
3824 all Items in some clause need a local context, it creates a new context
3825 and pushes it on the stack. All newly created Items always store the
3826 top-most context in the stack. Once the parser leaves the clause that
3827 required a local context, the parser pops the top-most context.
3833 HA_CHECK_OPT check_opt; // check/repair options
3836 LEX_MASTER_INFO mi; // used by CHANGE MASTER
3841 ulong type;
3842 /**
3843 This field is used as a work field during resolving to validate
3844 the use of aggregate functions. For example in a query
3845 SELECT ... FROM ...WHERE MIN(i) == 1 GROUP BY ... HAVING MIN(i) > 2
3846 MIN(i) in the WHERE clause is not allowed since only non-aggregated data
3847 is present, whereas MIN(i) in the HAVING clause is allowed because HAVING
3848 operates on the output of a grouping operation.
3849 Each query block is assigned a nesting level. This field is a bit field
3850 that contains the value one in the position of that nesting level if
3851 aggregate functions are allowed for that query block.
3852 */
3854 /**
3855 Windowing functions are not allowed in HAVING - in contrast to group
3856 aggregates - then we need to be stricter than allow_sum_func.
3857 One bit per query block, as allow_sum_func.
3858 */
3861 /// If true: during prepare, we did a subquery transformation (IN-to-EXISTS,
3862 /// SOME/ANY) that doesn't currently work for subquery to a derived table
3863 /// transformation.
3865
3867
3868 /*
3869 Usually `expr` rule of yacc is quite reused but some commands better
3870 not support subqueries which comes standard with this rule, like
3871 KILL, HA_READ, CREATE/ALTER EVENT etc. Set this to `false` to get
3872 syntax error back.
3873 */
3875 /**
3876 If currently re-parsing a CTE's definition, this is the offset in bytes
3877 of that definition in the original statement which had the WITH
3878 clause. Otherwise this is 0.
3879 */
3881 /**
3882 If currently re-parsing a condition which is pushed down to a derived
3883 table, this will be set to true.
3884 */
3886 /**
3887 If currently re-parsing a condition that is being pushed down to a
3888 derived table, this has the positions of all the parameters that are
3889 part of that condition in the original statement. Otherwise it is empty.
3893 enum SSL_type ssl_type; /* defined in violite.h */
3899 /// QUERY ID for SHOW PROFILE
3903 /**
3904 Set to true when GRANT ... GRANT OPTION ... TO ...
3905 is used (vs. GRANT ... WITH GRANT OPTION).
3906 The flag is used by @ref mysql_grant to grant GRANT OPTION (@ref GRANT_ACL)
3907 to all dynamic privileges.
3911 int select_number; ///< Number of query block (by EXPLAIN)
3914 /**
3915 @todo ensure that correct CONTEXT_ANALYSIS_ONLY is set for all preparation
3916 code, so we can fully rely on this field.
3917 */
3919 bool drop_if_exists;
3920 /**
3921 refers to optional IF EXISTS clause in REVOKE sql. This flag when set to
3922 true will report warnings in case privilege being granted is not granted to
3923 given user/role. When set to false error is reported.
3924 */
3925 bool grant_if_exists;
3926 /**
3927 refers to optional IGNORE UNKNOWN USER clause in REVOKE sql. This flag when
3928 set to true will report warnings in case target user/role for which
3929 privilege being granted does not exists. When set to false error is
3930 reported.
3934 bool autocommit;
3936 // For show commands to show hidden columns and indexes.
3937 bool m_extended_show;
3938
3939 enum enum_yes_no_unknown tx_chain, tx_release;
3940
3941 /**
3942 Whether this query will return the same answer every time, given unchanged
3943 data. Used to be for the query cache, but is now used to find out if an
3944 expression is usable for partitioning.
3945 */
3948 private:
3949 /// True if statement references UDF functions
3950 bool m_has_udf{false};
3953 public:
3954 bool is_ignore() const { return ignore; }
3955 void set_ignore(bool ignore_param) { ignore = ignore_param; }
3956 void set_has_udf() { m_has_udf = true; }
3957 bool has_udf() const { return m_has_udf; }
3960 /* Prepared statements SQL syntax:*/
3961 LEX_CSTRING prepared_stmt_name; /* Statement name (in all queries) */
3963 Prepared statement query text or name of variable that holds the
3964 prepared statement (in PREPARE ... queries)
3965 */
3967 /* If true, prepared_stmt_code is a name of variable that holds the query */
3969 /* Names of user variables holding parameters (in EXECUTE) */
3973 bool sp_lex_in_use; /* Keep track on lex usage in SPs for error handling */
3974 bool all_privileges;
3978
3979 private:
3980 bool m_broken; ///< see mark_broken()
3981 /**
3982 Set to true when execution has started (after parsing, tables opened and
3983 query preparation is complete. Used to track arena state for SPs).
3984 */
3985 bool m_exec_started;
3986 /**
3987 Set to true when execution is completed, ie optimization has been done
3988 and execution is successful or ended in error.
3989 */
3990 bool m_exec_completed;
3991 /**
3992 Current SP parsing context.
3993 @see also sp_head::m_root_parsing_ctx.
3994 */
3997 /**
3998 Statement context for Query_block::make_active_options.
3999 */
4001
4002 public:
4003 /**
4004 Gets the options that have been set for this statement. The options are
4005 propagated to the Query_block objects and should usually be read with
4006 #Query_block::active_options().
4007
4008 @return a bit set of options set for this statement
4009 */
4011 /**
4012 Add options to values of m_statement_options. options is an ORed
4013 bit set of options defined in query_options.h
4015 @param options Add this set of options to the set already in
4016 m_statement_options
4020 }
4021 bool is_broken() const { return m_broken; }
4022 /**
4023 Certain permanent transformations (like in2exists), if they fail, may
4024 leave the LEX in an inconsistent state. They should call the
4025 following function, so that this LEX is not reused by another execution.
4027 @todo If lex_start () were a member function of LEX, the "broken"
4028 argument could always be "true" and thus could be removed.
4029 */
4030 void mark_broken(bool broken = true) {
4031 if (broken) {
4032 /*
4033 "OPEN <cursor>" cannot be re-prepared if the cursor uses no tables
4034 ("SELECT FROM DUAL"). Indeed in that case cursor_query is left empty
4035 in constructions of sp_instr_cpush, and thus
4036 sp_lex_instr::parse_expr() cannot re-prepare. So we mark the statement
4037 as broken only if tables are used.
4038 */
4039 if (is_metadata_used()) m_broken = true;
4040 } else
4041 m_broken = false;
4043
4045
4046 void cleanup(THD *thd, bool full) {
4047 unit->cleanup(thd, full);
4048 if (full) {
4053
4054 bool is_exec_started() const { return m_exec_started; }
4055 void set_exec_started() { m_exec_started = true; }
4056 void reset_exec_started() {
4057 m_exec_started = false;
4058 m_exec_completed = false;
4059 }
4060 /**
4061 Check whether the statement has been executed (regardless of completion -
4062 successful or in error).
4063 Check this instead of Query_expression::is_executed() to determine
4064 the state of a complete statement.
4065 */
4066 bool is_exec_completed() const { return m_exec_completed; }
4067 void set_exec_completed() { m_exec_completed = true; }
4069
4073
4074 /// Check if the current statement uses meta-data (uses a table or a stored
4075 /// routine).
4076 bool is_metadata_used() const {
4077 return query_tables != nullptr || has_udf() ||
4078 (sroutines != nullptr && !sroutines->empty());
4079 }
4081 public:
4083
4085
4086 bool only_view; /* used for SHOW CREATE TABLE/VIEW */
4087 /*
4088 view created to be run from definer (standard behaviour)
4089 */
4091
4092 /**
4093 Intended to point to the next word after DEFINER-clause in the
4094 following statements:
4095
4096 - CREATE TRIGGER (points to "TRIGGER");
4097 - CREATE PROCEDURE (points to "PROCEDURE");
4098 - CREATE FUNCTION (points to "FUNCTION" or "AGGREGATE");
4099 - CREATE EVENT (points to "EVENT")
4101 This pointer is required to add possibly omitted DEFINER-clause to the
4102 DDL-statement before dumping it to the binlog.
4103 */
4104 const char *stmt_definition_begin;
4105 const char *stmt_definition_end;
4106
4107 /**
4108 During name resolution search only in the table list given by
4109 Name_resolution_context::first_name_resolution_table and
4110 Name_resolution_context::last_name_resolution_table
4111 (see Item_field::fix_fields()).
4112 */
4114
4115 bool is_lex_started; /* If lex_start() did run. For debugging. */
4116 /// Set to true while resolving values in ON DUPLICATE KEY UPDATE clause
4120
4121 // Maximum execution time for a statement.
4122 ulong max_execution_time;
4123
4125 To flag the current statement as dependent for binary logging
4126 on explicit_defaults_for_timestamp
4127 */
4129
4130 /**
4131 Used to inform the parser whether it should contextualize the parse
4132 tree. When we get a pure parser this will not be needed.
4133 */
4134 bool will_contextualize;
4135
4136 LEX();
4138 virtual ~LEX();
4139
4140 /// Destroy contained objects, but not the LEX object itself.
4141 void destroy() {
4142 if (unit == nullptr) return;
4143 unit->destroy();
4144 unit = nullptr;
4145 query_block = nullptr;
4146 all_query_blocks_list = nullptr;
4147 m_current_query_block = nullptr;
4149 }
4150
4151 /// Reset query context to initial state
4152 void reset();
4153
4154 /// Create an empty query block within this LEX object.
4156
4157 /// Create query expression object that contains one query block.
4158 Query_block *new_query(Query_block *curr_query_block);
4159
4160 /// Create query block and attach it to the current query expression.
4161 Query_block *new_union_query(Query_block *curr_query_block, bool distinct);
4162
4163 /// Create top-level query expression and query block.
4164 bool new_top_level_query();
4165
4166 /// Create query expression and query block in existing memory objects.
4167 void new_static_query(Query_expression *sel_query_expression,
4168 Query_block *select);
4169
4170 /// Create query expression under current_query_block and a query block under
4171 /// the new query expression. The new query expression is linked in under
4172 /// current_query_block. The new query block is linked in under the new
4173 /// query expression.
4174 ///
4175 /// @param thd current session context
4176 /// @param current_query_block the root under which we create the new
4177 /// expression
4178 /// and block
4179 /// @param where_clause any where clause for the block
4180 /// @param having_clause any having clause for the block
4181 /// @param ctx the parsing context
4182 ///
4183 /// @returns the new query expression, or nullptr on error.
4185 THD *thd, Query_block *current_query_block, Item *where_clause,
4186 Item *having_clause, enum_parsing_context ctx);
4187
4188 inline bool is_ps_or_view_context_analysis() {
4191 }
4192
4193 inline bool is_view_context_analysis() {
4195 }
4196
4197 void clear_execution();
4198
4199 /**
4200 Set the current query as uncacheable.
4201
4202 @param curr_query_block Current select query block
4203 @param cause Why this query is uncacheable.
4204
4205 @details
4206 All query blocks representing subqueries, from the current one up to
4207 the outer-most one, but excluding the main query block, are also set
4208 as uncacheable.
4209 */
4210 void set_uncacheable(Query_block *curr_query_block, uint8 cause) {
4211 safe_to_cache_query = false;
4212
4213 if (m_current_query_block == nullptr) return;
4214 Query_block *sl;
4215 Query_expression *un;
4216 for (sl = curr_query_block, un = sl->master_query_expression(); un != unit;
4217 sl = sl->outer_query_block(), un = sl->master_query_expression()) {
4218 sl->uncacheable |= cause;
4219 un->uncacheable |= cause;
4220 }
4221 }
4223
4224 TABLE_LIST *unlink_first_table(bool *link_to_local);
4225 void link_first_table_back(TABLE_LIST *first, bool link_to_local);
4227
4229
4231 for (TABLE_LIST *tr = insert_table->first_leaf_table(); tr != nullptr;
4232 tr = tr->next_leaf)
4233 tr->restore_properties();
4234 }
4235
4237
4238 bool can_use_merged();
4239 bool can_not_use_merged();
4240 bool need_correct_ident();
4241 /*
4242 Is this update command where 'WHITH CHECK OPTION' clause is important
4243
4244 SYNOPSIS
4245 LEX::which_check_option_applicable()
4246
4247 RETURN
4248 true have to take 'WHITH CHECK OPTION' clause into account
4249 false 'WHITH CHECK OPTION' clause do not need
4250 */
4251 inline bool which_check_option_applicable() {
4252 switch (sql_command) {
4253 case SQLCOM_UPDATE:
4255 case SQLCOM_INSERT:
4257 case SQLCOM_REPLACE:
4259 case SQLCOM_LOAD:
4260 return true;
4261 default:
4262 return false;
4263 }
4265
4267
4269 return context_stack.push_front(context);
4270 }
4271
4272 void pop_context() { context_stack.pop(); }
4273
4274 bool copy_db_to(char const **p_db, size_t *p_db_length) const;
4275
4276 bool copy_db_to(char **p_db, size_t *p_db_length) const {
4277 return copy_db_to(const_cast<const char **>(p_db), p_db_length);
4278 }
4279
4281
4284
4285 bool table_or_sp_used();
4286
4287 /**
4288 @brief check if the statement is a single-level join
4289 @return result of the check
4290 @retval true The statement doesn't contain subqueries, unions and
4291 stored procedure calls.
4292 @retval false There are subqueries, UNIONs or stored procedure calls.
4293 */
4294 bool is_single_level_stmt() {
4295 /*
4296 This check exploits the fact that the last added to all_select_list is
4297 on its top. So query_block (as the first added) will be at the tail
4298 of the list.
4299 */
4301 (sroutines == nullptr || sroutines->empty())) {
4303 return true;
4304 }
4305 return false;
4306 }
4307
4308 void release_plugins();
4309
4310 /**
4311 IS schema queries read some dynamic table statistics from SE.
4312 These statistics are cached, to avoid opening of table more
4313 than once while preparing a single output record buffer.
4314 */
4317
4318 bool accept(Select_lex_visitor *visitor);
4319
4320 bool set_wild(LEX_STRING);
4321 void clear_privileges();
4322
4323 bool make_sql_cmd(Parse_tree_root *parse_tree);
4324
4325 private:
4326 /**
4327 Context object used by secondary storage engines to store query
4328 state during optimization and execution.
4329 */
4331
4332 public:
4333 /**
4334 Gets the secondary engine execution context for this statement.
4335 */
4337 const {
4339 }
4340
4341 /**
4342 Sets the secondary engine execution context for this statement.
4343 The old context object is destroyed, if there is one. Can be set
4344 to nullptr to destroy the old context object and clear the
4345 pointer.
4346
4347 The supplied context object should be allocated on the execution
4348 MEM_ROOT, so that its memory doesn't have to be manually freed
4349 after query execution.
4350 */
4353
4354 private:
4356
4357 public:
4360 }
4361
4364 }
4365
4366 private:
4368
4369 public:
4372 }
4373
4376 }
4379
4380 private:
4381 bool rewrite_required{false};
4383 public:
4384 void set_rewrite_required() { rewrite_required = true; }
4385 void reset_rewrite_required() { rewrite_required = false; }
4386 bool is_rewrite_required() { return rewrite_required; }
4387};
4388
4390 RAII class to ease the call of LEX::mark_broken() if error.
4391 Used during preparation and optimization of DML queries.
4392*/
4394 public:
4395 Prepare_error_tracker(THD *thd_arg) : thd(thd_arg) {}
4397
4398 private:
4399 THD *const thd;
4400};
4401
4402/**
4403 The internal state of the syntax parser.
4404 This object is only available during parsing,
4405 and is private to the syntax parser implementation (sql_yacc.yy).
4406*/
4407class Yacc_state {
4408 public:
4410 reset();
4411 }
4412
4413 void reset() {
4414 if (yacc_yyss != nullptr) {
4416 yacc_yyss = nullptr;
4417 }
4418 if (yacc_yyvs != nullptr) {
4420 yacc_yyvs = nullptr;
4421 }
4422 if (yacc_yyls != nullptr) {
4424 yacc_yyls = nullptr;
4425 }
4428 }
4429
4430 ~Yacc_state();
4431
4432 /**
4433 Reset part of the state which needs resetting before parsing
4434 substatement.
4435 */
4439 }
4440
4441 /**
4442 Bison internal state stack, yyss, when dynamically allocated using
4443 my_yyoverflow().
4444 */
4446
4447 /**
4448 Bison internal semantic value stack, yyvs, when dynamically allocated using
4449 my_yyoverflow().
4450 */
4452
4453 /**
4454 Bison internal location value stack, yyls, when dynamically allocated using
4455 my_yyoverflow().
4456 */
4458
4459 /**
4460 Type of lock to be used for tables being added to the statement's
4461 table list in table_factor, table_alias_ref, single_multi and
4462 table_wild_one rules.
4463 Statements which use these rules but require lock type different
4464 from one specified by this member have to override it by using
4465 Query_block::set_lock_for_tables() method.
4466
4467 The default value of this member is TL_READ_DEFAULT. The only two
4468 cases in which we change it are:
4469 - When parsing SELECT HIGH_PRIORITY.
4470 - Rule for DELETE. In which we use this member to pass information
4471 about type of lock from delete to single_multi part of rule.
4473 We should try to avoid introducing new use cases as we would like
4474 to get rid of this member eventually.
4475 */
4477
4478 /**
4479 The type of requested metadata lock for tables added to
4480 the statement table list.
4481 */
4483
4484 /*
4485 TODO: move more attributes from the LEX structure here.
4486 */
4487};
4489/**
4490 Input parameters to the parser.
4491*/
4492struct Parser_input {
4493 /**
4494 True if the text parsed corresponds to an actual query,
4495 and not another text artifact.
4496 This flag is used to disable digest parsing of nested:
4497 - view definitions
4498 - table trigger definitions
4499 - table partition definitions
4500 - event scheduler event definitions
4501 */
4502 bool m_has_digest;
4503 /**
4504 True if the caller needs to compute a digest.
4505 This flag is used to request explicitly a digest computation,
4506 independently of the performance schema configuration.
4507 */
4508 bool m_compute_digest;
4509
4510 Parser_input() : m_has_digest(false), m_compute_digest(false) {}
4511};
4512
4513/**
4514 Internal state of the parser.
4515 The complete state consist of:
4516 - input parameters that control the parser behavior
4517 - state data used during lexical parsing,
4518 - state data used during syntactic parsing.
4519*/
4520class Parser_state {
4521 protected:
4522 /**
4523 Constructor for special parsers of partial SQL clauses (DD)
4524
4525 @param grammar_selector_token See Lex_input_stream::grammar_selector_token
4526 */
4527 explicit Parser_state(int grammar_selector_token)
4528 : m_input(), m_lip(grammar_selector_token), m_yacc(), m_comment(false) {}
4529
4530 public:
4531 Parser_state() : m_input(), m_lip(~0U), m_yacc(), m_comment(false) {}
4532
4533 /**
4534 Object initializer. Must be called before usage.
4536 @retval false OK
4537 @retval true Error
4538 */
4539 bool init(THD *thd, const char *buff, size_t length) {
4540 return m_lip.init(thd, buff, length);
4541 }
4542
4543 void reset(const char *found_semicolon, size_t length) {
4544 m_lip.reset(found_semicolon, length);
4546 }
4548 /// Signal that the current query has a comment
4549 void add_comment() { m_comment = true; }
4550 /// Check whether the current query has a comment
4551 bool has_comment() const { return m_comment; }
4553 public:
4557 /**
4558 Current performance digest instrumentation.
4561
4562 private:
4563 bool m_comment; ///< True if current query contains comments
4564};
4566/**
4567 Parser state for partition expression parser (.frm/DD stuff)
4568*/
4570 public:
4572
4574};
4576/**
4577 Parser state for generated column expression parser (.frm/DD stuff)
4578*/
4580 public:
4582
4584};
4586/**
4587 Parser state for single expression parser (.frm/DD stuff)
4588*/
4590 public:
4592
4593 Item *result;
4594};
4596/**
4597 Parser state for CTE subquery parser
4598*/
4600 public:
4602
4604};
4605
4607 Parser state for Derived table's condition parser.
4608 (Used in condition pushdown to derived tables)
4609*/
4611 public:
4615};
4616
4617struct st_lex_local : public LEX {
4618 static void *operator new(size_t size) noexcept {
4619 return (*THR_MALLOC)->Alloc(size);
4620 }
4621 static void *operator new(size_t size, MEM_ROOT *mem_root,
4622 const std::nothrow_t &arg
4623 [[maybe_unused]] = std::nothrow) noexcept {
4624 return mem_root->Alloc(size);
4625 }
4626 static void operator delete(void *ptr [[maybe_unused]],
4627 size_t size [[maybe_unused]]) {
4628 TRASH(ptr, size);
4629 }
4630 static void operator delete(
4631 void *, MEM_ROOT *, const std::nothrow_t &) noexcept { /* Never called */
4632 }
4633};
4634
4635extern bool lex_init(void);
4636extern void lex_free(void);
4637extern bool lex_start(THD *thd);
4638extern void lex_end(LEX *lex);
4639extern int MYSQLlex(union YYSTYPE *, struct YYLTYPE *, class THD *);
4640
4641extern void trim_whitespace(const CHARSET_INFO *cs, LEX_STRING *str);
4642
4643extern bool is_lex_native_function(const LEX_STRING *name);
4645bool is_keyword(const char *name, size_t len);
4646bool db_is_default_db(const char *db, size_t db_len, const THD *thd);
4647
4649
4650void print_derived_column_names(const THD *thd, String *str,
4652
4653/**
4654 @} (End of group GROUP_PARSER)
4655*/
4656
4657/**
4658 Check if the given string is invalid using the system charset.
4659
4660 @param string_val Reference to the string.
4661 @param charset_info Pointer to charset info.
4662
4663 @return true if the string has an invalid encoding using
4664 the system charset else false.
4665*/
4666
4667inline bool is_invalid_string(const LEX_CSTRING &string_val,
4668 const CHARSET_INFO *charset_info) {
4669 size_t valid_len;
4670 bool len_error;
4671
4672 if (validate_string(charset_info, string_val.str, string_val.length,
4673 &valid_len, &len_error)) {
4674 char hexbuf[7];
4675 octet2hex(
4676 hexbuf, string_val.str + valid_len,
4677 static_cast<uint>(std::min<size_t>(string_val.length - valid_len, 3)));
4678 my_error(ER_INVALID_CHARACTER_STRING, MYF(0), charset_info->csname, hexbuf);
4679 return true;
4680 }
4681 return false;
4682}
4683
4684/**
4685 Check if the given string is invalid using the system charset.
4686
4687 @param string_val Reference to the string.
4688 @param charset_info Pointer to charset info.
4689 @param[out] invalid_sub_str If string has an invalid encoding then invalid
4690 string in printable ASCII format is stored.
4691
4692 @return true if the string has an invalid encoding using
4693 the system charset else false.
4694*/
4695
4696inline bool is_invalid_string(const LEX_CSTRING &string_val,
4698 std::string &invalid_sub_str) {
4699 size_t valid_len;
4700 bool len_error;
4701
4702 if (validate_string(charset_info, string_val.str, string_val.length,
4703 &valid_len, &len_error)) {
4704 char printable_buff[32];
4706 printable_buff, sizeof(printable_buff), string_val.str + valid_len,
4707 static_cast<uint>(std::min<size_t>(string_val.length - valid_len, 3)),
4708 charset_info, 3);
4709 invalid_sub_str = printable_buff;
4710 return true;
4711 }
4712 return false;
4713}
4714
4715/**
4716 In debug mode, verify that we're not adding an item twice to the fields list
4717 with inconsistent hidden flags. Must be called before adding the item to
4718 fields.
4719 */
4721 [[maybe_unused]],
4722 Item *item [[maybe_unused]],
4723 bool hidden [[maybe_unused]]) {
4724#ifndef NDEBUG
4725 if (std::find(fields.begin(), fields.end(), item) != fields.end()) {
4726 // The item is already in the list, so we can't add it
4727 // with a different value for hidden.
4728 assert(item->hidden == hidden);
4729 }
4730#endif
4731}
4732
4733bool walk_item(Item *item, Select_lex_visitor *visitor);
4735bool accept_table(TABLE_LIST *t, Select_lex_visitor *visitor);
4737 Select_lex_visitor *visitor);
4738TABLE_LIST *nest_join(THD *thd, Query_block *select, TABLE_LIST *embedding,
4739 mem_root_deque<TABLE_LIST *> *jlist, size_t table_cnt,
4740 const char *legend);
4741#endif /* SQL_LEX_INCLUDED */
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:570
Data describing the table being created by CREATE TABLE or altered by ALTER TABLE.
Definition: sql_alter.h:203
Parser state for CTE subquery parser.
Definition: sql_lex.h:4595
Common_table_expr_parser_state()
Definition: sql_lex.cc:1126
PT_subquery * result
Definition: sql_lex.h:4599
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:4606
Item * result
Definition: sql_lex.h:4610
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:4585
Expression_parser_state()
Definition: sql_lex.cc:1123
Item * result
Definition: sql_lex.h:4589
Definition: field.h:573
Parser state for generated column expression parser (.frm/DD stuff)
Definition: sql_lex.h:4575
Value_generator * result
Definition: sql_lex.h:4579
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:2365
Definition: item_subselect.h:412
Definition: item.h:4033
Definition: item_func.h:3428
Definition: item_func.h:3479
This class is used to implement operations like SET @variable or @variable:= expression.
Definition: item_func.h:3238
A wrapper Item that normally returns its parameter, but becomes NULL when processing rows for rollup.
Definition: item_func.h:1602
A wrapper Item that contains a number of aggregate items, one for each level of rollup (see Item_roll...
Definition: item_sum.h:2699
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:3592
LEX_COLUMN(const String &x, const uint &y)
Definition: sql_lex.h:3596
uint rights
Definition: sql_lex.h:3595
String column
Definition: sql_lex.h:3594
Definition: sql_lex.h:3604
List< LEX_USER > * role_list
Definition: sql_lex.h:3613
void cleanup()
Definition: sql_lex.cc:5045
bool grant_as_used
Definition: sql_lex.h:3610
role_enum role_type
Definition: sql_lex.h:3611
LEX_USER * user
Definition: sql_lex.h:3612
LEX_GRANT_AS()
Definition: sql_lex.cc:5052
This class represents the character input stream consumed during lexical analysis.
Definition: sql_lex.h:3149
const char * m_end_of_query
End of the query text in the input stream, in the raw buffer.
Definition: sql_lex.h:3470
void restart_token()
Adjust the starting position of the current token.
Definition: sql_lex.h:3329
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:280
const int grammar_selector_token
The synthetic 1st token to prepend token stream with.
Definition: sql_lex.h:3587
void skip_binary(int n)
Skip binary from the input stream.
Definition: sql_lex.h:3193