MySQL  8.0.27
Source Code Documentation
sql_lex.h
Go to the documentation of this file.
1 /* Copyright (c) 2000, 2021, 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
64 #include "sql/dd/info_schema/table_stats.h" // dd::info_schema::Table_stati...
65 #include "sql/dd/info_schema/tablespace_stats.h" // dd::info_schema::Tablesp...
66 #include "sql/enum_query_type.h"
67 #include "sql/handler.h"
68 #include "sql/item.h" // Name_resolution_context
69 #include "sql/item_subselect.h" // Subquery_strategy
71 #include "sql/key_spec.h" // KEY_CREATE_INFO
72 #include "sql/mdl.h"
73 #include "sql/mem_root_array.h" // Mem_root_array
74 #include "sql/parse_tree_node_base.h" // enum_parsing_context
75 #include "sql/parser_yystype.h"
76 #include "sql/query_options.h" // OPTION_NO_CONST_TABLES
77 #include "sql/row_iterator.h"
78 #include "sql/set_var.h"
79 #include "sql/sql_array.h"
80 #include "sql/sql_connect.h" // USER_RESOURCES
81 #include "sql/sql_const.h"
82 #include "sql/sql_data_change.h" // enum_duplicates
83 #include "sql/sql_error.h" // warn_on_deprecated_charset
84 #include "sql/sql_list.h"
85 #include "sql/sql_plugin_ref.h"
86 #include "sql/sql_servers.h" // Server_options
87 #include "sql/sql_udf.h" // Item_udftype
88 #include "sql/table.h" // TABLE_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 
97 class Alter_info;
98 class Event_parse_data;
99 class Field;
100 class Item_cond;
102 class Item_func_match;
106 class Item_sum;
107 class JOIN;
108 class Opt_hints_global;
109 class Opt_hints_qb;
110 class PT_subquery;
111 class PT_with_clause;
112 class Parse_tree_root;
113 class Protocol;
114 class Query_result;
116 class Query_result_union;
117 class Query_block;
118 class Query_expression;
119 class Select_lex_visitor;
120 class Sql_cmd;
121 class THD;
122 class Value_generator;
123 class Window;
124 class partition_info;
125 class sp_head;
126 class sp_name;
127 class sp_pcontext;
128 struct LEX;
129 struct NESTED_JOIN;
130 struct PSI_digest_locker;
131 struct sql_digest_state;
132 union Lexer_yystype;
133 struct 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 */
175 extern 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 */
195 };
196 
200  SP_IS_SUID
201 };
202 
209 };
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 */
220 enum 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 
244 inline 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 
259 enum 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 */
290 class Table_function;
291 
292 class Table_ident {
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 */
315  table = EMPTY_CSTR;
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 {
381  } ssl,
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 
465  OLAP_TYPE
466 };
467 
468 /*
469  String names used to print a statement with index hints.
470  Keep in sync with index_hint_type.
471 */
472 extern 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 */
487 class Index_hint {
488  public:
489  /* The type of the hint : USE/FORCE/IGNORE */
490  enum index_hint_type type;
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  */
758  inline Query_block *global_parameters() const {
759  if (fake_query_block != nullptr)
760  return fake_query_block;
761  else if (saved_fake_query_block != nullptr)
762  return saved_fake_query_block;
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
835  Query_block *outer_query_block() const { return master; }
836 
837  /// @return the first query block inside this query expression
838  Query_block *first_query_block() const { return slave; }
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().
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);
971  void set_prepared() {
972  assert(!is_prepared());
973  prepared = true;
974  }
975  void set_optimized() {
976  assert(is_prepared() && !is_optimized());
977  optimized = true;
978  }
979  void set_executed() {
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
985  void reset_executed() {
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;
995  cleaned = UC_DIRTY;
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 
1011  inline bool is_union() const;
1012  bool union_needs_tmp_table(LEX *lex);
1013  /// @returns true if mixes UNION DISTINCT and UNION ALL
1014  bool mixed_union_operators() const;
1015 
1016  /// Include a query expression below a query block.
1017  void include_down(LEX *lex, Query_block *outer);
1018 
1019  /// Exclude this unit and immediately contained query_block objects
1020  void exclude_level();
1021 
1022  /// Exclude subtree of current unit from tree of SELECTs
1023  void exclude_tree(THD *thd);
1024 
1025  /// Renumber query blocks of a query expression according to supplied LEX
1026  void renumber_selects(LEX *lex);
1027 
1028  void restore_cmd_properties();
1029  bool save_cmd_properties(THD *thd);
1030 
1031  friend class Query_block;
1032 
1035  size_t num_visible_fields() const;
1036 
1037  // If we are doing a query with global LIMIT but without fake_query_block,
1038  // we need somewhere to store the record count for FOUND_ROWS().
1039  // It can't be in any of the JOINs, since they may have their own
1040  // LimitOffsetIterators, which will write to join->send_records
1041  // whenever there is an OFFSET. (It also can't be in saved_fake_query_block,
1042  // which has no join.) Thus, we'll keep it here instead.
1043  //
1044  // If we have a fake_query_block, we use its send_records instead
1045  // (since its LimitOffsetIterator will write there), and if we don't
1046  // have a UNION, FOUND_ROWS() refers to the (single) JOIN, and thus,
1047  // we use its send_records.
1049 
1050  enum_parsing_context get_explain_marker(const THD *thd) const;
1052  void set_explain_marker_from(THD *thd, const Query_expression *u);
1053 
1054 #ifndef NDEBUG
1055  /**
1056  Asserts that none of {this unit and its children units} is fully cleaned
1057  up.
1058  */
1059  void assert_not_fully_clean();
1060 #else
1061  void assert_not_fully_clean() {}
1062 #endif
1063  void invalidate();
1064 
1065  bool is_recursive() const { return first_recursive != nullptr; }
1066 
1068 
1070 
1071  void fix_after_pullout(Query_block *parent_query_block,
1072  Query_block *removed_query_block);
1073 
1074  /**
1075  If unit is a subquery, which forms an object of the upper level (an
1076  Item_subselect, a derived TABLE_LIST), adds to this object a map
1077  of tables of the upper level which the unit references.
1078  */
1080 
1081  /**
1082  If unit is a subquery, which forms an object of the upper level (an
1083  Item_subselect, a derived TABLE_LIST), returns the place of this object
1084  in the upper level query block.
1085  */
1086  enum_parsing_context place() const;
1087 
1088  bool walk(Item_processor processor, enum_walk walk, uchar *arg);
1089 
1090  /*
1091  An exception: this is the only function that needs to adjust
1092  explain_marker.
1093  */
1094  friend bool parse_view_definition(THD *thd, TABLE_LIST *view_ref);
1095 };
1096 
1099 
1100 /**
1101  Query_block type enum
1102 */
1103 enum class enum_explain_type {
1104  EXPLAIN_NONE = 0,
1109  EXPLAIN_UNION,
1112  // Total:
1113  EXPLAIN_total ///< fake type, total number of all valid types
1114 
1115  // Don't insert new types below this line!
1116 };
1117 
1118 /**
1119  This class represents a query block, aka a query specification, which is
1120  a query consisting of a SELECT keyword, followed by a table list,
1121  optionally followed by a WHERE clause, a GROUP BY, etc.
1122 */
1124  public:
1125  /**
1126  @note the group_by and order_by lists below will probably be added to the
1127  constructor when the parser is converted into a true bottom-up design.
1128 
1129  //SQL_I_LIST<ORDER> *group_by, SQL_I_LIST<ORDER> order_by
1130  */
1131  Query_block(MEM_ROOT *mem_root, Item *where, Item *having);
1132 
1133  Item *where_cond() const { return m_where_cond; }
1135  void set_where_cond(Item *cond) { m_where_cond = cond; }
1136  Item *having_cond() const { return m_having_cond; }
1138  void set_having_cond(Item *cond) { m_having_cond = cond; }
1141  bool change_query_result(THD *thd, Query_result_interceptor *new_result,
1142  Query_result_interceptor *old_result);
1143 
1144  /// Set base options for a query block (and active options too)
1145  void set_base_options(ulonglong options_arg) {
1146  DBUG_EXECUTE_IF("no_const_tables", options_arg |= OPTION_NO_CONST_TABLES;);
1147 
1148  // Make sure we do not overwrite options by accident
1149  assert(m_base_options == 0 && m_active_options == 0);
1150  m_base_options = options_arg;
1151  m_active_options = options_arg;
1152  }
1153 
1154  /// Add base options to a query block, also update active options
1156  assert(first_execution);
1159  }
1160 
1161  /**
1162  Remove base options from a query block.
1163  Active options are also updated, and we assume here that "extra" options
1164  cannot override removed base options.
1165  */
1167  assert(first_execution);
1168  m_base_options &= ~options;
1170  }
1171 
1172  /// Make active options from base options, supplied options and environment:
1173  void make_active_options(ulonglong added_options, ulonglong removed_options);
1174 
1175  /// Adjust the active option set
1177 
1178  /// @return the active query options
1180 
1181  /**
1182  Set associated tables as read_only, ie. they cannot be inserted into,
1183  updated or deleted from during this statement.
1184  Commonly used for query blocks that are part of derived tables or
1185  views that are materialized.
1186  */
1188  // Set all referenced base tables as read only.
1189  for (TABLE_LIST *tr = leaf_tables; tr != nullptr; tr = tr->next_leaf)
1190  tr->set_readonly();
1191  }
1192 
1193  /// @returns a map of all tables references in the query block
1194  table_map all_tables_map() const { return (1ULL << leaf_table_count) - 1; }
1195 
1196  void remove_derived(THD *thd, TABLE_LIST *tl);
1197  bool remove_aggregates(THD *thd, Query_block *select);
1198 
1202  Query_block *next_query_block() const { return next; }
1203 
1204  TABLE_LIST *find_table_by_name(const Table_ident *ident);
1205 
1206  /**
1207  @return true If STRAIGHT_JOIN applies to all tables.
1208  @return false Else.
1209  */
1211  bool straight_join = true;
1212  /// false for exmaple in t1 STRAIGHT_JOIN t2 JOIN t3.
1213  for (TABLE_LIST *tbl = leaf_tables->next_leaf; tbl; tbl = tbl->next_leaf)
1214  straight_join &= tbl->straight;
1215  return straight_join || (active_options() & SELECT_STRAIGHT_JOIN);
1216  }
1217 
1219  Query_block *mylast = this;
1220  for (; mylast->next_query_block(); mylast = mylast->next_query_block()) {
1221  }
1222  return mylast;
1223  }
1224 
1226 
1227  void mark_as_dependent(Query_block *last, bool aggregate);
1228 
1229  /// @return true if query block is explicitly grouped (non-empty GROUP BY)
1230  bool is_explicitly_grouped() const { return group_list.elements > 0; }
1231 
1232  /**
1233  @return true if this query block is implicitly grouped, ie it is not
1234  explicitly grouped but contains references to set functions.
1235  The query will return max. 1 row (@see also is_single_grouped()).
1236  */
1237  bool is_implicitly_grouped() const {
1238  return m_agg_func_used && group_list.elements == 0;
1239  }
1240 
1241  /**
1242  True if this query block is implicitly grouped.
1243 
1244  @note Not reliable before name resolution.
1245 
1246  @return true if this query block is implicitly grouped and returns exactly
1247  one row, which happens when it does not have a HAVING clause.
1248 
1249  @remark This function is currently unused.
1250  */
1251  bool is_single_grouped() const {
1252  return m_agg_func_used && group_list.elements == 0 &&
1253  m_having_cond == nullptr;
1254  }
1255 
1256  /**
1257  @return true if this query block is explicitly or implicitly grouped.
1258  @note a query with DISTINCT is not considered to be aggregated.
1259  @note in standard SQL, a query with HAVING is defined as grouped, however
1260  MySQL allows HAVING without any aggregation to be the same as WHERE.
1261  */
1262  bool is_grouped() const { return group_list.elements > 0 || m_agg_func_used; }
1263 
1264  /// @return true if this query block contains DISTINCT at start of select list
1265  bool is_distinct() const { return active_options() & SELECT_DISTINCT; }
1266 
1267  /**
1268  @return true if this query block contains an ORDER BY clause.
1269 
1270  @note returns false if ORDER BY has been eliminated, e.g if the query
1271  can return max. 1 row.
1272  */
1273  bool is_ordered() const { return order_list.elements > 0; }
1274 
1275  /**
1276  Based on the structure of the query at resolution time, it is possible to
1277  conclude that DISTINCT is useless and remove it.
1278  This is the case if:
1279  - all GROUP BY expressions are in SELECT list, so resulting group rows are
1280  distinct,
1281  - and ROLLUP is not specified, so it adds no row for NULLs.
1282 
1283  @returns true if we can remove DISTINCT.
1284 
1285  @todo could refine this to if ROLLUP were specified and all GROUP
1286  expressions were non-nullable, because ROLLUP then adds only NULL values.
1287  Currently, ROLLUP+DISTINCT is rejected because executor cannot handle
1288  it in all cases.
1289  */
1290  bool can_skip_distinct() const {
1291  return is_grouped() && hidden_group_field_count == 0 &&
1293  }
1294 
1295  /// @return true if this query block has a LIMIT clause
1296  bool has_limit() const { return select_limit != nullptr; }
1297 
1298  /// @return true if query block references full-text functions
1299  bool has_ft_funcs() const { return ftfunc_list->elements > 0; }
1300 
1301  /// @returns true if query block is a recursive member of a recursive unit
1302  bool is_recursive() const { return recursive_reference != nullptr; }
1303 
1304  bool is_in_select_list(Item *i);
1305 
1306  /**
1307  Finds a group expression matching the given item, or nullptr if
1308  none. When there are multiple candidates, ones that match in name are
1309  given priority (such that “a AS c GROUP BY a,b,c” resolves to c, not a);
1310  if there is still a tie, the leftmost is given priority.
1311 
1312  @param item The item to search for.
1313  @param [out] rollup_level If not nullptr, will be set to the group
1314  expression's index (0-based).
1315  */
1316  ORDER *find_in_group_list(Item *item, int *rollup_level) const;
1317  int group_list_size() const;
1318 
1319  /// @returns true if query block contains window functions
1320  bool has_windows() const { return m_windows.elements > 0; }
1321 
1322  void invalidate();
1323 
1324  uint get_in_sum_expr() const { return in_sum_expr; }
1325 
1326  bool add_item_to_list(Item *item);
1327  bool add_ftfunc_to_list(Item_func_match *func);
1328  void add_order_to_list(ORDER *order);
1329  TABLE_LIST *add_table_to_list(THD *thd, Table_ident *table, const char *alias,
1330  ulong table_options,
1332  enum_mdl_type mdl_type = MDL_SHARED_READ,
1333  List<Index_hint> *hints = nullptr,
1334  List<String> *partition_names = nullptr,
1335  LEX_STRING *option = nullptr,
1336  Parse_context *pc = nullptr);
1337 
1338  /**
1339  Add item to the hidden part of select list
1340 
1341  @param item item to add
1342 
1343  @return Pointer to reference of the added item
1344  */
1345  Item **add_hidden_item(Item *item);
1346 
1347  /// Remove hidden items from select list
1348  void remove_hidden_items();
1349 
1351  bool init_nested_join(THD *thd);
1353  TABLE_LIST *nest_last_join(THD *thd, size_t table_cnt = 2);
1354  bool add_joined_table(TABLE_LIST *table);
1356 
1357  /// Wrappers over fields / get_fields_list() that hide items where
1358  /// item->hidden, meant for range-based for loops. See sql/visible_fields.h.
1360  auto visible_fields() const { return VisibleFields(fields); }
1361 
1362  /// Check privileges for views that are merged into query block
1363  bool check_view_privileges(THD *thd, ulong want_privilege_first,
1364  ulong want_privilege_next);
1365  /// Check privileges for all columns referenced from query block
1366  bool check_column_privileges(THD *thd);
1367 
1368  /// Check privileges for column references in subqueries of a query block
1370 
1371  /// Resolve and prepare information about tables for one query block
1372  bool setup_tables(THD *thd, TABLE_LIST *tables, bool select_insert);
1373 
1374  /// Resolve OFFSET and LIMIT clauses
1375  bool resolve_limits(THD *thd);
1376 
1377  /// Resolve derived table, view, table function information for a query block
1378  bool resolve_placeholder_tables(THD *thd, bool apply_semijoin);
1379 
1380  /// Propagate exclusion from table uniqueness test into subqueries
1382 
1383  /// Merge name resolution context objects of a subquery into its parent
1384  void merge_contexts(Query_block *inner);
1385 
1386  /// Merge derived table into query block
1387  bool merge_derived(THD *thd, TABLE_LIST *derived_table);
1388 
1389  bool flatten_subqueries(THD *thd);
1390 
1391  /**
1392  Update available semijoin strategies for semijoin nests.
1393 
1394  Available semijoin strategies needs to be updated on every execution since
1395  optimizer_switch setting may have changed.
1396 
1397  @param thd Pointer to THD object for session.
1398  Used to access optimizer_switch
1399  */
1400  void update_semijoin_strategies(THD *thd);
1401 
1402  /**
1403  Returns which subquery execution strategies can be used for this query
1404  block.
1405 
1406  @param thd Pointer to THD object for session.
1407  Used to access optimizer_switch
1408 
1409  @retval SUBQ_MATERIALIZATION Subquery Materialization should be used
1410  @retval SUBQ_EXISTS In-to-exists execution should be used
1411  @retval CANDIDATE_FOR_IN2EXISTS_OR_MAT A cost-based decision should be made
1412  */
1413  Subquery_strategy subquery_strategy(const THD *thd) const;
1414 
1415  /**
1416  Returns whether semi-join is enabled for this query block
1417 
1418  @see @c Opt_hints_qb::semijoin_enabled for details on how hints
1419  affect this decision. If there are no hints for this query block,
1420  optimizer_switch setting determines whether semi-join is used.
1421 
1422  @param thd Pointer to THD object for session.
1423  Used to access optimizer_switch
1424 
1425  @return true if semijoin is enabled,
1426  false otherwise
1427  */
1428  bool semijoin_enabled(const THD *thd) const;
1429 
1431  sj_candidates = sj_cand;
1432  }
1433 
1434  bool has_sj_candidates() const {
1435  return sj_candidates != nullptr && !sj_candidates->empty();
1436  }
1437 
1438  /// Add full-text function elements from a list into this query block
1439  bool add_ftfunc_list(List<Item_func_match> *ftfuncs);
1440 
1441  void set_lock_for_table(const Lock_descriptor &descriptor, TABLE_LIST *table);
1442 
1443  void set_lock_for_tables(thr_lock_type lock_type);
1444 
1445  inline void init_order() {
1446  assert(order_list.elements == 0);
1447  order_list.elements = 0;
1448  order_list.first = nullptr;
1450  }
1451  /*
1452  This method created for reiniting LEX in mysql_admin_table() and can be
1453  used only if you are going remove all Query_block & units except belonger
1454  to LEX (LEX::unit & LEX::select, for other purposes use
1455  Query_expression::exclude_level()
1456  */
1457  void cut_subtree() { slave = nullptr; }
1458  bool test_limit();
1459  /**
1460  Get offset for LIMIT.
1461 
1462  Evaluate offset item if necessary.
1463 
1464  @return Number of rows to skip.
1465 
1466  @todo Integrate better with Query_expression::set_limit()
1467  */
1468  ha_rows get_offset(THD *thd);
1469  /**
1470  Get limit.
1471 
1472  Evaluate limit item if necessary.
1473 
1474  @return Limit of rows in result.
1475 
1476  @todo Integrate better with Query_expression::set_limit()
1477  */
1478  ha_rows get_limit(THD *thd);
1479 
1480  /// Assign a default name resolution object for this query block.
1481  bool set_context(Name_resolution_context *outer_context);
1482 
1483  /// Setup the array containing references to base items
1484  bool setup_base_ref_items(THD *thd);
1485  void print(const THD *thd, String *str, enum_query_type query_type);
1486 
1487  /**
1488  Print detail of the Query_block object.
1489 
1490  @param thd Thread handler
1491  @param query_type Options to print out string output
1492  @param[out] str String of output.
1493  */
1494  void print_query_block(const THD *thd, String *str,
1495  enum_query_type query_type);
1496 
1497  /**
1498  Print detail of the UPDATE statement.
1499 
1500  @param thd Thread handler
1501  @param[out] str String of output
1502  @param query_type Options to print out string output
1503  */
1504  void print_update(const THD *thd, String *str, enum_query_type query_type);
1505 
1506  /**
1507  Print detail of the DELETE statement.
1508 
1509  @param thd Thread handler
1510  @param[out] str String of output
1511  @param query_type Options to print out string output
1512  */
1513  void print_delete(const THD *thd, String *str, enum_query_type query_type);
1514 
1515  /**
1516  Print detail of the INSERT statement.
1517 
1518  @param thd Thread handler
1519  @param[out] str String of output
1520  @param query_type Options to print out string output
1521  */
1522  void print_insert(const THD *thd, String *str, enum_query_type query_type);
1523 
1524  /**
1525  Print detail of Hints.
1526 
1527  @param thd Thread handler
1528  @param[out] str String of output
1529  @param query_type Options to print out string output
1530  */
1531  void print_hints(const THD *thd, String *str, enum_query_type query_type);
1532 
1533  /**
1534  Print error.
1535 
1536  @param thd Thread handler
1537  @param[out] str String of output
1538 
1539  @retval false If there is no error
1540  @retval true else
1541  */
1542  bool print_error(const THD *thd, String *str);
1543 
1544  /**
1545  Print select options.
1546 
1547  @param[out] str String of output
1548  */
1550 
1551  /**
1552  Print UPDATE options.
1553 
1554  @param[out] str String of output
1555  */
1557 
1558  /**
1559  Print DELETE options.
1560 
1561  @param[out] str String of output
1562  */
1564 
1565  /**
1566  Print INSERT options.
1567 
1568  @param[out] str String of output
1569  */
1571 
1572  /**
1573  Print list of tables.
1574 
1575  @param thd Thread handler
1576  @param[out] str String of output
1577  @param table_list TABLE_LIST object
1578  @param query_type Options to print out string output
1579  */
1580  void print_table_references(const THD *thd, String *str,
1582  enum_query_type query_type);
1583 
1584  /**
1585  Print list of items in Query_block object.
1586 
1587  @param thd Thread handle
1588  @param[out] str String of output
1589  @param query_type Options to print out string output
1590  */
1591  void print_item_list(const THD *thd, String *str, enum_query_type query_type);
1592 
1593  /**
1594  Print assignments list. Used in UPDATE and
1595  INSERT ... ON DUPLICATE KEY UPDATE ...
1596 
1597  @param thd Thread handle
1598  @param[out] str String of output
1599  @param query_type Options to print out string output
1600  @param fields List columns to be assigned.
1601  @param values List of values.
1602  */
1603  void print_update_list(const THD *thd, String *str,
1604  enum_query_type query_type,
1606  const mem_root_deque<Item *> &values);
1607 
1608  /**
1609  Print column list to be inserted into. Used in INSERT.
1610 
1611  @param thd Thread handle
1612  @param[out] str String of output
1613  @param query_type Options to print out string output
1614  */
1615  void print_insert_fields(const THD *thd, String *str,
1616  enum_query_type query_type);
1617 
1618  /**
1619  Print list of values, used in INSERT and for general VALUES clause.
1620 
1621  @param thd Thread handle
1622  @param[out] str String of output
1623  @param query_type Options to print out string output
1624  @param values List of values
1625  @param prefix Prefix to print before each row in value list
1626  = nullptr: No prefix wanted
1627  */
1628  void print_values(const THD *thd, String *str, enum_query_type query_type,
1629  const mem_root_deque<mem_root_deque<Item *> *> &values,
1630  const char *prefix);
1631 
1632  /**
1633  Print list of tables in FROM clause.
1634 
1635  @param thd Thread handler
1636  @param[out] str String of output
1637  @param query_type Options to print out string output
1638  */
1639  void print_from_clause(const THD *thd, String *str,
1640  enum_query_type query_type);
1641 
1642  /**
1643  Print list of conditions in WHERE clause.
1644 
1645  @param thd Thread handle
1646  @param[out] str String of output
1647  @param query_type Options to print out string output
1648  */
1649  void print_where_cond(const THD *thd, String *str,
1650  enum_query_type query_type);
1651 
1652  /**
1653  Print list of items in GROUP BY clause.
1654 
1655  @param thd Thread handle
1656  @param[out] str String of output
1657  @param query_type Options to print out string output
1658  */
1659  void print_group_by(const THD *thd, String *str, enum_query_type query_type);
1660 
1661  /**
1662  Print list of items in HAVING clause.
1663 
1664  @param thd Thread handle
1665  @param[out] str String of output
1666  @param query_type Options to print out string output
1667  */
1668  void print_having(const THD *thd, String *str, enum_query_type query_type);
1669 
1670  /**
1671  Print details of Windowing functions.
1672 
1673  @param thd Thread handler
1674  @param[out] str String of output
1675  @param query_type Options to print out string output
1676  */
1677  void print_windows(const THD *thd, String *str, enum_query_type query_type);
1678 
1679  /**
1680  Print list of items in ORDER BY clause.
1681 
1682  @param thd Thread handle
1683  @param[out] str String of output
1684  @param query_type Options to print out string output
1685  */
1686  void print_order_by(const THD *thd, String *str, enum_query_type query_type);
1687 
1688  static void print_order(const THD *thd, String *str, ORDER *order,
1689  enum_query_type query_type);
1690  void print_limit(const THD *thd, String *str, enum_query_type query_type);
1691  bool save_properties(THD *thd);
1692 
1693  /**
1694  Accept function for SELECT and DELETE.
1695 
1696  @param visitor Select_lex_visitor Object
1697  */
1698  bool accept(Select_lex_visitor *visitor);
1699 
1700  /**
1701  Cleanup this subtree (this Query_block and all nested Query_blockes and
1702  Query_expressions).
1703  @param thd thread handle
1704  @param full if false only partial cleanup is done, JOINs and JOIN_TABs are
1705  kept to provide info for EXPLAIN CONNECTION; if true, complete cleanup is
1706  done, all JOINs are freed.
1707  */
1708  void cleanup(THD *thd, bool full);
1709  /*
1710  Recursively cleanup the join of this select lex and of all nested
1711  select lexes. This is not a full cleanup.
1712  */
1713  void cleanup_all_joins();
1714  /**
1715  Destroy contained objects, in particular temporary tables which may
1716  have their own mem_roots.
1717  */
1718  void destroy();
1719 
1720  /// Return true if this query block is part of a UNION
1721  bool is_part_of_union() const {
1722  return master_query_expression()->is_union();
1723  }
1724 
1725  /**
1726  @return true if query block is found during preparation to produce no data.
1727  Notice that if query is implicitly grouped, an aggregation row will
1728  still be returned.
1729  */
1730  bool is_empty_query() const { return m_empty_query; }
1731 
1732  /// Set query block as returning no data
1733  /// @todo This may also be set when we have an always false WHERE clause
1735  assert(join == nullptr);
1736  m_empty_query = true;
1737  }
1738  /*
1739  For MODE_ONLY_FULL_GROUP_BY we need to know if
1740  this query block is the aggregation query of at least one aggregate
1741  function.
1742  */
1743  bool agg_func_used() const { return m_agg_func_used; }
1744  bool json_agg_func_used() const { return m_json_agg_func_used; }
1745 
1746  void set_agg_func_used(bool val) { m_agg_func_used = val; }
1747 
1749 
1750  bool right_joins() const { return m_right_joins; }
1751  void set_right_joins() { m_right_joins = true; }
1752 
1753  /// Lookup for Query_block type
1755 
1756  /// Lookup for a type string
1757  const char *get_type_str() { return type_str[static_cast<int>(type())]; }
1758  static const char *get_type_str(enum_explain_type type) {
1759  return type_str[static_cast<int>(type)];
1760  }
1761 
1763  bool is_cacheable() const { return !uncacheable; }
1764 
1765  /// @returns true if this query block outputs at most one row.
1767  return (table_list.size() == 0 &&
1769  }
1770 
1771  /// Include query block inside a query expression.
1772  void include_down(LEX *lex, Query_expression *outer);
1773 
1774  /// Include a query block next to another query block.
1775  void include_neighbour(LEX *lex, Query_block *before);
1776 
1777  /// Include query block inside a query expression, but do not link.
1779 
1780  /// Include query block into global list.
1781  void include_in_global(Query_block **plink);
1782 
1783  /// Include chain of query blocks into global list.
1785 
1786  /// Renumber query blocks of contained query expressions
1787  void renumber(LEX *lex);
1788 
1789  /**
1790  Does permanent transformations which are local to a query block (which do
1791  not merge it to another block).
1792  */
1793  bool apply_local_transforms(THD *thd, bool prune);
1794 
1795  /// Pushes parts of the WHERE condition of this query block to materialized
1796  /// derived tables.
1798 
1799  bool get_optimizable_conditions(THD *thd, Item **new_where,
1800  Item **new_having);
1801 
1802  bool validate_outermost_option(LEX *lex, const char *wrong_option) const;
1803  bool validate_base_options(LEX *lex, ulonglong options) const;
1804 
1805  bool walk(Item_processor processor, enum_walk walk, uchar *arg);
1806 
1807  bool add_tables(THD *thd, const Mem_root_array<Table_ident *> *tables,
1808  ulong table_options, thr_lock_type lock_type,
1809  enum_mdl_type mdl_type);
1810 
1811  bool resolve_rollup_wfs(THD *thd);
1812 
1813  bool setup_conds(THD *thd);
1814  bool prepare(THD *thd, mem_root_deque<Item *> *insert_field_list);
1815  bool optimize(THD *thd, bool finalize_access_paths);
1817 
1818  bool change_group_ref_for_func(THD *thd, Item *func, bool *changed);
1819  bool change_group_ref_for_cond(THD *thd, Item_cond *cond, bool *changed);
1820 
1821  // If the query block has exactly one single visible field, returns it.
1822  // If not, returns nullptr.
1823  Item *single_visible_field() const;
1824  size_t num_visible_fields() const;
1825 
1826  // Whether the SELECT list is empty (hidden fields are ignored).
1827  // Typically used to distinguish INSERT INTO ... SELECT queries
1828  // from INSERT INTO ... VALUES queries.
1829  bool field_list_is_empty() const;
1830 
1831  void remove_hidden_fields();
1832 
1833  // ************************************************
1834  // * Members (most of these should not be public) *
1835  // ************************************************
1836 
1838  /**
1839  All expressions needed after join and filtering, ie., select list,
1840  group by list, having clause, window clause, order by clause,
1841  including hidden fields.
1842  Does not include join conditions nor where clause.
1843 
1844  This should ideally be changed into Mem_root_array<Item *>, but
1845  find_order_in_list() depends on pointer stability (it stores a pointer
1846  to an element in referenced_by[]). Similarly, there are some instances
1847  of thd->change_item_tree() that store pointers to elements in this list.
1848 
1849  Because of this, adding or removing elements in the middle is not allowed;
1850  std::deque guarantees pointer stability only in the face of adding
1851  or removing elements from either end, ie., {push,pop}_{front_back}.
1852 
1853  Currently, all hidden items must be before all visible items.
1854  This is primarily due to the requirement for pointer stability
1855  (remove_hidden_fields() runs during cleanup), but also because
1856  change_to_use_tmp_fields() depends on it when mapping items to
1857  ref_item_array indexes. It would be good to get rid of this
1858  requirement in the future.
1859  */
1861 
1862  /**
1863  All windows defined on the select, both named and inlined
1864  */
1866 
1867  /**
1868  Usually a pointer to ftfunc_list_alloc, but in UNION this is used to create
1869  fake query_block that consolidates result fields of UNION
1870  */
1873 
1874  /// The VALUES items of a table value constructor.
1876 
1877  /// List of semi-join nests generated for this query block
1879 
1880  /// List of tables in FROM clause - use TABLE_LIST::next_local to traverse
1882 
1883  /**
1884  ORDER BY clause.
1885  This list may be mutated during optimization (by remove_const()),
1886  so for prepared statements, we keep a copy of the ORDER.next pointers in
1887  order_list_ptrs, and re-establish the original list before each execution.
1888  */
1891 
1892  /**
1893  GROUP BY clause.
1894  This list may be mutated during optimization (by remove_const()),
1895  so for prepared statements, we keep a copy of the ORDER.next pointers in
1896  group_list_ptrs, and re-establish the original list before each execution.
1897  */
1900 
1901  // Used so that AggregateIterator knows which items to signal when the rollup
1902  // level changes. Obviously only used in the presence of rollup.
1907 
1908  /// Query-block-level hints, for this query block
1910 
1911  char *db{nullptr};
1912 
1913  /**
1914  If this query block is a recursive member of a recursive unit: the
1915  TABLE_LIST, in this recursive member, referencing the query
1916  name.
1917  */
1919 
1920  /// Reference to LEX that this query block belongs to
1921  LEX *parent_lex{nullptr};
1922 
1923  /**
1924  The set of those tables whose fields are referenced in the select list of
1925  this select level.
1926  */
1928  table_map outer_join{0}; ///< Bitmap of all inner tables from outer joins
1929 
1930  /**
1931  Context for name resolution for all column references except columns
1932  from joined tables.
1933  */
1935 
1936  /**
1937  Pointer to first object in list of Name res context objects that have
1938  this query block as the base query block.
1939  Includes field "context" which is embedded in this query block.
1940  */
1942 
1943  /**
1944  After optimization it is pointer to corresponding JOIN. This member
1945  should be changed only when THD::LOCK_query_plan mutex is taken.
1946  */
1947  JOIN *join{nullptr};
1948  /// join list of the top level
1950  /// list for the currently parsed join
1952  /// table embedding the above list
1954  /**
1955  Points to first leaf table of query block. After setup_tables() is done,
1956  this is a list of base tables and derived tables. After derived tables
1957  processing is done, this is a list of base tables only.
1958  Use TABLE_LIST::next_leaf to traverse the list.
1959  */
1961  // Last table for LATERAL join, used by table functions
1963 
1964  /// LIMIT clause, NULL if no limit is given
1965  Item *select_limit{nullptr};
1966  /// LIMIT ... OFFSET clause, NULL if no offset is given
1967  Item *offset_limit{nullptr};
1968 
1969  /**
1970  Circular linked list of aggregate functions in nested query blocks.
1971  This is needed if said aggregate functions depend on outer values
1972  from this query block; if so, we want to add them as hidden items
1973  in our own field list, to be able to evaluate them.
1974  @see Item_sum::check_sum_func
1975  */
1977 
1978  /**
1979  Array of pointers to "base" items; one each for every selected expression
1980  and referenced item in the query block. All references to fields are to
1981  buffers associated with the primary input tables.
1982  */
1984 
1985  uint select_number{0}; ///< Query block number (used for EXPLAIN)
1986 
1987  /**
1988  Saved values of the WHERE and HAVING clauses. Allowed values are:
1989  - COND_UNDEF if the condition was not specified in the query or if it
1990  has not been optimized yet
1991  - COND_TRUE if the condition is always true
1992  - COND_FALSE if the condition is impossible
1993  - COND_OK otherwise
1994  */
1997 
1998  /// Parse context: indicates where the current expression is being parsed
2000  /// Parse context: is inside a set function if this is positive
2002 
2003  /**
2004  Three fields used by semi-join transformations to know when semi-join is
2005  possible, and in which condition tree the subquery predicate is located.
2006  */
2013  };
2015  RESOLVE_NONE}; ///< Indicates part of query being resolved
2016 
2017  /**
2018  Number of fields used in select list or where clause of current select
2019  and all inner subselects.
2020  */
2022  /**
2023  number of items in select_list and HAVING clause used to get number
2024  bigger then can be number of entries that will be added to all item
2025  list during split_sum_func
2026  */
2028  uint cond_count{0}; ///< number of arguments of and/or/xor in where/having/on
2029  uint between_count{0}; ///< number of between predicates in where/having/on
2031  0}; ///< maximal number of elements in multiple equalities
2032 
2033  /**
2034  Number of Item_sum-derived objects in this SELECT. Keeps count of
2035  aggregate functions and window functions(to allocate items in ref array).
2036  See Query_block::setup_base_ref_items.
2037  */
2039  /// Number of Item_sum-derived objects in children and descendant SELECTs
2041 
2042  /// Keep track for allocation of base_ref_items: scalar subqueries may be
2043  /// replaced by a field during scalar_to_derived transformation
2045 
2046  /// Number of materialized derived tables and views in this query block.
2048  /// Number of partitioned tables
2050 
2051  /**
2052  Number of wildcards used in the SELECT list. For example,
2053  SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
2054  has 3 wildcards.
2055  */
2057 
2058  /// Number of leaf tables in this query block.
2060  /// Number of derived tables and views in this query block.
2062  /// Number of table functions in this query block
2064 
2065  /**
2066  Nesting level of query block, outer-most query block has level 0,
2067  its subqueries have level 1, etc. @see also sql/item_sum.h.
2068  */
2069  int nest_level{0};
2070 
2071  /// Indicates whether this query block contains the WITH ROLLUP clause
2073 
2074  /// @see enum_condition_context
2076 
2077  /// If set, the query block is of the form VALUES row_list.
2079 
2080  /// Describes context of this query block (e.g if it is a derived table).
2082 
2083  /**
2084  result of this query can't be cached, bit field, can be :
2085  UNCACHEABLE_DEPENDENT
2086  UNCACHEABLE_RAND
2087  UNCACHEABLE_SIDEEFFECT
2088  */
2090 
2091  void update_used_tables();
2092  void restore_cmd_properties();
2093  bool save_cmd_properties(THD *thd);
2094 
2095  /**
2096  This variable is required to ensure proper work of subqueries and
2097  stored procedures. Generally, one should use the states of
2098  Query_arena to determine if it's a statement prepare or first
2099  execution of a stored procedure. However, in case when there was an
2100  error during the first execution of a stored procedure, the SP body
2101  is not expelled from the SP cache. Therefore, a deeply nested
2102  subquery might be left unoptimized. So we need this per-subquery
2103  variable to inidicate the optimization/execution state of every
2104  subquery. Prepared statements work OK in that regard, as in
2105  case of an error during prepare the PS is not created.
2106  */
2107  bool first_execution{true};
2108 
2109  /// True when semi-join pull-out processing is complete
2110  bool sj_pullout_done{false};
2111 
2112  /// Used by nested scalar_to_derived transformations
2114 
2115  /// True: skip local transformations during prepare() call (used by INSERT)
2117 
2118  bool is_item_list_lookup{false};
2119 
2120  /// true when having fix field called in processing of this query block
2121  bool having_fix_field{false};
2122  /// true when GROUP BY fix field called in processing of this query block
2123  bool group_fix_field{false};
2124 
2125  /**
2126  True if contains or aggregates set functions.
2127  @note this is wrong when a locally found set function is aggregated
2128  in an outer query block.
2129  */
2130  bool with_sum_func{false};
2131 
2132  /**
2133  HAVING clause contains subquery => we can't close tables before
2134  query processing end even if we use temporary table
2135  */
2136  bool subquery_in_having{false};
2137 
2138  /**
2139  If true, use select_limit to limit number of rows selected.
2140  Applicable when no explicit limit is supplied, and only for the
2141  outermost query block of a SELECT statement.
2142  */
2143  bool m_use_select_limit{false};
2144 
2145  /// If true, limit object is added internally
2146  bool m_internal_limit{false};
2147 
2148  /// exclude this query block from unique_table() check
2150 
2151  bool no_table_names_allowed{false}; ///< used for global order by
2152 
2153  /// Hidden items added during optimization
2154  /// @note that using this means we modify resolved data during optimization
2156 
2157  private:
2158  friend class Query_expression;
2159  friend class Condition_context;
2160 
2161  /// Helper for save_properties()
2162  bool save_order_properties(THD *thd, SQL_I_List<ORDER> *list,
2163  Group_list_ptrs **list_ptrs);
2164 
2167  bool top, bool in_sj, Item **new_conds,
2168  uint *changelog = nullptr);
2169  /// Remove semijoin condition for this query block
2170  void clear_sj_expressions(NESTED_JOIN *nested_join);
2171  /// Build semijoin condition for th query block
2172  bool build_sj_cond(THD *thd, NESTED_JOIN *nested_join,
2173  Query_block *subq_query_block, table_map outer_tables_map,
2174  Item **sj_cond);
2176  TABLE_LIST *join_nest);
2177 
2180  Item *join_cond, bool left_outer,
2181  bool use_inner_join);
2182  bool transform_subquery_to_derived(THD *thd, TABLE_LIST **out_tl,
2183  Query_expression *subs_query_expression,
2184  Item_subselect *subq, bool use_inner_join,
2185  bool reject_multiple_rows,
2186  Item *join_condition,
2187  Item *lifted_where_cond);
2189  THD *thd, Item_exists_subselect *subq_pred);
2191  THD *thd, TABLE_LIST *derived, Item *lifted_where,
2192  Lifted_fields_map *lifted_where_fields, bool *added_card_check);
2194  THD *thd, TABLE_LIST *derived, Lifted_fields_map *lifted_where_fields,
2195  bool added_card_check);
2196  void remap_tables(THD *thd);
2197  bool resolve_subquery(THD *thd);
2199  Item *resolve_rollup_item(THD *thd, Item *item);
2200  bool resolve_rollup(THD *thd);
2201 
2202  bool setup_wild(THD *thd);
2203  bool setup_order_final(THD *thd);
2204  bool setup_group(THD *thd);
2205  void fix_after_pullout(Query_block *parent_query_block,
2206  Query_block *removed_query_block);
2210  void empty_order_list(Query_block *sl);
2212  bool in_update);
2213  bool find_common_table_expr(THD *thd, Table_ident *table_id, TABLE_LIST *tl,
2214  Parse_context *pc, bool *found);
2215  /**
2216  Transform eligible scalar subqueries in the SELECT list, WHERE condition,
2217  HAVING condition or JOIN conditions of a query block[*] to an equivalent
2218  derived table of a LEFT OUTER join, e.g. as shown in this uncorrelated
2219  subquery:
2220 
2221  [*] a.k.a "transformed query block" throughout this method and its minions.
2222 
2223  <pre>
2224  SELECT * FROM t1
2225  WHERE t1.a > (SELECT COUNT(a) AS cnt FROM t2); ->
2226 
2227  SELECT t1.* FROM t1 LEFT OUTER JOIN
2228  (SELECT COUNT(a) AS cnt FROM t2) AS derived
2229  ON TRUE WHERE t1.a > derived.cnt;
2230  </pre>
2231 
2232  Grouping in the transformed query block may necessitate the grouping to be
2233  moved down to another derived table, cf. transform_grouped_to_derived.
2234 
2235  Limitations:
2236  - only implicitly grouped subqueries (guaranteed to have cardinality one)
2237  are identified as scalar subqueries.
2238  _ Correlated subqueries are not handled
2239 
2240  @param[in,out] thd the session context
2241  @returns true on error
2242  */
2245  Item **lifted_where);
2246  bool transform_grouped_to_derived(THD *thd, bool *break_off);
2247  bool replace_subquery_in_expr(THD *thd, Item::Css_info *subquery,
2248  TABLE_LIST *tr, Item **expr);
2249  bool nest_derived(THD *thd, Item *join_cond,
2251  TABLE_LIST *new_derived_table);
2252 
2254 
2255  // Delete unused columns from merged derived tables
2257 
2258  /// Helper for fix_prepare_information()
2260  Group_list_ptrs **list_ptrs);
2261 
2262  bool prepare_values(THD *thd);
2263  bool check_only_full_group_by(THD *thd);
2265 
2266  //
2267  // Members:
2268  //
2269 
2270  /**
2271  Pointer to collection of subqueries candidate for semi/antijoin
2272  conversion.
2273  Template parameter is "true": no need to run DTORs on pointers.
2274  */
2276 
2277  /// How many expressions are part of the order by but not select list.
2279 
2280  /**
2281  Intrusive double-linked list of all query blocks within the same
2282  query expression.
2283  */
2284  Query_block *next{nullptr};
2285  Query_block **prev{nullptr};
2286 
2287  /// The query expression containing this query block.
2289  /// The first query expression contained within this query block.
2291 
2292  /// Intrusive double-linked global list of query blocks.
2295 
2296  /// Result of this query block
2298 
2299  /**
2300  Options assigned from parsing and throughout resolving,
2301  should not be modified after resolving is done.
2302  */
2304  /**
2305  Active options. Derived from base options, modifiers added during
2306  resolving and values from session variable option_bits. Since the latter
2307  may change, active options are refreshed per execution of a statement.
2308  */
2310 
2312  nullptr}; ///< Used when resolving outer join condition
2313 
2314  /**
2315  Condition to be evaluated after all tables in a query block are joined.
2316  After all permanent transformations have been conducted by
2317  Query_block::prepare(), this condition is "frozen", any subsequent changes
2318  to it must be done with change_item_tree(), unless they only modify AND/OR
2319  items and use a copy created by Query_block::get_optimizable_conditions().
2320  Same is true for 'having_cond'.
2321  */
2323 
2324  /// Condition to be evaluated on grouped rows after grouping.
2326 
2327  /// Number of GROUP BY expressions added to all_fields
2329 
2330  /**
2331  True if query block has semi-join nests merged into it. Notice that this
2332  is updated earlier than sj_nests, so check this if info is needed
2333  before the full resolver process is complete.
2334  */
2335  bool has_sj_nests{false};
2336  bool has_aj_nests{false}; ///< @see has_sj_nests; counts antijoin nests.
2337  bool m_right_joins{false}; ///< True if query block has right joins
2338 
2339  /// Allow merge of immediate unnamed derived tables
2341 
2342  bool m_agg_func_used{false};
2344 
2345  /**
2346  True if query block does not generate any rows before aggregation,
2347  determined during preparation (not optimization).
2348  */
2349  bool m_empty_query{false};
2350 
2351  static const char
2353 };
2354 
2355 inline bool Query_expression::is_union() const {
2356  return first_query_block()->next_query_block() &&
2358 }
2359 
2360 /// Utility RAII class to save/modify/restore the condition_context information
2361 /// of a query block. @see enum_condition_context.
2363  public:
2365  Query_block *select_ptr,
2367  : select(nullptr), saved_value() {
2368  if (select_ptr) {
2369  select = select_ptr;
2371  // More restrictive wins over less restrictive:
2372  if (new_type == enum_condition_context::NEITHER ||
2373  (new_type == enum_condition_context::ANDS_ORS &&
2375  select->condition_context = new_type;
2376  }
2377  }
2380  }
2381 
2382  private:
2385 };
2386 
2388  std::function<bool(TABLE_LIST *)> action);
2389 
2390 /**
2391  Base class for secondary engine execution context objects. Secondary
2392  storage engines may create classes derived from this one which
2393  contain state they need to preserve between optimization and
2394  execution of statements. The context objects should be allocated on
2395  the execution MEM_ROOT.
2396 */
2398  public:
2399  /**
2400  Destructs the secondary engine execution context object. It is
2401  called after the query execution has completed. Secondary engines
2402  may override the destructor in subclasses and add code that
2403  performs cleanup tasks that are needed after query execution.
2404  */
2406 };
2407 
2408 typedef struct struct_slave_connection {
2409  char *user;
2410  char *password;
2412  char *plugin_dir;
2413 
2414  void reset();
2416 
2420  bool detistic;
2422 };
2423 
2424 extern const LEX_STRING null_lex_str;
2425 
2429 
2430  /**
2431  FOLLOWS or PRECEDES as specified in the CREATE TRIGGER statement.
2432  */
2434 
2435  /**
2436  Trigger name referenced in the FOLLOWS/PRECEDES clause of the CREATE TRIGGER
2437  statement.
2438  */
2440 };
2441 
2443 
2444 class Sroutine_hash_entry;
2445 
2446 /*
2447  Class representing list of all tables used by statement and other
2448  information which is necessary for opening and locking its tables,
2449  like SQL command for this statement.
2450 
2451  Also contains information about stored functions used by statement
2452  since during its execution we may have to add all tables used by its
2453  stored functions/triggers to this list in order to pre-open and lock
2454  them.
2455 
2456  Also used by LEX::reset_n_backup/restore_backup_query_tables_list()
2457  methods to save and restore this information.
2458 */
2459 
2461  public:
2463 
2464  /**
2465  SQL command for this statement. Part of this class since the
2466  process of opening and locking tables for the statement needs
2467  this information to determine correct type of lock for some of
2468  the tables.
2469  */
2471  /* Global list of all tables used by this statement */
2473  /* Pointer to next_global member of last element in the previous list. */
2475  /*
2476  If non-0 then indicates that query requires prelocking and points to
2477  next_global member of last own element in query table list (i.e. last
2478  table which was not added to it as part of preparation to prelocking).
2479  0 - indicates that this query does not need prelocking.
2480  */
2482  /*
2483  Set of stored routines called by statement.
2484  (Note that we use lazy-initialization for this hash).
2485 
2486  See Sroutine_hash_entry for explanation why this hash uses binary
2487  key comparison.
2488  */
2490  std::unique_ptr<malloc_unordered_map<std::string, Sroutine_hash_entry *>>
2492  /*
2493  List linking elements of 'sroutines' set. Allows you to add new elements
2494  to this set as you iterate through the list of existing elements.
2495  'sroutines_list_own_last' is pointer to ::next member of last element of
2496  this list which represents routine which is explicitly used by query.
2497  'sroutines_list_own_elements' number of explicitly used routines.
2498  We use these two members for restoring of 'sroutines_list' to the state
2499  in which it was right after query parsing.
2500  */
2504 
2505  /**
2506  Locking state of tables in this particular statement.
2507 
2508  If we under LOCK TABLES or in prelocked mode we consider tables
2509  for the statement to be "locked" if there was a call to lock_tables()
2510  (which called handler::start_stmt()) for tables of this statement
2511  and there was no matching close_thread_tables() call.
2512 
2513  As result this state may differ significantly from one represented
2514  by Open_tables_state::lock/locked_tables_mode more, which are always
2515  "on" under LOCK TABLES or in prelocked mode.
2516  */
2519  bool is_query_tables_locked() const {
2520  return (lock_tables_state == LTS_LOCKED);
2521  }
2522 
2523  /**
2524  Number of tables which were open by open_tables() and to be locked
2525  by lock_tables().
2526  Note that we set this member only in some cases, when this value
2527  needs to be passed from open_tables() to lock_tables() which are
2528  separated by some amount of code.
2529  */
2531 
2532  /*
2533  These constructor and destructor serve for creation/destruction
2534  of Query_tables_list instances which are used as backup storage.
2535  */
2536  Query_tables_list() = default;
2537  ~Query_tables_list() = default;
2538 
2539  /* Initializes (or resets) Query_tables_list object for "real" use. */
2540  void reset_query_tables_list(bool init);
2543  *this = std::move(*state);
2544  }
2545 
2546  /*
2547  Direct addition to the list of query tables.
2548  If you are using this function, you must ensure that the table
2549  object, in particular table->db member, is initialized.
2550  */
2552  *(table->prev_global = query_tables_last) = table;
2553  query_tables_last = &table->next_global;
2554  }
2556  void mark_as_requiring_prelocking(TABLE_LIST **tables_own_last) {
2557  query_tables_own_last = tables_own_last;
2558  }
2559  /* Return pointer to first not-own table in query-tables or 0 */
2561  return (query_tables_own_last ? *query_tables_own_last : nullptr);
2562  }
2564  if (query_tables_own_last) {
2565  *query_tables_own_last = nullptr;
2567  query_tables_own_last = nullptr;
2568  }
2569  }
2570 
2571  /**
2572  All types of unsafe statements.
2573 
2574  @note The int values of the enum elements are used to point to
2575  bits in two bitmaps in two different places:
2576 
2577  - Query_tables_list::binlog_stmt_flags
2578  - THD::binlog_unsafe_warning_flags
2579 
2580  Hence in practice this is not an enum at all, but a map from
2581  symbols to bit indexes.
2582 
2583  The ordering of elements in this enum must correspond to the order of
2584  elements in the array binlog_stmt_unsafe_errcode.
2585  */
2587  /**
2588  SELECT..LIMIT is unsafe because the set of rows returned cannot
2589  be predicted.
2590  */
2592  /**
2593  Access to log tables is unsafe because slave and master probably
2594  log different things.
2595  */
2597  /**
2598  Inserting into an autoincrement column in a stored routine is unsafe.
2599  Even with just one autoincrement column, if the routine is invoked more
2600  than once slave is not guaranteed to execute the statement graph same way
2601  as the master. And since it's impossible to estimate how many times a
2602  routine can be invoked at the query pre-execution phase (see lock_tables),
2603  the statement is marked pessimistically unsafe.
2604  */
2606  /**
2607  Using a UDF (user-defined function) is unsafe.
2608  */
2610  /**
2611  Using most system variables is unsafe, because slave may run
2612  with different options than master.
2613  */
2615  /**
2616  Using some functions is unsafe (e.g., UUID).
2617  */
2619 
2620  /**
2621  Mixing transactional and non-transactional statements are unsafe if
2622  non-transactional reads or writes are occur after transactional
2623  reads or writes inside a transaction.
2624  */
2626 
2627  /**
2628  Mixing self-logging and non-self-logging engines in a statement
2629  is unsafe.
2630  */
2632 
2633  /**
2634  Statements that read from both transactional and non-transactional
2635  tables and write to any of them are unsafe.
2636  */
2638 
2639  /**
2640  INSERT...IGNORE SELECT is unsafe because which rows are ignored depends
2641  on the order that rows are retrieved by SELECT. This order cannot be
2642  predicted and may differ on master and the slave.
2643  */
2645 
2646  /**
2647  INSERT...SELECT...UPDATE is unsafe because which rows are updated depends
2648  on the order that rows are retrieved by SELECT. This order cannot be
2649  predicted and may differ on master and the slave.
2650  */
2652 
2653  /**
2654  Query that writes to a table with auto_inc column after selecting from
2655  other tables are unsafe as the order in which the rows are retrieved by
2656  select may differ on master and slave.
2657  */
2659 
2660  /**
2661  INSERT...REPLACE SELECT is unsafe because which rows are replaced depends
2662  on the order that rows are retrieved by SELECT. This order cannot be
2663  predicted and may differ on master and the slave.
2664  */
2666 
2667  /**
2668  CREATE TABLE... IGNORE... SELECT is unsafe because which rows are ignored
2669  depends on the order that rows are retrieved by SELECT. This order cannot
2670  be predicted and may differ on master and the slave.
2671  */
2673 
2674  /**
2675  CREATE TABLE...REPLACE... SELECT is unsafe because which rows are replaced
2676  depends on the order that rows are retrieved from SELECT. This order
2677  cannot be predicted and may differ on master and the slave
2678  */
2680 
2681  /**
2682  CREATE TABLE...SELECT on a table with auto-increment column is unsafe
2683  because which rows are replaced depends on the order that rows are
2684  retrieved from SELECT. This order cannot be predicted and may differ on
2685  master and the slave
2686  */
2688 
2689  /**
2690  UPDATE...IGNORE is unsafe because which rows are ignored depends on the
2691  order that rows are updated. This order cannot be predicted and may differ
2692  on master and the slave.
2693  */
2695 
2696  /**
2697  INSERT... ON DUPLICATE KEY UPDATE on a table with more than one
2698  UNIQUE KEYS is unsafe.
2699  */
2701 
2702  /**
2703  INSERT into auto-inc field which is not the first part in composed
2704  primary key.
2705  */
2707 
2708  /**
2709  Using a plugin is unsafe.
2710  */
2714 
2715  /**
2716  XA transactions and statements.
2717  */
2719 
2720  /**
2721  If a substatement inserts into or updates a table that has a column with
2722  an unsafe DEFAULT expression, it may not have the same effect on the
2723  slave.
2724  */
2726 
2727  /**
2728  DML or DDL statement that reads a ACL table is unsafe, because the row
2729  are read without acquiring SE row locks. This would allow ACL tables to
2730  be updated by concurrent thread. It would not have the same effect on the
2731  slave.
2732  */
2734 
2735  /* the last element of this enumeration type. */
2737  };
2738  /**
2739  This has all flags from 0 (inclusive) to BINLOG_STMT_FLAG_COUNT
2740  (exclusive) set.
2741  */
2742  static const int BINLOG_STMT_UNSAFE_ALL_FLAGS =
2743  ((1 << BINLOG_STMT_UNSAFE_COUNT) - 1);
2744 
2745  /**
2746  Maps elements of enum_binlog_stmt_unsafe to error codes.
2747  */
2749 
2750  /**
2751  Determine if this statement is marked as unsafe.
2752 
2753  @retval 0 if the statement is not marked as unsafe.
2754  @retval nonzero if the statement is marked as unsafe.
2755  */
2756  inline bool is_stmt_unsafe() const { return get_stmt_unsafe_flags() != 0; }
2757 
2759  return binlog_stmt_flags & (1 << unsafe);
2760  }
2761 
2762  /**
2763  Flag the current (top-level) statement as unsafe.
2764  The flag will be reset after the statement has finished.
2765 
2766  @param unsafe_type The type of unsafety: one of the @c
2767  BINLOG_STMT_FLAG_UNSAFE_* flags in @c enum_binlog_stmt_flag.
2768  */
2769  inline void set_stmt_unsafe(enum_binlog_stmt_unsafe unsafe_type) {
2770  DBUG_TRACE;
2771  assert(unsafe_type >= 0 && unsafe_type < BINLOG_STMT_UNSAFE_COUNT);
2772  binlog_stmt_flags |= (1U << unsafe_type);
2773  return;
2774  }
2775 
2776  /**
2777  Set the bits of binlog_stmt_flags determining the type of
2778  unsafeness of the current statement. No existing bits will be
2779  cleared, but new bits may be set.
2780 
2781  @param flags A binary combination of zero or more bits, (1<<flag)
2782  where flag is a member of enum_binlog_stmt_unsafe.
2783  */
2785  DBUG_TRACE;
2786  assert((flags & ~BINLOG_STMT_UNSAFE_ALL_FLAGS) == 0);
2788  return;
2789  }
2790 
2791  /**
2792  Return a binary combination of all unsafe warnings for the
2793  statement. If the statement has been marked as unsafe by the
2794  'flag' member of enum_binlog_stmt_unsafe, then the return value
2795  from this function has bit (1<<flag) set to 1.
2796  */
2798  DBUG_TRACE;
2800  }
2801 
2802  /**
2803  Determine if this statement is a row injection.
2804 
2805  @retval 0 if the statement is not a row injection
2806  @retval nonzero if the statement is a row injection
2807  */
2808  inline bool is_stmt_row_injection() const {
2809  return binlog_stmt_flags &
2811  }
2812 
2813  /**
2814  Flag the statement as a row injection. A row injection is either
2815  a BINLOG statement, or a row event in the relay log executed by
2816  the slave SQL thread.
2817  */
2818  inline void set_stmt_row_injection() {
2819  DBUG_TRACE;
2822  return;
2823  }
2824 
2826  /*
2827  If a transactional table is about to be read. Note that
2828  a write implies a read.
2829  */
2831  /*
2832  If a non-transactional table is about to be read. Note that
2833  a write implies a read.
2834  */
2836  /*
2837  If a temporary transactional table is about to be read. Note
2838  that a write implies a read.
2839  */
2841  /*
2842  If a temporary non-transactional table is about to be read. Note
2843  that a write implies a read.
2844  */
2846  /*
2847  If a transactional table is about to be updated.
2848  */
2850  /*
2851  If a non-transactional table is about to be updated.
2852  */
2854  /*
2855  If a temporary transactional table is about to be updated.
2856  */
2858  /*
2859  If a temporary non-transactional table is about to be updated.
2860  */
2862  /*
2863  The last element of the enumeration. Please, if necessary add
2864  anything before this.
2865  */
2867  };
2868 
2869 #ifndef NDEBUG
2870  static inline const char *stmt_accessed_table_string(
2871  enum_stmt_accessed_table accessed_table) {
2872  switch (accessed_table) {
2874  return "STMT_READS_TRANS_TABLE";
2875  break;
2877  return "STMT_READS_NON_TRANS_TABLE";
2878  break;
2880  return "STMT_READS_TEMP_TRANS_TABLE";
2881  break;
2883  return "STMT_READS_TEMP_NON_TRANS_TABLE";
2884  break;
2886  return "STMT_WRITES_TRANS_TABLE";
2887  break;
2889  return "STMT_WRITES_NON_TRANS_TABLE";
2890  break;
2892  return "STMT_WRITES_TEMP_TRANS_TABLE";
2893  break;
2895  return "STMT_WRITES_TEMP_NON_TRANS_TABLE";
2896  break;
2898  default:
2899  assert(0);
2900  break;
2901  }
2903  return "";
2904  }
2905 #endif /* DBUG */
2906 
2907 #define BINLOG_DIRECT_ON \
2908  0xF0 /* unsafe when \
2909  --binlog-direct-non-trans-updates \
2910  is ON */
2911 
2912 #define BINLOG_DIRECT_OFF \
2913  0xF /* unsafe when \
2914  --binlog-direct-non-trans-updates \
2915  is OFF */
2916 
2917 #define TRX_CACHE_EMPTY 0x33 /* unsafe when trx-cache is empty */
2918 
2919 #define TRX_CACHE_NOT_EMPTY 0xCC /* unsafe when trx-cache is not empty */
2920 
2921 #define IL_LT_REPEATABLE 0xAA /* unsafe when < ISO_REPEATABLE_READ */
2922 
2923 #define IL_GTE_REPEATABLE 0x55 /* unsafe when >= ISO_REPEATABLE_READ */
2924 
2925  /**
2926  Sets the type of table that is about to be accessed while executing a
2927  statement.
2929  @param accessed_table Enumeration type that defines the type of table,
2930  e.g. temporary, transactional, non-transactional.
2931  */
2932  inline void set_stmt_accessed_table(enum_stmt_accessed_table accessed_table) {
2933  DBUG_TRACE;
2934 
2935  assert(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
2936  stmt_accessed_table_flag |= (1U << accessed_table);
2937 
2938  return;
2939  }
2940 
2941  /**
2942  Checks if a type of table is about to be accessed while executing a
2943  statement.
2944 
2945  @param accessed_table Enumeration type that defines the type of table,
2946  e.g. temporary, transactional, non-transactional.
2948  @retval true if the type of the table is about to be accessed
2949  @retval false otherwise
2950  */
2951  inline bool stmt_accessed_table(enum_stmt_accessed_table accessed_table) {
2952  DBUG_TRACE;
2953 
2954  assert(accessed_table >= 0 && accessed_table < STMT_ACCESS_TABLE_COUNT);
2955 
2956  return (stmt_accessed_table_flag & (1U << accessed_table)) != 0;
2957  }
2958 
2959  /*
2960  Checks if a mixed statement is unsafe.
2961 
2962 
2963  @param in_multi_stmt_transaction_mode defines if there is an on-going
2964  multi-transactional statement.
2965  @param binlog_direct defines if --binlog-direct-non-trans-updates is
2966  active.
2967  @param trx_cache_is_not_empty defines if the trx-cache is empty or not.
2968  @param trx_isolation defines the isolation level.
2969 
2970  @return
2971  @retval true if the mixed statement is unsafe
2972  @retval false otherwise
2973  */
2974  inline bool is_mixed_stmt_unsafe(bool in_multi_stmt_transaction_mode,
2975  bool binlog_direct,
2976  bool trx_cache_is_not_empty,
2977  uint tx_isolation) {
2978  bool unsafe = false;
2979 
2980  if (in_multi_stmt_transaction_mode) {
2981  uint condition =
2982  (binlog_direct ? BINLOG_DIRECT_ON : BINLOG_DIRECT_OFF) &
2983  (trx_cache_is_not_empty ? TRX_CACHE_NOT_EMPTY : TRX_CACHE_EMPTY) &
2984  (tx_isolation >= ISO_REPEATABLE_READ ? IL_GTE_REPEATABLE
2985  : IL_LT_REPEATABLE);
2986 
2987  unsafe = (binlog_unsafe_map[stmt_accessed_table_flag] & condition);
2988 
2989 #if !defined(NDEBUG)
2990  DBUG_PRINT("LEX::is_mixed_stmt_unsafe",
2991  ("RESULT %02X %02X %02X\n", condition,
2994 
2995  int type_in = 0;
2996  for (; type_in < STMT_ACCESS_TABLE_COUNT; type_in++) {
2998  DBUG_PRINT("LEX::is_mixed_stmt_unsafe",
2999  ("ACCESSED %s ", stmt_accessed_table_string(
3000  (enum_stmt_accessed_table)type_in)));
3001  }
3002 #endif
3003  }
3004 
3007  tx_isolation < ISO_REPEATABLE_READ)
3008  unsafe = true;
3011  tx_isolation < ISO_REPEATABLE_READ)
3012  unsafe = true;
3013 
3014  return (unsafe);
3015  }
3016 
3017  /**
3018  true if the parsed tree contains references to stored procedures
3019  or functions, false otherwise
3020  */
3021  bool uses_stored_routines() const { return sroutines_list.elements != 0; }
3023  void set_using_match() { using_match = true; }
3024  bool get_using_match() { return using_match; }
3025 
3027  bool is_stmt_unsafe_with_mixed_mode() const {
3029  }
3030 
3031  private:
3032  /**
3033  Enumeration listing special types of statements.
3034 
3035  Currently, the only possible type is ROW_INJECTION.
3036  */
3037  enum enum_binlog_stmt_type {
3038  /**
3039  The statement is a row injection (i.e., either a BINLOG
3040  statement or a row event executed by the slave SQL thread).
3041  */
3043 
3044  /** The last element of this enumeration type. */
3046  };
3047 
3048  /**
3049  Bit field indicating the type of statement.
3050 
3051  There are two groups of bits:
3052 
3053  - The low BINLOG_STMT_UNSAFE_COUNT bits indicate the types of
3054  unsafeness that the current statement has.
3055 
3056  - The next BINLOG_STMT_TYPE_COUNT bits indicate if the statement
3057  is of some special type.
3058 
3059  This must be a member of LEX, not of THD: each stored procedure
3060  needs to remember its unsafeness state between calls and each
3061  stored procedure has its own LEX object (but no own THD object).
3062  */
3064 
3065  /**
3066  Bit field that determines the type of tables that are about to be
3067  be accessed while executing a statement.
3068  */
3071  /**
3072  It will be set true if 'MATCH () AGAINST' is used in the statement.
3073  */
3074  bool using_match;
3075 
3076  /**
3077  This flag is set to true if statement is unsafe to be binlogged in STATEMENT
3078  format, when in MIXED mode.
3079  Currently this flag is set to true if stored program used in statement has
3080  CREATE/DROP temporary table operation(s) as sub-statement(s).
3081  */
3082  bool stmt_unsafe_with_mixed_mode{false};
3083 };
3084 
3085 /*
3086  st_parsing_options contains the flags for constructions that are
3087  allowed in the current statement.
3088 */
3089 
3091  bool allows_variable;
3092  bool allows_select_into;
3093 
3094  st_parsing_options() { reset(); }
3095  void reset();
3096 };
3098 /**
3099  The state of the lexical parser, when parsing comments.
3100 */
3102  /**
3103  Not parsing comments.
3104  */
3105  NO_COMMENT,
3106 
3107  /**
3108  Parsing comments that need to be preserved.
3109  (Copy '/' '*' and '*' '/' sequences to the preprocessed buffer.)
3110  Typically, these are user comments '/' '*' ... '*' '/'.
3111  */
3113 
3114  /**
3115  Parsing comments that need to be discarded.
3116  (Don't copy '/' '*' '!' and '*' '/' sequences to the preprocessed buffer.)
3117  Typically, these are special comments '/' '*' '!' ... '*' '/',
3118  or '/' '*' '!' 'M' 'M' 'm' 'm' 'm' ... '*' '/', where the comment
3119  markers should not be expanded.
3120  */
3122 };
3123 
3124 /**
3125  This class represents the character input stream consumed during lexical
3126  analysis.
3127 
3128  In addition to consuming the input stream, this class performs some comment
3129  pre processing, by filtering out out-of-bound special text from the query
3130  input stream.
3131 
3132  Two buffers, with pointers inside each, are maintained in parallel. The
3133  'raw' buffer is the original query text, which may contain out-of-bound
3134  comments. The 'cpp' (for comments pre processor) is the pre-processed buffer
3135  that contains only the query text that should be seen once out-of-bound data
3136  is removed.
3137 */
3138 
3139 class Lex_input_stream {
3140  public:
3141  /**
3142  Constructor
3144  @param grammar_selector_token_arg See grammar_selector_token.
3145  */
3146 
3147  explicit Lex_input_stream(uint grammar_selector_token_arg)
3148  : grammar_selector_token(grammar_selector_token_arg) {}
3149 
3150  /**
3151  Object initializer. Must be called before usage.
3152 
3153  @retval false OK
3154  @retval true Error
3155  */
3156  bool init(THD *thd, const char *buff, size_t length);
3157 
3158  void reset(const char *buff, size_t length);
3159 
3160  /**
3161  Set the echo mode.
3162 
3163  When echo is true, characters parsed from the raw input stream are
3164  preserved. When false, characters parsed are silently ignored.
3165  @param echo the echo mode.
3166  */
3167  void set_echo(bool echo) { m_echo = echo; }
3168 
3169  void save_in_comment_state() {
3172  }
3173 
3174  void restore_in_comment_state() {
3175  m_echo = m_echo_saved;
3177  }
3178 
3179  /**
3180  Skip binary from the input stream.
3181  @param n number of bytes to accept.
3182  */
3183  void skip_binary(int n) {
3184  assert(m_ptr + n <= m_end_of_query);
3185  if (m_echo) {
3186  memcpy(m_cpp_ptr, m_ptr, n);
3187  m_cpp_ptr += n;
3188  }
3189  m_ptr += n;
3190  }
3191 
3192  /**
3193  Get a character, and advance in the stream.
3194  @return the next character to parse.
3195  */
3196  unsigned char yyGet() {
3197  assert(m_ptr <= m_end_of_query);
3198  char c = *m_ptr++;
3199  if (m_echo) *m_cpp_ptr++ = c;
3200  return c;
3201  }
3202 
3203  /**
3204  Get the last character accepted.
3205  @return the last character accepted.
3206  */
3207  unsigned char yyGetLast() const { return m_ptr[-1]; }
3209  /**
3210  Look at the next character to parse, but do not accept it.
3211  */
3212  unsigned char yyPeek() const {
3213  assert(m_ptr <= m_end_of_query);
3214  return m_ptr[0];
3215  }
3216 
3217  /**
3218  Look ahead at some character to parse.
3219  @param n offset of the character to look up
3220  */
3221  unsigned char yyPeekn(int n) const {
3222  assert(m_ptr + n <= m_end_of_query);
3223  return m_ptr[n];
3224  }
3225 
3226  /**
3227  Cancel the effect of the last yyGet() or yySkip().
3228  Note that the echo mode should not change between calls to yyGet / yySkip
3229  and yyUnget. The caller is responsible for ensuring that.
3230  */
3231  void yyUnget() {
3232  m_ptr--;
3233  if (m_echo) m_cpp_ptr--;
3234  }
3236  /**
3237  Accept a character, by advancing the input stream.
3238  */
3239  void yySkip() {
3240  assert(m_ptr <= m_end_of_query);
3241  if (m_echo)
3242  *m_cpp_ptr++ = *m_ptr++;
3243  else
3244  m_ptr++;
3245  }
3246 
3247  /**
3248  Accept multiple characters at once.
3249  @param n the number of characters to accept.
3250  */
3251  void yySkipn(int n) {
3252  assert(m_ptr + n <= m_end_of_query);
3253  if (m_echo) {
3254  memcpy(m_cpp_ptr, m_ptr, n);
3255  m_cpp_ptr += n;
3256  }
3257  m_ptr += n;
3258  }
3259 
3260  /**
3261  Puts a character back into the stream, canceling
3262  the effect of the last yyGet() or yySkip().
3263  Note that the echo mode should not change between calls
3264  to unput, get, or skip from the stream.
3265  */
3266  char *yyUnput(char ch) {
3267  *--m_ptr = ch;
3268  if (m_echo) m_cpp_ptr--;
3269  return m_ptr;
3270  }
3271 
3272  /**
3273  Inject a character into the pre-processed stream.
3274 
3275  Note, this function is used to inject a space instead of multi-character
3276  C-comment. Thus there is no boundary checks here (basically, we replace
3277  N-chars by 1-char here).
3278  */
3279  char *cpp_inject(char ch) {
3280  *m_cpp_ptr = ch;
3281  return ++m_cpp_ptr;
3282  }
3283 
3284  /**
3285  End of file indicator for the query text to parse.
3286  @return true if there are no more characters to parse
3287  */
3288  bool eof() const { return (m_ptr >= m_end_of_query); }
3289 
3290  /**
3291  End of file indicator for the query text to parse.
3292  @param n number of characters expected
3293  @return true if there are less than n characters to parse
3294  */
3295  bool eof(int n) const { return ((m_ptr + n) >= m_end_of_query); }
3296 
3297  /** Get the raw query buffer. */
3298  const char *get_buf() const { return m_buf; }
3299 
3300  /** Get the pre-processed query buffer. */
3301  const char *get_cpp_buf() const { return m_cpp_buf; }
3302 
3303  /** Get the end of the raw query buffer. */
3304  const char *get_end_of_query() const { return m_end_of_query; }
3305 
3306  /** Mark the stream position as the start of a new token. */
3307  void start_token() {
3308  m_tok_start = m_ptr;
3309  m_tok_end = m_ptr;
3310 
3313  }
3314 
3315  /**
3316  Adjust the starting position of the current token.
3317  This is used to compensate for starting whitespace.
3318  */
3319  void restart_token() {
3320  m_tok_start = m_ptr;
3322  }
3323 
3324  /** Get the token start position, in the raw buffer. */
3325  const char *get_tok_start() const { return m_tok_start; }
3326 
3327  /** Get the token start position, in the pre-processed buffer. */
3328  const char *get_cpp_tok_start() const { return m_cpp_tok_start; }
3329 
3330  /** Get the token end position, in the raw buffer. */
3331  const char *get_tok_end() const { return m_tok_end; }
3332 
3333  /** Get the token end position, in the pre-processed buffer. */
3334  const char *get_cpp_tok_end() const { return m_cpp_tok_end; }
3335 
3336  /** Get the current stream pointer, in the raw buffer. */
3337  const char *get_ptr() const { return m_ptr; }
3338 
3339  /** Get the current stream pointer, in the pre-processed buffer. */
3340  const char *get_cpp_ptr() const { return m_cpp_ptr; }
3341 
3342  /** Get the length of the current token, in the raw buffer. */
3343  uint yyLength() const {
3344  /*
3345  The assumption is that the lexical analyser is always 1 character ahead,
3346  which the -1 account for.
3347  */
3348  assert(m_ptr > m_tok_start);
3349  return (uint)((m_ptr - m_tok_start) - 1);
3350  }
3351 
3352  /** Get the utf8-body string. */
3353  const char *get_body_utf8_str() const { return m_body_utf8; }
3354 
3355  /** Get the utf8-body length. */
3356  uint get_body_utf8_length() const {
3357  return (uint)(m_body_utf8_ptr - m_body_utf8);
3358  }
3359 
3360  void body_utf8_start(THD *thd, const char *begin_ptr);
3361  void body_utf8_append(const char *ptr);
3362  void body_utf8_append(const char *ptr, const char *end_ptr);
3363  void body_utf8_append_literal(THD *thd, const LEX_STRING *txt,
3364  const CHARSET_INFO *txt_cs,
3365  const char *end_ptr);
3367  uint get_lineno(const char *raw_ptr) const;
3368 
3369  /** Current thread. */
3370  THD *m_thd;
3371 
3372  /** Current line number. */
3373  uint yylineno;
3374 
3375  /** Length of the last token parsed. */
3376  uint yytoklen;
3377 
3378  /** Interface with bison, value of the last token parsed. */
3380 
3381  /**
3382  LALR(2) resolution, look ahead token.
3383  Value of the next token to return, if any,
3384  or -1, if no token was parsed in advance.
3385  Note: 0 is a legal token, and represents YYEOF.
3386  */
3387  int lookahead_token;
3388 
3389  /** LALR(2) resolution, value of the look ahead token.*/
3391 
3392  /// Skip adding of the current token's digest since it is already added
3393  ///
3394  /// Usually we calculate a digest token by token at the top-level function
3395  /// of the lexer: MYSQLlex(). However, some complex ("hintable") tokens break
3396  /// that data flow: for example, the `SELECT /*+ HINT(t) */` is the single
3397  /// token from the main parser's point of view, and we add the "SELECT"
3398  /// keyword to the digest buffer right after the lex_one_token() call,
3399  /// but the "/*+ HINT(t) */" is a sequence of separate tokens from the hint
3400  /// parser's point of view, and we add those tokens to the digest buffer
3401  /// *inside* the lex_one_token() call. Thus, the usual data flow adds
3402  /// tokens from the "/*+ HINT(t) */" string first, and only than it appends
3403  /// the "SELECT" keyword token to that stream: "/*+ HINT(t) */ SELECT".
3404  /// This is not acceptable, since we use the digest buffer to restore
3405  /// query strings in their normalized forms, so the order of added tokens is
3406  /// important. Thus, we add tokens of "hintable" keywords to a digest buffer
3407  /// right in the hint parser and skip adding of them at the caller with the
3408  /// help of skip_digest flag.
3409  bool skip_digest;
3410 
3411  void add_digest_token(uint token, Lexer_yystype *yylval);
3412 
3413  void reduce_digest_token(uint token_left, uint token_right);
3414 
3415  /**
3416  True if this scanner tokenizes a partial query (partition expression,
3417  generated column expression etc.)
3418 
3419  @return true if parsing a partial query, otherwise false.
3420  */
3421  bool is_partial_parser() const { return grammar_selector_token >= 0; }
3422 
3423  /**
3424  Outputs warnings on deprecated charsets in complete SQL statements
3426  @param [in] cs The character set/collation to check for a deprecation.
3427  @param [in] alias The name/alias of @p cs.
3428  */
3430  const char *alias) const {
3431  if (!is_partial_parser()) {
3433  }
3434  }
3435 
3436  /**
3437  Outputs warnings on deprecated collations in complete SQL statements
3438 
3439  @param [in] collation The collation to check for a deprecation.
3440  */
3442  if (!is_partial_parser()) {
3444  }
3445  }
3446 
3448 
3449  private:
3450  /** Pointer to the current position in the raw input stream. */
3451  char *m_ptr;
3452 
3453  /** Starting position of the last token parsed, in the raw buffer. */
3454  const char *m_tok_start;
3455 
3456  /** Ending position of the previous token parsed, in the raw buffer. */
3457  const char *m_tok_end;
3458 
3459  /** End of the query text in the input stream, in the raw buffer. */
3460  const char *m_end_of_query;
3461 
3462  /** Begining of the query text in the input stream, in the raw buffer. */
3463  const char *m_buf;
3464 
3465  /** Length of the raw buffer. */
3467 
3468  /** Echo the parsed stream to the pre-processed buffer. */
3469  bool m_echo;
3470  bool m_echo_saved;
3471 
3472  /** Pre-processed buffer. */
3473  char *m_cpp_buf;
3474 
3475  /** Pointer to the current position in the pre-processed input stream. */
3476  char *m_cpp_ptr;
3477 
3478  /**
3479  Starting position of the last token parsed,
3480  in the pre-processed buffer.
3481  */
3482  const char *m_cpp_tok_start;
3483 
3484  /**
3485  Ending position of the previous token parsed,
3486  in the pre-processed buffer.
3487  */
3488  const char *m_cpp_tok_end;
3489 
3490  /** UTF8-body buffer created during parsing. */
3491  char *m_body_utf8;
3492 
3493  /** Pointer to the current position in the UTF8-body buffer. */
3494  char *m_body_utf8_ptr;
3495 
3496  /**
3497  Position in the pre-processed buffer. The query from m_cpp_buf to
3498  m_cpp_utf_processed_ptr is converted to UTF8-body.
3499  */
3500  const char *m_cpp_utf8_processed_ptr;
3501 
3502  public:
3503  /** Current state of the lexical analyser. */
3505 
3506  /**
3507  Position of ';' in the stream, to delimit multiple queries.
3508  This delimiter is in the raw buffer.
3509  */
3510  const char *found_semicolon;
3511 
3512  /** Token character bitmaps, to detect 7bit strings. */
3513  uchar tok_bitmap;
3514 
3515  /** SQL_MODE = IGNORE_SPACE. */
3516  bool ignore_space;
3517 
3518  /**
3519  true if we're parsing a prepared statement: in this mode
3520  we should allow placeholders.
3521  */
3523  /**
3524  true if we should allow multi-statements.
3525  */
3527 
3528  /** State of the lexical analyser for comments. */
3531 
3532  /**
3533  Starting position of the TEXT_STRING or IDENT in the pre-processed
3534  buffer.
3535 
3536  NOTE: this member must be used within MYSQLlex() function only.
3537  */
3538  const char *m_cpp_text_start;
3539 
3540  /**
3541  Ending position of the TEXT_STRING or IDENT in the pre-processed
3542  buffer.
3543 
3544  NOTE: this member must be used within MYSQLlex() function only.
3545  */
3546  const char *m_cpp_text_end;
3547 
3548  /**
3549  Character set specified by the character-set-introducer.
3550 
3551  NOTE: this member must be used within MYSQLlex() function only.
3552  */
3555  /**
3556  Current statement digest instrumentation.
3557  */
3559 
3560  /**
3561  The synthetic 1st token to prepend token stream with.
3562 
3563  This token value tricks parser to simulate multiple %start-ing points.
3564  Currently the grammar is aware of 4 such synthetic tokens:
3565  1. GRAMMAR_SELECTOR_PART for partitioning stuff from DD,
3566  2. GRAMMAR_SELECTOR_GCOL for generated column stuff from DD,
3567  3. GRAMMAR_SELECTOR_EXPR for generic single expressions from DD/.frm.
3568  4. GRAMMAR_SELECTOR_CTE for generic subquery expressions from CTEs.
3569  5. -1 when parsing with the main grammar (no grammar selector available).
3570 
3571  @note yylex() is expected to return the value of type int:
3572  0 is for EOF and everything else for real token numbers.
3573  Bison, in its turn, generates positive token numbers.
3574  So, the negative grammar_selector_token means "not a token".
3575  In other words, -1 is "empty value".
3576  */
3577  const int grammar_selector_token;
3579  bool text_string_is_7bit() const { return !(tok_bitmap & 0x80); }
3580 };
3582 class LEX_COLUMN {
3583  public:
3584  String column;
3585  uint rights;
3586  LEX_COLUMN(const String &x, const uint &y) : column(x), rights(y) {}
3587 };
3588 
3589 enum class role_enum;
3591 /*
3592  This structure holds information about grantor's context
3593 */
3594 class LEX_GRANT_AS {
3595  public:
3597  void cleanup();
3599  public:
3600  bool grant_as_used;
3602  LEX_USER *user;
3604 };
3605 
3606 /**
3607  The LEX object currently serves three different purposes:
3608 
3609  - It contains some universal properties of an SQL command, such as
3610  sql_command, presence of IGNORE in data change statement syntax, and list
3611  of tables (query_tables).
3612 
3613  - It contains some execution state variables, like m_exec_started
3614  (set to true when execution is started), plugins (list of plugins used
3615  by statement), insert_update_values_map (a map of objects used by certain
3616  INSERT statements), etc.
3617 
3618  - It contains a number of members that should be local to subclasses of
3619  Sql_cmd, like purge_value_list (for the PURGE command), kill_value_list
3620  (for the KILL command).
3621 
3622  The LEX object is strictly a part of class Sql_cmd, for those SQL commands
3623  that are represented by an Sql_cmd class. For the remaining SQL commands,
3624  it is a standalone object linked to the current THD.
3625 
3626  The lifecycle of a LEX object is as follows:
3627 
3628  - The LEX object is constructed either on the execution mem_root
3629  (for regular statements), on a Prepared_statement mem_root (for
3630  prepared statements), on an SP mem_root (for stored procedure instructions),
3631  or created on the current mem_root for short-lived uses.
3632 
3633  - Call lex_start() to initialize a LEX object before use.
3634  This initializes the execution state part of the object.
3635  It also calls LEX::reset() to ensure that all members are properly inited.
3636 
3637  - Parse and resolve the statement, using the LEX as a work area.
3638 
3639  - Execute an SQL command: call set_exec_started() when starting to execute
3640  (actually when starting to optimize).
3641  Typically call is_exec_started() to distinguish between preparation
3642  and optimization/execution stages of SQL command execution.
3643 
3644  - Call clear_execution() when execution is finished. This will clear all
3645  execution state associated with the SQL command, it also includes calling
3646  LEX::reset_exec_started().
3647 
3648  @todo - Create subclasses of Sql_cmd to contain data that are local
3649  to specific commands.
3650 
3651  @todo - Create a Statement context object that will hold the execution state
3652  part of struct LEX.
3653 
3654  @todo - Ensure that a LEX struct is never reused, thus making e.g
3655  LEX::reset() redundant.
3656 */
3658 struct LEX : public Query_tables_list {
3659  friend bool lex_start(THD *thd);
3661  Query_expression *unit; ///< Outer-most query expression
3662  /// @todo: query_block can be replaced with unit->first-select()
3663  Query_block *query_block; ///< First query block
3664  Query_block *all_query_blocks_list; ///< List of all query blocks
3665  private:
3666  /* current Query_block in parsing */
3668 
3669  public:
3670  inline Query_block *current_query_block() const {
3671  return m_current_query_block;
3672  }
3673 
3674  /*
3675  We want to keep current_thd out of header files, so the debug assert
3676  is moved to the .cc file.
3677  */
3679  inline void set_current_query_block(Query_block *select) {
3680 #ifndef NDEBUG
3682 #endif
3684  }
3685  /// @return true if this is an EXPLAIN statement
3686  bool is_explain() const { return explain_format != nullptr; }
3687  bool is_explain_analyze = false;
3688  /**
3689  Whether the currently-running query should be (attempted) executed in
3690  the hypergraph optimizer. This will not change after the query is
3691  done parsing, so you can use it in any query phase to e.g. figure out
3692  whether to inhibit some transformation that the hypergraph optimizer
3693  does not properly understand yet.
3694  */
3696  LEX_STRING name;
3697  char *help_arg;
3698  char *to_log; /* For PURGE MASTER LOGS TO */
3700  // Widcard from SHOW ... LIKE <wildcard> statements.
3704  nullptr, 0}; ///< Argument of the BINLOG event statement.
3711  THD *thd;
3712 
3713  /* Optimizer hints */
3716  /* maintain a list of used plugins for this LEX */
3718  Plugins_array;
3720 
3724  /// Table being inserted into (may be a view)
3726  /// Leaf table being inserted into (always a base table)
3728 
3729  /** SELECT of CREATE VIEW statement */
3731 
3732  /* Partition info structure filled in by PARTITION BY parse part */
3734 
3735  /*
3736  The definer of the object being created (view, trigger, stored routine).
3737  I.e. the value of DEFINER clause.
3738  */
3747 
3748  // PURGE statement-specific fields:
3750 
3751  // KILL statement-specific fields:
3753 
3754  // other stuff:
3756  List<Item_func_set_user_var> set_var_list; // in-query assignment list
3757  /**
3758  List of placeholders ('?') for parameters of a prepared statement. Because
3759  we append to this list during parsing, it is naturally sorted by
3760  position of the '?' in the query string. The code which fills placeholders
3761  with user-supplied values, and the code which writes a query for
3762  statement-based logging, rely on this order.
3763  This list contains only real placeholders, not the clones which originate
3764  in a re-parsed CTE definition.
3765  */
3767 
3768  bool locate_var_assignment(const Name_string &name);
3769 
3770  void insert_values_map(Item_field *f1, Field *f2) {
3772  insert_update_values_map = new std::map<Item_field *, Field *>;
3773  insert_update_values_map->insert(std::make_pair(f1, f2));
3774  }
3775  void destroy_values_map() {
3777  insert_update_values_map->clear();
3779  insert_update_values_map = nullptr;
3780  }
3781  }
3782  void clear_values_map() {
3785  }
3786  }
3787  bool has_values_map() const { return insert_update_values_map != nullptr; }
3788  std::map<Item_field *, Field *>::iterator begin_values_map() {
3789  return insert_update_values_map->begin();
3790  }
3791  std::map<Item_field *, Field *>::iterator end_values_map() {
3792  return insert_update_values_map->end();
3793  }
3794 
3795  private:
3796  /*
3797  With Visual Studio, an std::map will always allocate two small objects
3798  on the heap. Sometimes we put LEX objects in a MEM_ROOT, and never run
3799  the LEX DTOR. To avoid memory leaks, put this std::map on the heap,
3800  and call clear_values_map() at the end of each statement.
3801  */
3802  std::map<Item_field *, Field *> *insert_update_values_map;
3803 
3804  public:
3805  /*
3806  A stack of name resolution contexts for the query. This stack is used
3807  at parse time to set local name resolution contexts for various parts
3808  of a query. For example, in a JOIN ... ON (some_condition) clause the
3809  Items in 'some_condition' must be resolved only against the operands
3810  of the the join, and not against the whole clause. Similarly, Items in
3811  subqueries should be resolved against the subqueries (and outer queries).
3812  The stack is used in the following way: when the parser detects that
3813  all Items in some clause need a local context, it creates a new context
3814  and pushes it on the stack. All newly created Items always store the
3815  top-most context in the stack. Once the parser leaves the clause that
3816  required a local context, the parser pops the top-most context.
3817  */
3822  HA_CHECK_OPT check_opt; // check/repair options
3825  LEX_MASTER_INFO mi; // used by CHANGE MASTER
3830  ulong type;
3831  /**
3832  This field is used as a work field during resolving to validate
3833  the use of aggregate functions. For example in a query
3834  SELECT ... FROM ...WHERE MIN(i) == 1 GROUP BY ... HAVING MIN(i) > 2
3835  MIN(i) in the WHERE clause is not allowed since only non-aggregated data
3836  is present, whereas MIN(i) in the HAVING clause is allowed because HAVING
3837  operates on the output of a grouping operation.
3838  Each query block is assigned a nesting level. This field is a bit field
3839  that contains the value one in the position of that nesting level if
3840  aggregate functions are allowed for that query block.
3841  */
3843  /**
3844  Windowing functions are not allowed in HAVING - in contrast to group
3845  aggregates - then we need to be stricter than allow_sum_func.
3846  One bit per query block, as allow_sum_func.
3847  */
3850  /// If true: during prepare, we did a subquery transformation (IN-to-EXISTS,
3851  /// SOME/ANY) that doesn't currently work for subquery to a derived table
3852  /// transformation.
3854 
3855  Sql_cmd *m_sql_cmd;
3856 
3857  /*
3858  Usually `expr` rule of yacc is quite reused but some commands better
3859  not support subqueries which comes standard with this rule, like
3860  KILL, HA_READ, CREATE/ALTER EVENT etc. Set this to `false` to get
3861  syntax error back.
3862  */
3863  bool expr_allows_subselect;
3864  /**
3865  If currently re-parsing a CTE's definition, this is the offset in bytes
3866  of that definition in the original statement which had the WITH
3867  clause. Otherwise this is 0.
3868  */
3870 
3871  enum SSL_type ssl_type; /* defined in violite.h */
3877  /// QUERY ID for SHOW PROFILE
3881  /**
3882  Set to true when GRANT ... GRANT OPTION ... TO ...
3883  is used (vs. GRANT ... WITH GRANT OPTION).
3884  The flag is used by @ref mysql_grant to grant GRANT OPTION (@ref GRANT_ACL)
3885  to all dynamic privileges.
3886  */
3889  int select_number; ///< Number of query block (by EXPLAIN)
3892  /**
3893  @todo ensure that correct CONTEXT_ANALYSIS_ONLY is set for all preparation
3894  code, so we can fully rely on this field.
3895  */
3897  bool drop_if_exists;
3899  bool autocommit;
3901  // For show commands to show hidden columns and indexes.
3902  bool m_extended_show;
3903 
3904  enum enum_yes_no_unknown tx_chain, tx_release;
3905 
3906  /**
3907  Whether this query will return the same answer every time, given unchanged
3908  data. Used to be for the query cache, but is now used to find out if an
3909  expression is usable for partitioning.
3910  */
3913  private:
3914  /// True if statement references UDF functions
3915  bool m_has_udf{false};
3916  bool ignore;
3918  public:
3919  bool is_ignore() const { return ignore; }
3920  void set_ignore(bool ignore_param) { ignore = ignore_param; }
3921  void set_has_udf() { m_has_udf = true; }
3922  bool has_udf() const { return m_has_udf; }
3925  /* Prepared statements SQL syntax:*/
3926  LEX_CSTRING prepared_stmt_name; /* Statement name (in all queries) */
3927  /*
3928  Prepared statement query text or name of variable that holds the
3929  prepared statement (in PREPARE ... queries)
3930  */
3932  /* If true, prepared_stmt_code is a name of variable that holds the query */
3934  /* Names of user variables holding parameters (in EXECUTE) */
3938  bool sp_lex_in_use; /* Keep track on lex usage in SPs for error handling */
3939  bool all_privileges;
3943 
3944  private:
3945  bool m_broken; ///< see mark_broken()
3946  /**
3947  Set to true when execution has started (after parsing, tables opened and
3948  query preparation is complete. Used to track arena state for SPs).
3949  */
3950  bool m_exec_started;
3951  /**
3952  Set to true when execution is completed, ie optimization has been done
3953  and execution is successful or ended in error.
3954  */
3955  bool m_exec_completed;
3956  /**
3957  Current SP parsing context.
3958  @see also sp_head::m_root_parsing_ctx.
3959  */
3962  /**
3963  Statement context for Query_block::make_active_options.
3964  */
3966 
3967  public:
3968  /**
3969  Gets the options that have been set for this statement. The options are
3970  propagated to the Query_block objects and should usually be read with
3971  #Query_block::active_options().
3972 
3973  @return a bit set of options set for this statement
3974  */
3976  /**
3977  Add options to values of m_statement_options. options is an ORed
3978  bit set of options defined in query_options.h
3980  @param options Add this set of options to the set already in
3981  m_statement_options
3982  */
3985  }
3986  bool is_broken() const { return m_broken; }
3987  /**
3988  Certain permanent transformations (like in2exists), if they fail, may
3989  leave the LEX in an inconsistent state. They should call the
3990  following function, so that this LEX is not reused by another execution.
3992  @todo If lex_start () were a member function of LEX, the "broken"
3993  argument could always be "true" and thus could be removed.
3994  */
3995  void mark_broken(bool broken = true) {
3996  if (broken) {
3997  /*
3998  "OPEN <cursor>" cannot be re-prepared if the cursor uses no tables
3999  ("SELECT FROM DUAL"). Indeed in that case cursor_query is left empty
4000  in constructions of sp_instr_cpush, and thus
4001  sp_lex_instr::parse_expr() cannot re-prepare. So we mark the statement
4002  as broken only if tables are used.
4003  */
4004  if (is_metadata_used()) m_broken = true;
4005  } else
4006  m_broken = false;
4007  }
4008 
4011  void cleanup(THD *thd, bool full) { unit->cleanup(thd, full); }
4012 
4013  bool is_exec_started() const { return m_exec_started; }
4014  void set_exec_started() { m_exec_started = true; }
4015  void reset_exec_started() {
4016  m_exec_started = false;
4017  m_exec_completed = false;
4018  }
4019  /**
4020  Check whether the statement has been executed (regardless of completion -
4021  successful or in error).
4022  Check this instead of Query_expression::is_executed() to determine
4023  the state of a complete statement.
4024  */
4025  bool is_exec_completed() const { return m_exec_completed; }
4026  void set_exec_completed() { m_exec_completed = true; }
4028 
4030  sp_current_parsing_ctx = ctx;
4031  }
4032 
4033  /// Check if the current statement uses meta-data (uses a table or a stored
4034  /// routine).
4035  bool is_metadata_used() const {
4036  return query_tables != nullptr || has_udf() ||
4037  (sroutines != nullptr && !sroutines->empty());
4038  }
4040  public:
4042 
4044 
4045  bool only_view; /* used for SHOW CREATE TABLE/VIEW */
4046  /*
4047  view created to be run from definer (standard behaviour)
4048  */
4050 
4051  /**
4052  Intended to point to the next word after DEFINER-clause in the
4053  following statements:
4054 
4055  - CREATE TRIGGER (points to "TRIGGER");
4056  - CREATE PROCEDURE (points to "PROCEDURE");
4057  - CREATE FUNCTION (points to "FUNCTION" or "AGGREGATE");
4058  - CREATE EVENT (points to "EVENT")
4060  This pointer is required to add possibly omitted DEFINER-clause to the
4061  DDL-statement before dumping it to the binlog.
4062  */
4063  const char *stmt_definition_begin;
4064  const char *stmt_definition_end;
4065 
4066  /**
4067  During name resolution search only in the table list given by
4068  Name_resolution_context::first_name_resolution_table and
4069  Name_resolution_context::last_name_resolution_table
4070  (see Item_field::fix_fields()).
4071  */
4073 
4074  bool is_lex_started; /* If lex_start() did run. For debugging. */
4075  /// Set to true while resolving values in ON DUPLICATE KEY UPDATE clause
4079 
4080  // Maximum execution time for a statement.
4081  ulong max_execution_time;
4082 
4083  /*
4084  To flag the current statement as dependent for binary logging
4085  on explicit_defaults_for_timestamp
4086  */
4088 
4089  /**
4090  Used to inform the parser whether it should contextualize the parse
4091  tree. When we get a pure parser this will not be needed.
4092  */
4093  bool will_contextualize;
4094 
4095  LEX();
4097  virtual ~LEX();
4098 
4099  /// Destroy contained objects, but not the LEX object itself.
4100  void destroy() {
4101  if (unit == nullptr) return;
4102  unit->destroy();
4103  unit = nullptr;
4104  query_block = nullptr;
4105  all_query_blocks_list = nullptr;
4106  m_current_query_block = nullptr;
4108  }
4109 
4110  /// Reset query context to initial state
4111  void reset();
4112 
4113  /// Create an empty query block within this LEX object.
4115 
4116  /// Create query expression object that contains one query block.
4117  Query_block *new_query(Query_block *curr_query_block);
4118 
4119  /// Create query block and attach it to the current query expression.
4120  Query_block *new_union_query(Query_block *curr_query_block, bool distinct);
4121 
4122  /// Create top-level query expression and query block.
4123  bool new_top_level_query();
4124 
4125  /// Create query expression and query block in existing memory objects.
4126  void new_static_query(Query_expression *sel_query_expression,
4127  Query_block *select);
4128 
4129  /// Create query expression under current_query_block and a query block under
4130  /// the new query expression. The new query expression is linked in under
4131  /// current_query_block. The new query block is linked in under the new
4132  /// query expression.
4133  ///
4134  /// @param thd current session context
4135  /// @param current_query_block the root under which we create the new
4136  /// expression
4137  /// and block
4138  /// @param where_clause any where clause for the block
4139  /// @param having_clause any having clause for the block
4140  /// @param ctx the parsing context
4141  ///
4142  /// @returns the new query expression, or nullptr on error.
4144  THD *thd, Query_block *current_query_block, Item *where_clause,
4145  Item *having_clause, enum_parsing_context ctx);
4146 
4147  inline bool is_ps_or_view_context_analysis() {
4150  }
4151 
4152  inline bool is_view_context_analysis() {
4154  }
4155 
4156  void clear_execution();
4157 
4158  /**
4159  Set the current query as uncacheable.
4160 
4161  @param curr_query_block Current select query block
4162  @param cause Why this query is uncacheable.
4163 
4164  @details
4165  All query blocks representing subqueries, from the current one up to
4166  the outer-most one, but excluding the main query block, are also set
4167  as uncacheable.
4168  */
4169  void set_uncacheable(Query_block *curr_query_block, uint8 cause) {
4170  safe_to_cache_query = false;
4171 
4172  if (m_current_query_block == nullptr) return;
4173  Query_block *sl;
4174  Query_expression *un;
4175  for (sl = curr_query_block, un = sl->master_query_expression(); un != unit;
4176  sl = sl->outer_query_block(), un = sl->master_query_expression()) {
4177  sl->uncacheable |= cause;
4178  un->uncacheable |= cause;
4179  }
4180  }
4182 
4183  TABLE_LIST *unlink_first_table(bool *link_to_local);
4184  void link_first_table_back(TABLE_LIST *first, bool link_to_local);
4186 
4188 
4190  for (TABLE_LIST *tr = insert_table->first_leaf_table(); tr != nullptr;
4191  tr = tr->next_leaf)
4192  tr->restore_properties();
4193  }
4194 
4195  bool save_cmd_properties(THD *thd) { return unit->save_cmd_properties(thd); }
4196 
4197  bool can_use_merged();
4198  bool can_not_use_merged();
4199  bool need_correct_ident();
4200  /*
4201  Is this update command where 'WHITH CHECK OPTION' clause is important
4202 
4203  SYNOPSIS
4204  LEX::which_check_option_applicable()
4205 
4206  RETURN
4207  true have to take 'WHITH CHECK OPTION' clause into account
4208  false 'WHITH CHECK OPTION' clause do not need
4209  */
4210  inline bool which_check_option_applicable() {
4211  switch (sql_command) {
4212  case SQLCOM_UPDATE:
4213  case SQLCOM_UPDATE_MULTI:
4214  case SQLCOM_INSERT:
4215  case SQLCOM_INSERT_SELECT:
4216  case SQLCOM_REPLACE:
4217  case SQLCOM_REPLACE_SELECT:
4218  case SQLCOM_LOAD:
4219  return true;
4220  default:
4221  return false;
4222  }
4223  }
4224 
4226 
4228  return context_stack.push_front(context);
4229  }
4230 
4232 
4233  bool copy_db_to(char const **p_db, size_t *p_db_length) const;
4234 
4235  bool copy_db_to(char **p_db, size_t *p_db_length) const {
4236  return copy_db_to(const_cast<const char **>(p_db), p_db_length);
4237  }
4238 
4240 
4243 
4244  bool table_or_sp_used();
4245 
4246  /**
4247  @brief check if the statement is a single-level join
4248  @return result of the check
4249  @retval true The statement doesn't contain subqueries, unions and
4250  stored procedure calls.
4251  @retval false There are subqueries, UNIONs or stored procedure calls.
4252  */
4253  bool is_single_level_stmt() {
4254  /*
4255  This check exploits the fact that the last added to all_select_list is
4256  on its top. So query_block (as the first added) will be at the tail
4257  of the list.
4258  */
4260  (sroutines == nullptr || sroutines->empty())) {
4262  return true;
4263  }
4264  return false;
4265  }
4266 
4267  void release_plugins();
4268 
4270  bool rebind_plugin_vars(THD *);
4271 
4272  /**
4273  IS schema queries read some dynamic table statistics from SE.
4274  These statistics are cached, to avoid opening of table more
4275  than once while preparing a single output record buffer.
4276  */
4279 
4280  bool accept(Select_lex_visitor *visitor);
4281 
4282  bool set_wild(LEX_STRING);
4283  void clear_privileges();
4284 
4285  bool make_sql_cmd(Parse_tree_root *parse_tree);
4286 
4287  private:
4288  /**
4289  Context object used by secondary storage engines to store query
4290  state during optimization and execution.
4291  */
4293 
4294  public:
4295  /**
4296  Gets the secondary engine execution context for this statement.
4297  */
4299  const {
4301  }
4302 
4303  /**
4304  Sets the secondary engine execution context for this statement.
4305  The old context object is destroyed, if there is one. Can be set
4306  to nullptr to destroy the old context object and clear the
4307  pointer.
4308 
4309  The supplied context object should be allocated on the execution
4310  MEM_ROOT, so that its memory doesn't have to be manually freed
4311  after query execution.
4312  */
4315 
4316  private:
4318 
4319  public:
4322  }
4323 
4326  }
4327 
4328  bool set_channel_name(LEX_CSTRING name = {});
4329 };
4330 
4331 /**
4332  RAII class to ease the call of LEX::mark_broken() if error.
4333  Used during preparation and optimization of DML queries.
4334 */
4335 class Prepare_error_tracker {
4336  public:
4337  Prepare_error_tracker(THD *thd_arg) : thd(thd_arg) {}
4339 
4340  private:
4341  THD *const thd;
4342 };
4343 
4344 /**
4345  The internal state of the syntax parser.
4346  This object is only available during parsing,
4347  and is private to the syntax parser implementation (sql_yacc.yy).
4348 */
4349 class Yacc_state {
4350  public:
4352  reset();
4353  }
4354 
4355  void reset() {
4356  if (yacc_yyss != nullptr) {
4357  my_free(yacc_yyss);
4358  yacc_yyss = nullptr;
4359  }
4360  if (yacc_yyvs != nullptr) {
4361  my_free(yacc_yyvs);
4362  yacc_yyvs = nullptr;
4363  }
4364  if (yacc_yyls != nullptr) {
4365  my_free(yacc_yyls);
4366  yacc_yyls = nullptr;
4367  }
4370  }
4371 
4372  ~Yacc_state();
4373 
4374  /**
4375  Reset part of the state which needs resetting before parsing
4376  substatement.
4377  */
4378  void reset_before_substatement() {
4381  }
4382 
4383  /**
4384  Bison internal state stack, yyss, when dynamically allocated using
4385  my_yyoverflow().
4386  */
4387  uchar *yacc_yyss;
4388 
4389  /**
4390  Bison internal semantic value stack, yyvs, when dynamically allocated using
4391  my_yyoverflow().
4392  */
4393  uchar *yacc_yyvs;
4394 
4395  /**
4396  Bison internal location value stack, yyls, when dynamically allocated using
4397  my_yyoverflow().
4398  */
4399  uchar *yacc_yyls;
4400 
4401  /**
4402  Type of lock to be used for tables being added to the statement's
4403  table list in table_factor, table_alias_ref, single_multi and
4404  table_wild_one rules.
4405  Statements which use these rules but require lock type different
4406  from one specified by this member have to override it by using
4407  Query_block::set_lock_for_tables() method.
4408 
4409  The default value of this member is TL_READ_DEFAULT. The only two
4410  cases in which we change it are:
4411  - When parsing SELECT HIGH_PRIORITY.
4412  - Rule for DELETE. In which we use this member to pass information
4413  about type of lock from delete to single_multi part of rule.
4415  We should try to avoid introducing new use cases as we would like
4416  to get rid of this member eventually.
4417  */
4419 
4420  /**
4421  The type of requested metadata lock for tables added to
4422  the statement table list.
4423  */
4425 
4426  /*
4427  TODO: move more attributes from the LEX structure here.
4428  */
4429 };
4431 /**
4432  Input parameters to the parser.
4433 */
4434 struct Parser_input {
4435  /**
4436  True if the text parsed corresponds to an actual query,
4437  and not another text artifact.
4438  This flag is used to disable digest parsing of nested:
4439  - view definitions
4440  - table trigger definitions
4441  - table partition definitions
4442  - event scheduler event definitions
4443  */
4444  bool m_has_digest;
4445  /**
4446  True if the caller needs to compute a digest.
4447  This flag is used to request explicitly a digest computation,
4448  independently of the performance schema configuration.
4449  */
4450  bool m_compute_digest;
4451 
4452  Parser_input() : m_has_digest(false), m_compute_digest(false) {}
4453 };
4454 
4455 /**
4456  Internal state of the parser.
4457  The complete state consist of:
4458  - input parameters that control the parser behavior
4459  - state data used during lexical parsing,
4460  - state data used during syntactic parsing.
4461 */
4462 class Parser_state {
4463  protected:
4464  /**
4465  Constructor for special parsers of partial SQL clauses (DD)
4466 
4467  @param grammar_selector_token See Lex_input_stream::grammar_selector_token
4468  */
4469  explicit Parser_state(int grammar_selector_token)
4470  : m_input(), m_lip(grammar_selector_token), m_yacc(), m_comment(false) {}
4471 
4472  public:
4473  Parser_state() : m_input(), m_lip(-1), m_yacc(), m_comment(false) {}
4474 
4475  /**
4476  Object initializer. Must be called before usage.
4478  @retval false OK
4479  @retval true Error
4480  */
4481  bool init(THD *thd, const char *buff, size_t length) {
4482  return m_lip.init(thd, buff, length);
4483  }
4484 
4485  void reset(const char *found_semicolon, size_t length) {
4486  m_lip.reset(found_semicolon, length);
4488  }
4490  /// Signal that the current query has a comment
4491  void add_comment() { m_comment = true; }
4492  /// Check whether the current query has a comment
4493  bool has_comment() const { return m_comment; }
4495  public:
4499  /**
4500  Current performance digest instrumentation.
4501  */
4503 
4504  private:
4505  bool m_comment; ///< True if current query contains comments
4506 };
4508 /**
4509  Parser state for partition expression parser (.frm/DD stuff)
4510 */
4512  public:
4514 
4516 };
4518 /**
4519  Parser state for generated column expression parser (.frm/DD stuff)
4520 */
4522  public:
4524 
4526 };
4528 /**
4529  Parser state for single expression parser (.frm/DD stuff)
4530 */
4532  public:
4534 
4535  Item *result;
4536 };
4538 /**
4539  Parser state for CTE subquery parser
4540 */
4542  public:
4544 
4546 };
4548 /**
4549  Parser state for Derived table select expressions
4550 */
4552  public:
4556 };
4557 
4558 struct st_lex_local : public LEX {
4559  static void *operator new(size_t size) noexcept {
4560  return (*THR_MALLOC)->Alloc(size);
4561  }
4562  static void *operator new(size_t size, MEM_ROOT *mem_root,
4563  const std::nothrow_t &arg
4564  [[maybe_unused]] = std::nothrow) noexcept {
4565  return mem_root->Alloc(size);
4566  }
4567  static void operator delete(void *ptr [[maybe_unused]],
4568  size_t size [[maybe_unused]]) {
4569  TRASH(ptr, size);
4570  }
4571  static void operator delete(
4572  void *, MEM_ROOT *, const std::nothrow_t &) noexcept { /* Never called */
4573  }
4574 };
4575 
4576 extern bool lex_init(void);
4577 extern void lex_free(void);
4578 extern bool lex_start(THD *thd);
4579 extern void lex_end(LEX *lex);
4580 extern int MYSQLlex(union YYSTYPE *, struct YYLTYPE *, class THD *);
4581 
4582 extern void trim_whitespace(const CHARSET_INFO *cs, LEX_STRING *str);
4583 
4584 extern bool is_lex_native_function(const LEX_STRING *name);
4586 bool is_keyword(const char *name, size_t len);
4587 bool db_is_default_db(const char *db, size_t db_len, const THD *thd);
4588 
4590 
4591 void print_derived_column_names(const THD *thd, String *str,
4593 
4594 /**
4595  @} (End of group GROUP_PARSER)
4596 */
4597 
4598 /**
4599  Check if the given string is invalid using the system charset.
4600 
4601  @param string_val Reference to the string.
4602  @param charset_info Pointer to charset info.
4603 
4604  @return true if the string has an invalid encoding using
4605  the system charset else false.
4606 */
4607 
4608 inline bool is_invalid_string(const LEX_CSTRING &string_val,
4609  const CHARSET_INFO *charset_info) {
4610  size_t valid_len;
4611  bool len_error;
4612 
4613  if (validate_string(charset_info, string_val.str, string_val.length,
4614  &valid_len, &len_error)) {
4615  char hexbuf[7];
4616  octet2hex(
4617  hexbuf, string_val.str + valid_len,
4618  static_cast<uint>(std::min<size_t>(string_val.length - valid_len, 3)));
4619  my_error(ER_INVALID_CHARACTER_STRING, MYF(0), charset_info->csname, hexbuf);
4620  return true;
4621  }
4622  return false;
4623 }
4624 
4625 /**
4626  In debug mode, verify that we're not adding an item twice to the fields list
4627  with inconsistent hidden flags. Must be called before adding the item to
4628  fields.
4629  */
4631  [[maybe_unused]],
4632  Item *item [[maybe_unused]],
4633  bool hidden [[maybe_unused]]) {
4634 #ifndef NDEBUG
4635  if (std::find(fields.begin(), fields.end(), item) != fields.end()) {
4636  // The item is already in the list, so we can't add it
4637  // with a different value for hidden.
4638  assert(item->hidden == hidden);
4639  }
4640 #endif
4641 }
4642 
4643 bool walk_item(Item *item, Select_lex_visitor *visitor);
4645 bool accept_table(TABLE_LIST *t, Select_lex_visitor *visitor);
4647  Select_lex_visitor *visitor);
4648 TABLE_LIST *nest_join(THD *thd, Query_block *select, TABLE_LIST *embedding,
4649  mem_root_deque<TABLE_LIST *> *jlist, size_t table_cnt,
4650  const char *legend);
4651 #endif /* SQL_LEX_INCLUDED */
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:570
Data describing the table being created by CREATE TABLE or altered by ALTER TABLE.
Definition: sql_alter.h:204
Parser state for CTE subquery parser.
Definition: sql_lex.h:4537
Common_table_expr_parser_state()
Definition: sql_lex.cc:1143
PT_subquery * result
Definition: sql_lex.h:4541
Utility RAII class to save/modify/restore the condition_context information of a query block.
Definition: sql_lex.h:2362
enum_condition_context saved_value
Definition: sql_lex.h:2384
~Condition_context()
Definition: sql_lex.h:2378
Query_block * select
Definition: sql_lex.h:2383
Condition_context(Query_block *select_ptr, enum_condition_context new_type=enum_condition_context::NEITHER)
Definition: sql_lex.h:2364
Parser state for Derived table select expressions.
Definition: sql_lex.h:4547
Item * result
Definition: sql_lex.h:4551
Derived_expr_parser_state()
Definition: sql_lex.cc:1146
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:4527
Expression_parser_state()
Definition: sql_lex.cc:1140
Item * result
Definition: sql_lex.h:4531
Definition: field.h:590
Parser state for generated column expression parser (.frm/DD stuff)
Definition: sql_lex.h:4517
Value_generator * result
Definition: sql_lex.h:4521
Gcol_expr_parser_state()
Definition: sql_lex.cc:1137
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:2634
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:2334
Definition: item_subselect.h:412
Definition: item.h:4027
Definition: item_func.h:3398
Definition: item_func.h:3444
This class is used to implement operations like SET @variable or @variable:= expression.
Definition: item_func.h:3208
A wrapper Item that normally returns its parameter, but becomes NULL when processing rows for rollup.
Definition: item_func.h:1578
A wrapper Item that contains a number of aggregate items, one for each level of rollup (see Item_roll...
Definition: item_sum.h:2686
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:3578
LEX_COLUMN(const String &x, const uint &y)
Definition: sql_lex.h:3582
uint rights
Definition: sql_lex.h:3581
String column
Definition: sql_lex.h:3580
Definition: sql_lex.h:3590
List< LEX_USER > * role_list
Definition: sql_lex.h:3599
void cleanup()
Definition: sql_lex.cc:5045
bool grant_as_used
Definition: sql_lex.h:3596
role_enum role_type
Definition: sql_lex.h:3597
LEX_USER * user
Definition: sql_lex.h:3598
LEX_GRANT_AS()
Definition: sql_lex.cc:5052
This class represents the character input stream consumed during lexical analysis.
Definition: sql_lex.h:3135
const char * m_end_of_query
End of the query text in the input stream, in the raw buffer.
Definition: sql_lex.h:3456
void restart_token()
Adjust the starting position of the current token.
Definition: sql_lex.h:3315
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:284
const int grammar_selector_token
The synthetic 1st token to prepend token stream with.
Definition: sql_lex.h:3573
void skip_binary(int n)
Skip binary from the input stream.
Definition: sql_lex.h:3179
bool skip_digest
Skip adding of the current token's digest since it is already added.
Definition: sql_lex.h:3405
char * yyUnput(char ch)
Puts a character back into the stream, canceling the effect of the last yyGet() or yySkip().
Definition: sql_lex.h:3262
const char * m_tok_start
Starting position of the last token parsed, in the raw buffer.
Definition: sql_lex.h:3450
const char * get_cpp_ptr() const
Get the current stream pointer, in the pre-processed buffer.
Definition: sql_lex.h:3336
void body_utf8_append(const char *ptr)
The operation appends unprocessed part of the pre-processed buffer till the given pointer (ptr) and s...
Definition: sql_lex.cc:345
uchar tok_bitmap
Token character bitmaps, to detect 7bit strings.
Definition: sql_lex.h:3509
char * m_cpp_ptr
Pointer to the current position in the pre-processed input stream.
Definition: sql_lex.h:3472
const char * get_buf() const
Get the raw query buffer.
Definition: sql_lex.h:3294
void restore_in_comment_state()
Definition: sql_lex.h:3170
bool multi_statements
true if we should allow multi-statements.
Definition: sql_lex.h:3522
const char * m_cpp_tok_start
Starting position of the last token parsed, in the pre-processed buffer.
Definition: sql_lex.h:3478
uint get_body_utf8_length() const
Get the utf8-body length.
Definition: sql_lex.h:3352
bool m_echo_saved
Definition: sql_lex.h:3466
const char * get_ptr() const
Get the current stream pointer, in the raw buffer.
Definition: sql_lex.h:3333
const char * m_tok_end
Ending position of the previous token parsed, in the raw buffer.
Definition: sql_lex.h:3453
void warn_on_deprecated_collation(const CHARSET_INFO *collation) const
Outputs warnings on deprecated collations in complete SQL statements.
Definition: sql_lex.h:3437
bool init(THD *thd, const char *buff, size_t length)
Object initializer.
Definition: sql_lex.cc:204
enum my_lex_states next_state
Current state of the lexical analyser.
Definition: sql_lex.h:3500
Lexer_yystype * yylval
Interface with bison, value of the last token parsed.
Definition: sql_lex.h:3375
uint yyLength() const
Get the length of the current token, in the raw buffer.
Definition: sql_lex.h:3339
char * m_body_utf8
UTF8-body buffer created during parsing.
Definition: sql_lex.h:3487
char * m_body_utf8_ptr
Pointer to the current position in the UTF8-body buffer.
Definition: sql_lex.h:3490
const char * m_cpp_utf8_processed_ptr
Position in the pre-processed buffer.
Definition: sql_lex.h:3496
enum_comment_state in_comment_saved
Definition: sql_lex.h:3526
char * m_ptr
Pointer to the current position in the raw input stream.
Definition: sql_lex.h:3447
const char * get_tok_start() const
Get the token start position, in the raw buffer.
Definition: sql_lex.h:3321
const char * get_cpp_tok_end() const
Get the token end position, in the pre-processed buffer.
Definition: sql_lex.h:3330
uint get_lineno(const char *raw_ptr) const
Definition: sql_lex.cc:1117
bool text_string_is_7bit() const
Definition: sql_lex.h:3575
bool stmt_prepare_mode
true if we're parsing a prepared statement: in this mode we should allow placeholders.
Definition: sql_lex.h:3518
void add_digest_token(uint token, Lexer_yystype *yylval)
Definition: sql_lex.cc:385
const CHARSET_INFO * query_charset
Definition: sql_lex.h:3443
const char * get_end_of_query() const
Get the end of the raw query buffer.
Definition: sql_lex.h:3300
const char * m_cpp_text_start
Starting position of the TEXT_STRING or IDENT in the pre-processed buffer.
Definition: sql_lex.h:3534
char * m_cpp_buf
Pre-processed buffer.
Definition: sql_lex.h:3469
size_t m_buf_length
Length of the raw buffer.
Definition: sql_lex.h:3462
const char * found_semicolon
Position of ';' in the stream, to delimit multiple queries.
Definition: sql_lex.h:3506
sql_digest_state * m_digest
Current statement digest instrumentation.
Definition: sql_lex.h:3554
char * cpp_inject(char ch)
Inject a character into the pre-processed stream.
Definition: sql_lex.h:3275
const char * m_cpp_text_end
Ending position of the TEXT_STRING or IDENT in the pre-processed buffer.
Definition: sql_lex.h:3542
void reduce_digest_token(uint token_left, uint token_right)
Definition: sql_lex.cc:391
THD * m_thd
Current thread.
Definition: sql_lex.h:3366
const char * get_tok_end() const
Get the token end position, in the raw buffer.
Definition: sql_lex.h:3327
const char * get_cpp_buf() const
Get the pre-processed query buffer.
Definition: sql_lex.h:3297
Lexer_yystype * lookahead_yylval
LALR(2) resolution, value of the look ahead token.
Definition: sql_lex.h:3386
const char * m_buf
Begining of the query text in the input stream, in the raw buffer.
Definition: sql_lex.h:3459
Lex_input_stream(uint grammar_selector_token_arg)
Constructor.
Definition: sql_lex.h:3143
void save_in_comment_state()
Definition: sql_lex.h:3165
unsigned char yyGet()
Get a character, and advance in the stream.
Definition: sql_lex.h:3192
void warn_on_deprecated_charset(const CHARSET_INFO *cs, const char *alias) const
Outputs warnings on deprecated charsets in complete SQL statements.
Definition: sql_lex.h:3425
void yySkip()
Accept a character, by advancing the input stream.
Definition: sql_lex.h:3235
bool m_echo
Echo the parsed stream to the pre-processed buffer.
Definition: sql_lex.h:3465
uint yylineno
Current line number.
Definition: sql_lex.h:3369
void yySkipn(int n)
Accept multiple characters at once.
Definition: sql_lex.h:3247
enum_comment_state in_comment
State of the lexical analyser for comments.
Definition: sql_lex.h:3525
int lookahead_token
LALR(2) resolution, look ahead token.
Definition: sql_lex.h:3383
void body_utf8_append_literal(THD *thd, const LEX_STRING *txt, const CHARSET_INFO *txt_cs, const char *end_ptr)
The operation converts the specified text literal to the utf8 and appends the result to the utf8-body...
Definition: sql_lex.cc:361
unsigned char yyGetLast() const
Get the last character accepted.
Definition: sql_lex.h:3203
const char * get_body_utf8_str() const
Get the utf8-body string.
Definition: sql_lex.h:3349
bool ignore_space
SQL_MODE = IGNORE_SPACE.
Definition: sql_lex.h:3512
const char * m_cpp_tok_end
Ending position of the previous token parsed, in the pre-processed buffer.
Definition: sql_lex.h:3484
void set_echo(bool echo)
Set the echo mode.
Definition: sql_lex.h:3163
void start_token()
Mark the stream position as the start of a new token.
Definition: sql_lex.h:3303
void reset(const char *buff, size_t length)
Prepare Lex_input_stream instance state for use for handling next SQL statement.
Definition: sql_lex.cc:232
void yyUnget()
Cancel the effect of the last yyGet() or yySkip().
Definition: sql_lex.h:3227
const CHARSET_INFO * m_underscore_cs
Character set specified by the character-set-introducer.
Definition: sql_lex.h:3549
unsigned char yyPeekn(int n) const
Look ahead at some character to parse.
Definition: sql_lex.h:3217
unsigned char yyPeek() const
Look at the next character to parse, but do not accept it.
Definition: sql_lex.h:3208
bool eof() const
End of file indicator for the query text to parse.
Definition: sql_lex.h:3284
const char * get_cpp_tok_start() const
Get the token start position, in the pre-processed buffer.
Definition: sql_lex.h:3324
bool is_partial_parser() const
True if this scanner tokenizes a partial query (partition expression, generated column expression etc...
Definition: sql_lex.h:3417
uint yytoklen
Length of the last token parsed.
Definition: sql_lex.h:3372
T * pop()
Definition: sql_list.h:462
T * head()
Definition: sql_list.h:457
bool push_front(T *a)
Definition: sql_list.h:451
bool empty() const
Definition: mem_root_array.h:398
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:418
Storage for name strings.
Definition: item.h:285
Global level hints.
Definition: opt_hints.h:349
Query block level hints.
Definition: opt_hints.h:371
Definition: parse_tree_nodes.h:1638
Represents the WITH clause: WITH [...], [...] SELECT ..., ^^^^^^^^^^^^^^^^^.
Definition: parse_tree_nodes.h:336
Base class for all top-level nodes of SQL statements.
Definition: parse_tree_nodes.h:157
Internal state of the parser.
Definition: sql_lex.h:4458
Lex_input_stream m_lip
Definition: sql_lex.h:4493
void add_comment()
Signal that the current query has a comment.
Definition: sql_lex.h:4487
void reset(const char *found_semicolon, size_t length)
Definition: sql_lex.h:4481
Yacc_state m_yacc
Definition: sql_lex.h:4494
Parser_state()
Definition: sql_lex.h:4469
Parser_input m_input
Definition: sql_lex.h:4492
bool has_comment() const
Check whether the current query has a comment.
Definition: sql_lex.h:4489
bool m_comment
True if current query contains comments.
Definition: sql_lex.h:4501
bool init(THD *thd, const char *buff, size_t length)
Object initializer.
Definition: sql_lex.h:4477
PSI_digest_locker * m_digest_psi
Current performance digest instrumentation.
Definition: sql_lex.h:4498
Parser state for partition expression parser (.frm/DD stuff)
Definition: sql_lex.h:4507
Partition_expr_parser_state()
Definition: sql_lex.cc:1134
partition_info * result
Definition: sql_lex.h:4511
RAII class to ease the call of LEX::mark_broken() if error.
Definition: sql_lex.h:4331
~Prepare_error_tracker()
Definition: sql_lex.cc:141
THD *const thd
Definition: sql_lex.h:4337
Prepare_error_tracker(THD *thd_arg)
Definition: sql_lex.h:4333
Definition: protocol.h:32
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1123
void print_update_list(const THD *thd, String *str, enum_query_type query_type, const mem_root_deque< Item * > &fields, const mem_root_deque< Item * > &values)
Print assignments list.
Definition: sql_lex.cc:3220
void add_order_to_list(ORDER *order)
Definition: sql_lex.cc:2442
void print_delete(const THD *thd, String *str, enum_query_type query_type)
Print detail of the DELETE statement.
Definition: sql_lex.cc:2989
void add_base_options(ulonglong options)
Add base options to a query block, also update active options.
Definition: sql_lex.h:1155
uint n_scalar_subqueries
Keep track for allocation of base_ref_items: scalar subqueries may be replaced by a field during scal...
Definition: sql_lex.h:2044
Query_block * next
Intrusive double-linked list of all query blocks within the same query expression.
Definition: sql_lex.h:2284
void cleanup_all_joins()
Definition: sql_union.cc:1632
uint select_number
Query block number (used for EXPLAIN)
Definition: sql_lex.h:1985
bool subquery_in_having
HAVING clause contains subquery => we can't close tables before query processing end even if we use t...
Definition: sql_lex.h:2136
void print_where_cond(const THD *thd, String *str, enum_query_type query_type)
Print list of conditions in WHERE clause.
Definition: sql_lex.cc:3307
bool is_grouped() const
Definition: sql_lex.h:1262
void print_insert_options(String *str)
Print INSERT options.
Definition: sql_lex.cc:3148
bool m_json_agg_func_used
Definition: sql_lex.h:2343
mem_root_deque< mem_root_deque< Item * > * > * row_value_list
The VALUES items of a table value constructor.
Definition: sql_lex.h:1875
bool is_dependent() const
Definition: sql_lex.h:1762
bool with_sum_func
True if contains or aggregates set functions.
Definition: sql_lex.h:2130
Query_result * query_result() const
Definition: sql_lex.h:1140
bool is_explicitly_grouped() const
Definition: sql_lex.h:1230
Item * m_where_cond
Condition to be evaluated after all tables in a query block are joined.
Definition: sql_lex.h:2322
void print_order_by(const THD *thd, String *str, enum_query_type query_type)
Print list of items in ORDER BY clause.
Definition: sql_lex.cc:3375
olap_type olap
Indicates whether this query block contains the WITH ROLLUP clause.
Definition: sql_lex.h:2072
Item::cond_result having_value
Definition: sql_lex.h:1996
Item::cond_result cond_value
Saved values of the WHERE and HAVING clauses.
Definition: sql_lex.h:1995
bool setup_base_ref_items(THD *thd)
Setup the array containing references to base items.
Definition: sql_lex.cc:2472
uint get_in_sum_expr() const
Definition: sql_lex.h:1324
void print_values(const THD *thd, String *str, enum_query_type query_type, const mem_root_deque< mem_root_deque< Item * > * > &values, const char *prefix)
Print list of values, used in INSERT and for general VALUES clause.
Definition: sql_lex.cc:3261
bool group_fix_field
true when GROUP BY fix field called in processing of this query block
Definition: sql_lex.h:2123
void print_item_list(const THD *thd, String *str, enum_query_type query_type)
Print list of items in Query_block object.
Definition: sql_lex.cc:3196
Resolve_place resolve_place
Indicates part of query being resolved.
Definition: sql_lex.h:2014
bool m_right_joins
True if query block has right joins.
Definition: sql_lex.h:2337
Query_block * outer_query_block() const
Definition: sql_lex.h:1201
mem_root_deque< Item * > * get_fields_list()
Definition: sql_lex.h:1355
Query_block(MEM_ROOT *mem_root, Item *where, Item *having)
Construct and initialize Query_block object.
Definition: sql_lex.cc:2133
bool change_group_ref_for_func(THD *thd, Item *func, bool *changed)
bool is_implicitly_grouped() const
Definition: sql_lex.h:1237
Item * m_having_cond
Condition to be evaluated on grouped rows after grouping.
Definition: sql_lex.h:2325
Item * having_cond() const
Definition: sql_lex.h:1136
uint cond_count
number of arguments of and/or/xor in where/having/on
Definition: sql_lex.h:2028
Query_result * m_query_result
Result of this query block.
Definition: sql_lex.h:2297
void print_hints(const THD *thd, String *str, enum_query_type query_type)
Print detail of Hints.
Definition: sql_lex.cc:3069
bool accept(Select_lex_visitor *visitor)
Accept function for SELECT and DELETE.
Definition: sql_lex.cc:3434
Item ** where_cond_ref()
Definition: sql_lex.h:1134
uint max_equal_elems
maximal number of elements in multiple equalities
Definition: sql_lex.h:2030
uint table_func_count
Number of table functions in this query block.
Definition: sql_lex.h:2063
Mem_root_array< Item_exists_subselect * > * sj_candidates
Pointer to collection of subqueries candidate for semi/antijoin conversion.
Definition: sql_lex.h:2275
bool having_fix_field
true when having fix field called in processing of this query block
Definition: sql_lex.h:2121
bool has_aj_nests
Definition: sql_lex.h:2336
uint hidden_items_from_optimization
Hidden items added during optimization.
Definition: sql_lex.h:2155
Query_block * link_next
Intrusive double-linked global list of query blocks.
Definition: sql_lex.h:2293
mem_root_deque< TABLE_LIST * > * join_list
list for the currently parsed join
Definition: sql_lex.h:1951
void invalidate()
Invalidate by nulling out pointers to other Query_expressions and Query_blockes.
Definition: sql_lex.cc:2463
Opt_hints_qb * opt_hints_qb
Query-block-level hints, for this query block.
Definition: sql_lex.h:1909
Query_block ** link_prev
Definition: sql_lex.h:2294
uint with_wild
Number of wildcards used in the SELECT list.
Definition: sql_lex.h:2056
Name_resolution_context context
Context for name resolution for all column references except columns from joined tables.
Definition: sql_lex.h:1934
void make_active_options(ulonglong added_options, ulonglong removed_options)
Make active options from base options, supplied options and environment:
Definition: sql_lex.cc:2322
void set_empty_query()
Set query block as returning no data.
Definition: sql_lex.h:1734
Query_expression * slave
The first query expression contained within this query block.
Definition: sql_lex.h:2290
mem_root_deque< TABLE_LIST * > top_join_list
join list of the top level
Definition: sql_lex.h:1949
bool is_item_list_lookup
Definition: sql_lex.h:2118
void mark_as_dependent(Query_block *last, bool aggregate)
Mark all query blocks from this to 'last' as dependent.
Definition: sql_lex.cc:2358
bool save_order_properties(THD *thd, SQL_I_List< ORDER > *list, Group_list_ptrs **list_ptrs)
Helper for save_properties()
Definition: sql_lex.cc:4232
Item_sum * inner_sum_func_list
Circular linked list of aggregate functions in nested query blocks.
Definition: sql_lex.h:1976
static void print_order(const THD *thd, String *str, ORDER *order, enum_query_type query_type)
Definition: sql_lex.cc:2593
bool first_execution
This variable is required to ensure proper work of subqueries and stored procedures.
Definition: sql_lex.h:2107
void print_delete_options(String *str)
Print DELETE options.
Definition: sql_lex.cc:3140
bool add_ftfunc_to_list(Item_func_match *func)
Definition: sql_lex.cc:2455
void print_having(const THD *thd, String *str, enum_query_type query_type)
Print list of items in HAVING clause.
Definition: sql_lex.cc:3337
bool walk(Item_processor processor, enum_walk walk, uchar *arg)
Definition: sql_lex.cc:4636
bool m_agg_func_used
Definition: sql_lex.h:2342
uint materialized_derived_table_count
Number of materialized derived tables and views in this query block.
Definition: sql_lex.h:2047
List< Item_func_match > * ftfunc_list
Usually a pointer to ftfunc_list_alloc, but in UNION this is used to create fake query_block that con...
Definition: sql_lex.h:1871
uint in_sum_expr
Parse context: is inside a set function if this is positive.
Definition: sql_lex.h:2001
ha_rows get_offset(THD *thd)
Get offset for LIMIT.
Definition: sql_lex.cc:2421
enum_condition_context condition_context
Definition: sql_lex.h:2075
void set_right_joins()
Definition: sql_lex.h:1751
auto visible_fields()
Wrappers over fields / get_fields_list() that hide items where item->hidden, meant for range-based fo...
Definition: sql_lex.h:1359
uint n_sum_items
Number of Item_sum-derived objects in this SELECT.
Definition: sql_lex.h:2038
void renumber(LEX *lex)
Renumber query blocks of contained query expressions.
Definition: sql_lex.cc:4363
bool is_part_of_union() const
Return true if this query block is part of a UNION.
Definition: sql_lex.h:1721
Query_block * next_query_block() const
Definition: sql_lex.h:1202
List< Window > m_windows
All windows defined on the select, both named and inlined.
Definition: sql_lex.h:1865
uint leaf_table_count
Number of leaf tables in this query block.
Definition: sql_lex.h:2059
void set_having_cond(Item *cond)
Definition: sql_lex.h:1138
bool m_use_select_limit
If true, use select_limit to limit number of rows selected.
Definition: sql_lex.h:2143
bool has_limit() const
Definition: sql_lex.h:1296
bool validate_outermost_option(LEX *lex, const char *wrong_option) const
Check if an option that can be used only for an outer-most query block is applicable to this query bl...
Definition: sql_lex.cc:4541
void destroy()
Destroy contained objects, in particular temporary tables which may have their own mem_roots.
Definition: sql_union.cc:1643
uint derived_table_count
Number of derived tables and views in this query block.
Definition: sql_lex.h:2061
bool is_ordered() const
Definition: sql_lex.h:1273
Item * where_cond() const
Definition: sql_lex.h:1133
uint partitioned_table_count
Number of partitioned tables.
Definition: sql_lex.h:2049
Prealloced_array< Item_rollup_group_item *, 4 > rollup_group_items
Definition: sql_lex.h:1903
void print_insert_fields(const THD *thd, String *str, enum_query_type query_type)
Print column list to be inserted into.
Definition: sql_lex.cc:3241
void cleanup(THD *thd, bool full)
Cleanup this subtree (this Query_block and all nested Query_blockes and Query_expressions).
Definition: sql_union.cc:1605
bool json_agg_func_used() const
Definition: sql_lex.h:1744
bool get_optimizable_conditions(THD *thd, Item **new_where, Item **new_having)
Returns disposable copies of WHERE/HAVING/ON conditions.
Definition: sql_lex.cc:4439
uint between_count
number of between predicates in where/having/on
Definition: sql_lex.h:2029
void include_in_global(Query_block **plink)
Include query block into global list.
Definition: sql_lex.cc:4379
bool agg_func_used() const
Definition: sql_lex.h:1743
Resolve_place
Three fields used by semi-join transformations to know when semi-join is possible,...
Definition: sql_lex.h:2007
@ RESOLVE_HAVING
Definition: sql_lex.h:2011