1 /*
2 Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 /* A lexical scanner on a temporary buffer with a yacc interface */
25
26 #include "sql/sql_lex.h"
27
28 #include <limits.h>
29 #include <stdlib.h>
30 #include <algorithm> // find_if, iter_swap, reverse
31
32 #include "m_ctype.h"
33 #include "my_alloc.h"
34 #include "my_dbug.h"
35 #include "my_macros.h"
36 #include "mysql/mysql_lex_string.h"
37 #include "mysql/service_mysql_alloc.h"
38 #include "mysql_version.h" // MYSQL_VERSION_ID
39 #include "mysqld_error.h"
40 #include "prealloced_array.h" // Prealloced_array
41 #include "sql/current_thd.h"
42 #include "sql/derror.h"
43 #include "sql/item_func.h"
44 #include "sql/item_subselect.h"
45 #include "sql/mysqld.h" // table_alias_charset
46 #include "sql/nested_join.h"
47 #include "sql/parse_location.h"
48 #include "sql/parse_tree_nodes.h" // PT_with_clause
49 #include "sql/protocol.h"
50 #include "sql/select_lex_visitor.h"
51 #include "sql/sp_head.h" // sp_head
52 #include "sql/sql_admin.h"
53 #include "sql/sql_base.h"
54 #include "sql/sql_class.h" // THD
55 #include "sql/sql_error.h"
56 #include "sql/sql_insert.h" // Sql_cmd_insert_base
57 #include "sql/sql_lex_hash.h"
58 #include "sql/sql_lex_hints.h"
59 #include "sql/sql_optimizer.h" // JOIN
60 #include "sql/sql_parse.h" // add_to_list
61 #include "sql/sql_plugin.h" // plugin_unlock_list
62 #include "sql/sql_profile.h"
63 #include "sql/sql_show.h" // append_identifier
64 #include "sql/sql_table.h" // primary_key_name
65 #include "sql/sql_yacc.h"
66 #include "sql/system_variables.h"
67 #include "sql/table_function.h"
68 #include "sql/window.h"
69 #include "sql_update.h" // Sql_cmd_update
70 #include "template_utils.h"
71
72 extern int HINT_PARSER_parse(THD *thd, Hint_scanner *scanner,
73 PT_hint_list **ret);
74
75 static int lex_one_token(Lexer_yystype *yylval, THD *thd);
76
77 /*
78 We are using pointer to this variable for distinguishing between assignment
79 to NEW row field (when parsing trigger definition) and structured variable.
80 */
81
82 sys_var *trg_new_row_fake_var = (sys_var *)0x01;
83
84 /**
85 LEX_STRING constant for null-string to be used in parser and other places.
86 */
87 const LEX_STRING null_lex_str = {nullptr, 0};
88 /**
89 Mapping from enum values in enum_binlog_stmt_unsafe to error codes.
90
91 @note The order of the elements of this array must correspond to
92 the order of elements in enum_binlog_stmt_unsafe.
93
94 Todo/fixme Bug#22860121 ER_BINLOG_UNSAFE_* FAMILY OF ERROR CODES IS UNUSED
95 suggests to turn ER_BINLOG_UNSAFE* to private consts/messages.
96 */
97 const int
98 Query_tables_list::binlog_stmt_unsafe_errcode[BINLOG_STMT_UNSAFE_COUNT] = {
99 ER_BINLOG_UNSAFE_LIMIT,
100 ER_BINLOG_UNSAFE_SYSTEM_TABLE,
101 ER_BINLOG_UNSAFE_AUTOINC_COLUMNS,
102 ER_BINLOG_UNSAFE_UDF,
103 ER_BINLOG_UNSAFE_SYSTEM_VARIABLE,
104 ER_BINLOG_UNSAFE_SYSTEM_FUNCTION,
105 ER_BINLOG_UNSAFE_NONTRANS_AFTER_TRANS,
106 ER_BINLOG_UNSAFE_MULTIPLE_ENGINES_AND_SELF_LOGGING_ENGINE,
107 ER_BINLOG_UNSAFE_MIXED_STATEMENT,
108 ER_BINLOG_UNSAFE_INSERT_IGNORE_SELECT,
109 ER_BINLOG_UNSAFE_INSERT_SELECT_UPDATE,
110 ER_BINLOG_UNSAFE_WRITE_AUTOINC_SELECT,
111 ER_BINLOG_UNSAFE_REPLACE_SELECT,
112 ER_BINLOG_UNSAFE_CREATE_IGNORE_SELECT,
113 ER_BINLOG_UNSAFE_CREATE_REPLACE_SELECT,
114 ER_BINLOG_UNSAFE_CREATE_SELECT_AUTOINC,
115 ER_BINLOG_UNSAFE_UPDATE_IGNORE,
116 ER_BINLOG_UNSAFE_INSERT_TWO_KEYS,
117 ER_BINLOG_UNSAFE_AUTOINC_NOT_FIRST,
118 ER_BINLOG_UNSAFE_FULLTEXT_PLUGIN,
119 ER_BINLOG_UNSAFE_SKIP_LOCKED,
120 ER_BINLOG_UNSAFE_NOWAIT,
121 ER_BINLOG_UNSAFE_XA,
122 ER_BINLOG_UNSAFE_DEFAULT_EXPRESSION_IN_SUBSTATEMENT};
123
124 /*
125 Names of the index hints (for error messages). Keep in sync with
126 index_hint_type
127 */
128
129 const char *index_hint_type_name[] = {"IGNORE INDEX", "USE INDEX",
130 "FORCE INDEX"};
131
132 /**
133 @note The order of the elements of this array must correspond to
134 the order of elements in type_enum
135 */
136 const char *
137 SELECT_LEX::type_str[static_cast<int>(enum_explain_type::EXPLAIN_total)] = {
138 "NONE", "PRIMARY", "SIMPLE", "DERIVED",
139 "SUBQUERY", "UNION", "UNION RESULT", "MATERIALIZED"};
140
Table_ident(Protocol * protocol,const LEX_CSTRING & db_arg,const LEX_CSTRING & table_arg,bool force)141 Table_ident::Table_ident(Protocol *protocol, const LEX_CSTRING &db_arg,
142 const LEX_CSTRING &table_arg, bool force)
143 : table(table_arg), sel(nullptr), table_function(nullptr) {
144 if (!force && protocol->has_client_capability(CLIENT_NO_SCHEMA))
145 db = NULL_CSTR;
146 else
147 db = db_arg;
148 }
149
lex_init(void)150 bool lex_init(void) {
151 DBUG_TRACE;
152
153 for (CHARSET_INFO **cs = all_charsets;
154 cs < all_charsets + array_elements(all_charsets) - 1; cs++) {
155 if (*cs && (*cs)->ctype && is_supported_parser_charset(*cs)) {
156 if (init_state_maps(*cs)) return true; // OOM
157 }
158 }
159
160 return false;
161 }
162
lex_free(void)163 void lex_free(void) { // Call this when daemon ends
164 DBUG_TRACE;
165 }
166
reset()167 void st_parsing_options::reset() {
168 allows_variable = true;
169 allows_select_into = true;
170 }
171
172 /**
173 Cleans slave connection info.
174 */
reset()175 void struct_slave_connection::reset() {
176 user = nullptr;
177 password = nullptr;
178 plugin_auth = nullptr;
179 plugin_dir = nullptr;
180 }
181
182 /**
183 Perform initialization of Lex_input_stream instance.
184
185 Basically, a buffer for a pre-processed query. This buffer should be large
186 enough to keep a multi-statement query. The allocation is done once in
187 Lex_input_stream::init() in order to prevent memory pollution when
188 the server is processing large multi-statement queries.
189 */
190
init(THD * thd,const char * buff,size_t length)191 bool Lex_input_stream::init(THD *thd, const char *buff, size_t length) {
192 DBUG_EXECUTE_IF("bug42064_simulate_oom",
193 DBUG_SET("+d,simulate_out_of_memory"););
194
195 query_charset = thd->charset();
196
197 m_cpp_buf = (char *)thd->alloc(length + 1);
198
199 DBUG_EXECUTE_IF("bug42064_simulate_oom",
200 DBUG_SET("-d,bug42064_simulate_oom"););
201
202 if (m_cpp_buf == nullptr) return true;
203
204 m_thd = thd;
205 reset(buff, length);
206
207 return false;
208 }
209
210 /**
211 Prepare Lex_input_stream instance state for use for handling next SQL
212 statement.
213
214 It should be called between two statements in a multi-statement query.
215 The operation resets the input stream to the beginning-of-parse state,
216 but does not reallocate m_cpp_buf.
217 */
218
reset(const char * buffer,size_t length)219 void Lex_input_stream::reset(const char *buffer, size_t length) {
220 yylineno = 1;
221 yytoklen = 0;
222 yylval = nullptr;
223 lookahead_token = grammar_selector_token;
224 static Lexer_yystype dummy_yylval;
225 lookahead_yylval = &dummy_yylval;
226 skip_digest = false;
227 /*
228 Lex_input_stream modifies the query string in one special case (sic!).
229 yyUnput() modifises the string when patching version comments.
230 This is done to prevent newer slaves from executing a different
231 statement than older masters.
232
233 For now, cast away const here. This means that e.g. SHOW PROCESSLIST
234 can see partially patched query strings. It would be better if we
235 could replicate the query string as is and have the slave take the
236 master version into account.
237 */
238 m_ptr = const_cast<char *>(buffer);
239 m_tok_start = nullptr;
240 m_tok_end = nullptr;
241 m_end_of_query = buffer + length;
242 m_buf = buffer;
243 m_buf_length = length;
244 m_echo = true;
245 m_cpp_tok_start = nullptr;
246 m_cpp_tok_end = nullptr;
247 m_body_utf8 = nullptr;
248 m_cpp_utf8_processed_ptr = nullptr;
249 next_state = MY_LEX_START;
250 found_semicolon = nullptr;
251 ignore_space = m_thd->variables.sql_mode & MODE_IGNORE_SPACE;
252 stmt_prepare_mode = false;
253 multi_statements = true;
254 in_comment = NO_COMMENT;
255 m_underscore_cs = nullptr;
256 m_cpp_ptr = m_cpp_buf;
257 }
258
259 /**
260 The operation is called from the parser in order to
261 1) designate the intention to have utf8 body;
262 1) Indicate to the lexer that we will need a utf8 representation of this
263 statement;
264 2) Determine the beginning of the body.
265
266 @param thd Thread context.
267 @param begin_ptr Pointer to the start of the body in the pre-processed
268 buffer.
269 */
270
body_utf8_start(THD * thd,const char * begin_ptr)271 void Lex_input_stream::body_utf8_start(THD *thd, const char *begin_ptr) {
272 DBUG_ASSERT(begin_ptr);
273 DBUG_ASSERT(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
274
275 size_t body_utf8_length =
276 (m_buf_length / thd->variables.character_set_client->mbminlen) *
277 my_charset_utf8_bin.mbmaxlen;
278
279 m_body_utf8 = (char *)thd->alloc(body_utf8_length + 1);
280 m_body_utf8_ptr = m_body_utf8;
281 *m_body_utf8_ptr = 0;
282
283 m_cpp_utf8_processed_ptr = begin_ptr;
284 }
285
286 /**
287 @brief The operation appends unprocessed part of pre-processed buffer till
288 the given pointer (ptr) and sets m_cpp_utf8_processed_ptr to end_ptr.
289
290 The idea is that some tokens in the pre-processed buffer (like character
291 set introducers) should be skipped.
292
293 Example:
294 CPP buffer: SELECT 'str1', _latin1 'str2';
295 m_cpp_utf8_processed_ptr -- points at the "SELECT ...";
296 In order to skip "_latin1", the following call should be made:
297 body_utf8_append(<pointer to "_latin1 ...">, <pointer to " 'str2'...">)
298
299 @param ptr Pointer in the pre-processed buffer, which specifies the
300 end of the chunk, which should be appended to the utf8
301 body.
302 @param end_ptr Pointer in the pre-processed buffer, to which
303 m_cpp_utf8_processed_ptr will be set in the end of the
304 operation.
305 */
306
body_utf8_append(const char * ptr,const char * end_ptr)307 void Lex_input_stream::body_utf8_append(const char *ptr, const char *end_ptr) {
308 DBUG_ASSERT(m_cpp_buf <= ptr && ptr <= m_cpp_buf + m_buf_length);
309 DBUG_ASSERT(m_cpp_buf <= end_ptr && end_ptr <= m_cpp_buf + m_buf_length);
310
311 if (!m_body_utf8) return;
312
313 if (m_cpp_utf8_processed_ptr >= ptr) return;
314
315 size_t bytes_to_copy = ptr - m_cpp_utf8_processed_ptr;
316
317 memcpy(m_body_utf8_ptr, m_cpp_utf8_processed_ptr, bytes_to_copy);
318 m_body_utf8_ptr += bytes_to_copy;
319 *m_body_utf8_ptr = 0;
320
321 m_cpp_utf8_processed_ptr = end_ptr;
322 }
323
324 /**
325 The operation appends unprocessed part of the pre-processed buffer till
326 the given pointer (ptr) and sets m_cpp_utf8_processed_ptr to ptr.
327
328 @param ptr Pointer in the pre-processed buffer, which specifies the end
329 of the chunk, which should be appended to the utf8 body.
330 */
331
body_utf8_append(const char * ptr)332 void Lex_input_stream::body_utf8_append(const char *ptr) {
333 body_utf8_append(ptr, ptr);
334 }
335
336 /**
337 The operation converts the specified text literal to the utf8 and appends
338 the result to the utf8-body.
339
340 @param thd Thread context.
341 @param txt Text literal.
342 @param txt_cs Character set of the text literal.
343 @param end_ptr Pointer in the pre-processed buffer, to which
344 m_cpp_utf8_processed_ptr will be set in the end of the
345 operation.
346 */
347
body_utf8_append_literal(THD * thd,const LEX_STRING * txt,const CHARSET_INFO * txt_cs,const char * end_ptr)348 void Lex_input_stream::body_utf8_append_literal(THD *thd, const LEX_STRING *txt,
349 const CHARSET_INFO *txt_cs,
350 const char *end_ptr) {
351 if (!m_cpp_utf8_processed_ptr) return;
352
353 LEX_STRING utf_txt;
354
355 if (!my_charset_same(txt_cs, &my_charset_utf8_general_ci)) {
356 thd->convert_string(&utf_txt, &my_charset_utf8_general_ci, txt->str,
357 txt->length, txt_cs);
358 } else {
359 utf_txt.str = txt->str;
360 utf_txt.length = txt->length;
361 }
362
363 /* NOTE: utf_txt.length is in bytes, not in symbols. */
364
365 memcpy(m_body_utf8_ptr, utf_txt.str, utf_txt.length);
366 m_body_utf8_ptr += utf_txt.length;
367 *m_body_utf8_ptr = 0;
368
369 m_cpp_utf8_processed_ptr = end_ptr;
370 }
371
add_digest_token(uint token,Lexer_yystype * yylval)372 void Lex_input_stream::add_digest_token(uint token, Lexer_yystype *yylval) {
373 if (m_digest != nullptr) {
374 m_digest = digest_add_token(m_digest, token, yylval);
375 }
376 }
377
reduce_digest_token(uint token_left,uint token_right)378 void Lex_input_stream::reduce_digest_token(uint token_left, uint token_right) {
379 if (m_digest != nullptr) {
380 m_digest = digest_reduce_token(m_digest, token_left, token_right);
381 }
382 }
383
assert_ok_set_current_select()384 void LEX::assert_ok_set_current_select() {
385 // (2) Only owning thread could change m_current_select
386 // (1) bypass for bootstrap and "new THD"
387 DBUG_ASSERT(!current_thd || !thd || //(1)
388 thd == current_thd); //(2)
389 }
390
~LEX()391 LEX::~LEX() {
392 destroy_query_tables_list();
393 plugin_unlock_list(nullptr, plugins.begin(), plugins.size());
394 unit = nullptr; // Created in mem_root - no destructor
395 select_lex = nullptr;
396 m_current_select = nullptr;
397 }
398
399 /**
400 Reset a LEX object so that it is ready for a new query preparation
401 and execution.
402 Pointers to query expression and query block objects are set to NULL.
403 This is correct, as they point into a mem_root that has been recycled.
404 */
405
reset()406 void LEX::reset() {
407 // CREATE VIEW
408 create_view_mode = enum_view_create_mode::VIEW_CREATE_NEW;
409 create_view_algorithm = VIEW_ALGORITHM_UNDEFINED;
410 create_view_suid = true;
411
412 context_stack.empty();
413 unit = nullptr;
414 select_lex = nullptr;
415 m_current_select = nullptr;
416 all_selects_list = nullptr;
417
418 bulk_insert_row_cnt = 0;
419
420 purge_value_list.empty();
421
422 kill_value_list.empty();
423
424 set_var_list.empty();
425 param_list.empty();
426 prepared_stmt_params.empty();
427 context_analysis_only = 0;
428 safe_to_cache_query = true;
429 insert_table = nullptr;
430 insert_table_leaf = nullptr;
431 parsing_options.reset();
432 alter_info = nullptr;
433 part_info = nullptr;
434 duplicates = DUP_ERROR;
435 ignore = false;
436 spname = nullptr;
437 sphead = nullptr;
438 set_sp_current_parsing_ctx(nullptr);
439 m_sql_cmd = nullptr;
440 query_tables = nullptr;
441 reset_query_tables_list(false);
442 expr_allows_subselect = true;
443 use_only_table_context = false;
444 contains_plaintext_password = false;
445 keep_diagnostics = DA_KEEP_NOTHING;
446 m_statement_options = 0;
447 next_binlog_file_nr = 0;
448
449 name.str = nullptr;
450 name.length = 0;
451 event_parse_data = nullptr;
452 profile_options = PROFILE_NONE;
453 select_number = 0;
454 allow_sum_func = 0;
455 m_deny_window_func = 0;
456 in_sum_func = nullptr;
457 create_info = nullptr;
458 server_options.reset();
459 explain_format = nullptr;
460 is_explain_analyze = false;
461 is_lex_started = true;
462 reset_slave_info.all = false;
463 mi.channel = nullptr;
464
465 wild = nullptr;
466 mark_broken(false);
467 reset_exec_started();
468 max_execution_time = 0;
469 reparse_common_table_expr_at = 0;
470 opt_hints_global = nullptr;
471 binlog_need_explicit_defaults_ts = false;
472 m_extended_show = false;
473 option_type = OPT_DEFAULT;
474 check_opt = HA_CHECK_OPT();
475
476 clear_privileges();
477 grant_as.cleanup();
478 alter_user_attribute = enum_alter_user_attribute::ALTER_USER_COMMENT_NOT_USED;
479 }
480
481 /**
482 Call lex_start() before every query that is to be prepared and executed.
483 Because of this, it's critical not to do too many things here. (We already
484 do too much)
485
486 The function creates a select_lex and a select_lex_unit object.
487 These objects should rather be created by the parser bottom-up.
488 */
489
lex_start(THD * thd)490 bool lex_start(THD *thd) {
491 DBUG_TRACE;
492
493 LEX *lex = thd->lex;
494
495 lex->thd = thd;
496 lex->reset();
497 // Initialize the cost model to be used for this query
498 thd->init_cost_model();
499
500 const bool status = lex->new_top_level_query();
501 DBUG_ASSERT(lex->current_select() == nullptr);
502 lex->m_current_select = lex->select_lex;
503
504 lex->m_IS_table_stats.invalidate_cache();
505 lex->m_IS_tablespace_stats.invalidate_cache();
506
507 return status;
508 }
509
510 /**
511 Call this function after preparation and execution of a query.
512 */
513
lex_end(LEX * lex)514 void lex_end(LEX *lex) {
515 DBUG_TRACE;
516 DBUG_PRINT("enter", ("lex: %p", lex));
517
518 /* release used plugins */
519 if (!lex->plugins.empty()) /* No function call and no mutex if no plugins. */
520 {
521 plugin_unlock_list(nullptr, lex->plugins.begin(), lex->plugins.size());
522 }
523 lex->plugins.clear();
524
525 sp_head::destroy(lex->sphead);
526 lex->sphead = nullptr;
527 }
528
new_empty_query_block()529 SELECT_LEX *LEX::new_empty_query_block() {
530 SELECT_LEX *select =
531 new (thd->mem_root) SELECT_LEX(thd->mem_root, nullptr, nullptr);
532 if (select == nullptr) return nullptr; /* purecov: inspected */
533
534 select->parent_lex = this;
535
536 return select;
537 }
538
create_query_expr_and_block(THD * thd,SELECT_LEX * current_select,Item * where,Item * having,enum_parsing_context ctx)539 SELECT_LEX_UNIT *LEX::create_query_expr_and_block(THD *thd,
540 SELECT_LEX *current_select,
541 Item *where, Item *having,
542 enum_parsing_context ctx) {
543 if (current_select != nullptr &&
544 current_select->nest_level >= (int)MAX_SELECT_NESTING) {
545 my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT, MYF(0),
546 MAX_SELECT_NESTING);
547 return nullptr;
548 }
549
550 auto *const new_expression = new (thd->mem_root) SELECT_LEX_UNIT(ctx);
551 if (new_expression == nullptr) return nullptr;
552
553 auto *const new_select =
554 new (thd->mem_root) SELECT_LEX(thd->mem_root, where, having);
555 if (new_select == nullptr) return nullptr;
556
557 // Link the new query expression below the current query block, if any
558 if (current_select != nullptr)
559 new_expression->include_down(this, current_select);
560
561 new_select->include_down(this, new_expression);
562
563 new_select->parent_lex = this;
564 new_select->include_in_global(&this->all_selects_list);
565
566 return new_expression;
567 }
568
569 /**
570 Create new select_lex_unit and select_lex objects for a query block,
571 which can be either a top-level query or a subquery.
572 For the second and subsequent query block of a UNION query, use
573 LEX::new_union_query() instead.
574 Set the new select_lex as the current select_lex of the LEX object.
575
576 @param curr_select current query specification
577
578 @return new query specification if successful, NULL if error
579 */
new_query(SELECT_LEX * curr_select)580 SELECT_LEX *LEX::new_query(SELECT_LEX *curr_select) {
581 DBUG_TRACE;
582
583 Name_resolution_context *outer_context = current_context();
584
585 enum_parsing_context parsing_place =
586 curr_select ? curr_select->parsing_place : CTX_NONE;
587
588 SELECT_LEX_UNIT *const sel_unit = create_query_expr_and_block(
589 thd, curr_select, nullptr, nullptr, parsing_place);
590 if (sel_unit == nullptr) return nullptr;
591 SELECT_LEX *const select = sel_unit->first_select();
592
593 if (select->set_context(nullptr)) return nullptr; /* purecov: inspected */
594 /*
595 Assume that a subquery has an outer name resolution context
596 (even a non-lateral derived table may have outer references).
597 When we come here for a view, it's when we parse the view (in
598 open_tables()): we parse it as a standalone query, where parsing_place
599 is CTX_NONE, so the outer context is set to nullptr. Then we'll resolve the
600 view's query (thus, using no outer context). Later we may merge the
601 view's query, but that happens after resolution, so there's no chance that
602 a view "looks outside" (uses outer references). An assertion in
603 resolve_derived() checks this.
604 */
605 if (parsing_place == CTX_NONE) // Outer-most query block
606 {
607 } else if (parsing_place == CTX_INSERT_VALUES) {
608 /*
609 Outer references are not allowed for
610 - subqueries in INSERT ... VALUES clauses
611 */
612 DBUG_ASSERT(select->context.outer_context == nullptr);
613 } else {
614 select->context.outer_context = outer_context;
615 }
616 /*
617 in subquery is SELECT query and we allow resolution of names in SELECT
618 list
619 */
620 select->context.resolve_in_select_list = true;
621 DBUG_PRINT("outer_field",
622 ("ctx %p <-> SL# %d", &select->context, select->select_number));
623
624 return select;
625 }
626
627 /**
628 Create new select_lex object for all branches of a UNION except the left-most
629 one.
630 Set the new select_lex as the current select_lex of the LEX object.
631
632 @param curr_select current query specification
633 @param distinct True if part of UNION DISTINCT query
634
635 @return new query specification if successful, NULL if an error occurred.
636 */
637
new_union_query(SELECT_LEX * curr_select,bool distinct)638 SELECT_LEX *LEX::new_union_query(SELECT_LEX *curr_select, bool distinct) {
639 DBUG_TRACE;
640
641 DBUG_ASSERT(unit != nullptr && select_lex != nullptr);
642
643 // Is this the outer-most query expression?
644 bool const outer_most = curr_select->master_unit() == unit;
645 /*
646 Only the last SELECT can have INTO. Since the grammar won't allow INTO in
647 a nested SELECT, we make this check only when creating a query block on
648 the outer-most level:
649 */
650 if (outer_most && result) {
651 my_error(ER_MISPLACED_INTO, MYF(0));
652 return nullptr;
653 }
654
655 SELECT_LEX *const select = new_empty_query_block();
656 if (!select) return nullptr; /* purecov: inspected */
657
658 select->include_neighbour(this, curr_select);
659
660 SELECT_LEX_UNIT *const sel_unit = select->master_unit();
661
662 if (!sel_unit->fake_select_lex && sel_unit->add_fake_select_lex(thd))
663 return nullptr; /* purecov: inspected */
664
665 if (select->set_context(sel_unit->first_select()->context.outer_context))
666 return nullptr; /* purecov: inspected */
667
668 select->include_in_global(&all_selects_list);
669
670 select->linkage = UNION_TYPE;
671
672 if (distinct) /* UNION DISTINCT - remember position */
673 sel_unit->union_distinct = select;
674
675 /*
676 By default we assume that this is a regular subquery, in which resolution
677 of names in SELECT list is allowed.
678 */
679 select->context.resolve_in_select_list = true;
680
681 return select;
682 }
683
684 /**
685 Given a LEX object, create a query expression object (select_lex_unit) and
686 a query block object (select_lex).
687
688 @return false if successful, true if error
689 */
690
new_top_level_query()691 bool LEX::new_top_level_query() {
692 DBUG_TRACE;
693
694 // Assure that the LEX does not contain any query expression already
695 DBUG_ASSERT(unit == nullptr && select_lex == nullptr);
696
697 // Check for the special situation when using INTO OUTFILE and LOAD DATA.
698 DBUG_ASSERT(result == nullptr);
699
700 select_lex = new_query(nullptr);
701 if (select_lex == nullptr) return true; /* purecov: inspected */
702
703 unit = select_lex->master_unit();
704
705 return false;
706 }
707
708 /**
709 Initialize a LEX object, a query expression object (select_lex_unit) and
710 a query block object (select_lex).
711 All objects are passed as pointers so they can be stack-allocated.
712 The purpose of this structure is for short-lived procedures that need a
713 LEX and a query block object.
714
715 Do not extend the struct with more query objects after creation.
716
717 The struct can be abandoned after use, no cleanup is needed.
718
719 @param sel_unit Pointer to the query expression object
720 @param select Pointer to the query block object
721 */
722
new_static_query(SELECT_LEX_UNIT * sel_unit,SELECT_LEX * select)723 void LEX::new_static_query(SELECT_LEX_UNIT *sel_unit, SELECT_LEX *select)
724
725 {
726 DBUG_TRACE;
727
728 reset();
729
730 DBUG_ASSERT(unit == nullptr && select_lex == nullptr &&
731 current_select() == nullptr);
732
733 select->parent_lex = this;
734
735 select->include_down(this, sel_unit);
736
737 select->include_in_global(&all_selects_list);
738
739 (void)select->set_context(nullptr);
740
741 select_lex = select;
742 unit = sel_unit;
743
744 set_current_select(select);
745
746 select->context.resolve_in_select_list = true;
747 }
748
~Yacc_state()749 Yacc_state::~Yacc_state() {
750 if (yacc_yyss) {
751 my_free(yacc_yyss);
752 my_free(yacc_yyvs);
753 my_free(yacc_yyls);
754 }
755 }
756
consume_optimizer_hints(Lex_input_stream * lip)757 static bool consume_optimizer_hints(Lex_input_stream *lip) {
758 const my_lex_states *state_map = lip->query_charset->state_maps->main_map;
759 int whitespace = 0;
760 uchar c = lip->yyPeek();
761 size_t newlines = 0;
762
763 for (; state_map[c] == MY_LEX_SKIP;
764 whitespace++, c = lip->yyPeekn(whitespace)) {
765 if (c == '\n') newlines++;
766 }
767
768 if (lip->yyPeekn(whitespace) == '/' && lip->yyPeekn(whitespace + 1) == '*' &&
769 lip->yyPeekn(whitespace + 2) == '+') {
770 lip->yylineno += newlines;
771 lip->yySkipn(whitespace); // skip whitespace
772
773 Hint_scanner hint_scanner(lip->m_thd, lip->yylineno, lip->get_ptr(),
774 lip->get_end_of_query() - lip->get_ptr(),
775 lip->m_digest);
776 PT_hint_list *hint_list = nullptr;
777 int rc = HINT_PARSER_parse(lip->m_thd, &hint_scanner, &hint_list);
778 if (rc == 2)
779 return true; // Bison's internal OOM error
780 else if (rc == 1) {
781 /*
782 This branch is for 2 cases:
783 1. YYABORT in the hint parser grammar (we use it to process OOM errors),
784 2. open commentary error.
785 */
786 lip->start_token(); // adjust error message text pointer to "/*+"
787 return true;
788 } else {
789 lip->yylineno = hint_scanner.get_lineno();
790 lip->yySkipn(hint_scanner.get_ptr() - lip->get_ptr());
791 lip->yylval->optimizer_hints = hint_list; // NULL in case of syntax error
792 lip->m_digest =
793 hint_scanner.get_digest(); // NULL is digest buf. is full.
794 return false;
795 }
796 } else
797 return false;
798 }
799
find_keyword(Lex_input_stream * lip,uint len,bool function)800 static int find_keyword(Lex_input_stream *lip, uint len, bool function) {
801 const char *tok = lip->get_tok_start();
802
803 const SYMBOL *symbol =
804 function ? Lex_hash::sql_keywords_and_funcs.get_hash_symbol(tok, len)
805 : Lex_hash::sql_keywords.get_hash_symbol(tok, len);
806
807 if (symbol) {
808 lip->yylval->keyword.symbol = symbol;
809 lip->yylval->keyword.str = const_cast<char *>(tok);
810 lip->yylval->keyword.length = len;
811
812 if ((symbol->tok == NOT_SYM) &&
813 (lip->m_thd->variables.sql_mode & MODE_HIGH_NOT_PRECEDENCE))
814 return NOT2_SYM;
815 if ((symbol->tok == OR_OR_SYM) &&
816 !(lip->m_thd->variables.sql_mode & MODE_PIPES_AS_CONCAT)) {
817 push_deprecated_warn(lip->m_thd, "|| as a synonym for OR", "OR");
818 return OR2_SYM;
819 }
820
821 lip->yylval->optimizer_hints = nullptr;
822 if (symbol->group & SG_HINTABLE_KEYWORDS) {
823 lip->add_digest_token(symbol->tok, lip->yylval);
824 if (consume_optimizer_hints(lip)) return ABORT_SYM;
825 lip->skip_digest = true;
826 }
827
828 return symbol->tok;
829 }
830 return 0;
831 }
832
833 /*
834 Check if name is a keyword
835
836 SYNOPSIS
837 is_keyword()
838 name checked name (must not be empty)
839 len length of checked name
840
841 RETURN VALUES
842 0 name is a keyword
843 1 name isn't a keyword
844 */
845
is_keyword(const char * name,size_t len)846 bool is_keyword(const char *name, size_t len) {
847 DBUG_ASSERT(len != 0);
848 return Lex_hash::sql_keywords.get_hash_symbol(name, len) != nullptr;
849 }
850
851 /**
852 Check if name is a sql function
853
854 @param name checked name
855
856 @return is this a native function or not
857 @retval 0 name is a function
858 @retval 1 name isn't a function
859 */
860
is_lex_native_function(const LEX_STRING * name)861 bool is_lex_native_function(const LEX_STRING *name) {
862 DBUG_ASSERT(name != nullptr);
863 return Lex_hash::sql_keywords_and_funcs.get_hash_symbol(
864 name->str, (uint)name->length) != nullptr;
865 }
866
867 /* make a copy of token before ptr and set yytoklen */
868
get_token(Lex_input_stream * lip,uint skip,uint length)869 static LEX_STRING get_token(Lex_input_stream *lip, uint skip, uint length) {
870 LEX_STRING tmp;
871 lip->yyUnget(); // ptr points now after last token char
872 tmp.length = lip->yytoklen = length;
873 tmp.str = lip->m_thd->strmake(lip->get_tok_start() + skip, tmp.length);
874
875 lip->m_cpp_text_start = lip->get_cpp_tok_start() + skip;
876 lip->m_cpp_text_end = lip->m_cpp_text_start + tmp.length;
877
878 return tmp;
879 }
880
881 /*
882 todo:
883 There are no dangerous charsets in mysql for function
884 get_quoted_token yet. But it should be fixed in the
885 future to operate multichar strings (like ucs2)
886 */
887
get_quoted_token(Lex_input_stream * lip,uint skip,uint length,char quote)888 static LEX_STRING get_quoted_token(Lex_input_stream *lip, uint skip,
889 uint length, char quote) {
890 LEX_STRING tmp;
891 const char *from, *end;
892 char *to;
893 lip->yyUnget(); // ptr points now after last token char
894 tmp.length = lip->yytoklen = length;
895 tmp.str = (char *)lip->m_thd->alloc(tmp.length + 1);
896 from = lip->get_tok_start() + skip;
897 to = tmp.str;
898 end = to + length;
899
900 lip->m_cpp_text_start = lip->get_cpp_tok_start() + skip;
901 lip->m_cpp_text_end = lip->m_cpp_text_start + length;
902
903 for (; to != end;) {
904 if ((*to++ = *from++) == quote) {
905 from++; // Skip double quotes
906 lip->m_cpp_text_start++;
907 }
908 }
909 *to = 0; // End null for safety
910 return tmp;
911 }
912
913 /*
914 Return an unescaped text literal without quotes
915 Fix sometimes to do only one scan of the string
916 */
917
get_text(Lex_input_stream * lip,int pre_skip,int post_skip)918 static char *get_text(Lex_input_stream *lip, int pre_skip, int post_skip) {
919 uchar c, sep;
920 uint found_escape = 0;
921 const CHARSET_INFO *cs = lip->m_thd->charset();
922
923 lip->tok_bitmap = 0;
924 sep = lip->yyGetLast(); // String should end with this
925 while (!lip->eof()) {
926 c = lip->yyGet();
927 lip->tok_bitmap |= c;
928 {
929 int l;
930 if (use_mb(cs) &&
931 (l = my_ismbchar(cs, lip->get_ptr() - 1, lip->get_end_of_query()))) {
932 lip->skip_binary(l - 1);
933 continue;
934 }
935 }
936 if (c == '\\' && !(lip->m_thd->variables.sql_mode &
937 MODE_NO_BACKSLASH_ESCAPES)) { // Escaped character
938 found_escape = 1;
939 if (lip->eof()) return nullptr;
940 lip->yySkip();
941 } else if (c == sep) {
942 if (c == lip->yyGet()) // Check if two separators in a row
943 {
944 found_escape = 1; // duplicate. Remember for delete
945 continue;
946 } else
947 lip->yyUnget();
948
949 /* Found end. Unescape and return string */
950 const char *str, *end;
951 char *start;
952
953 str = lip->get_tok_start();
954 end = lip->get_ptr();
955 /* Extract the text from the token */
956 str += pre_skip;
957 end -= post_skip;
958 DBUG_ASSERT(end >= str);
959
960 if (!(start =
961 static_cast<char *>(lip->m_thd->alloc((uint)(end - str) + 1))))
962 return const_cast<char *>(""); // MEM_ROOT has set error flag
963
964 lip->m_cpp_text_start = lip->get_cpp_tok_start() + pre_skip;
965 lip->m_cpp_text_end = lip->get_cpp_ptr() - post_skip;
966
967 if (!found_escape) {
968 lip->yytoklen = (uint)(end - str);
969 memcpy(start, str, lip->yytoklen);
970 start[lip->yytoklen] = 0;
971 } else {
972 char *to;
973
974 for (to = start; str != end; str++) {
975 int l;
976 if (use_mb(cs) && (l = my_ismbchar(cs, str, end))) {
977 while (l--) *to++ = *str++;
978 str--;
979 continue;
980 }
981 if (!(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
982 *str == '\\' && str + 1 != end) {
983 switch (*++str) {
984 case 'n':
985 *to++ = '\n';
986 break;
987 case 't':
988 *to++ = '\t';
989 break;
990 case 'r':
991 *to++ = '\r';
992 break;
993 case 'b':
994 *to++ = '\b';
995 break;
996 case '0':
997 *to++ = 0; // Ascii null
998 break;
999 case 'Z': // ^Z must be escaped on Win32
1000 *to++ = '\032';
1001 break;
1002 case '_':
1003 case '%':
1004 *to++ = '\\'; // remember prefix for wildcard
1005 /* Fall through */
1006 default:
1007 *to++ = *str;
1008 break;
1009 }
1010 } else if (*str == sep)
1011 *to++ = *str++; // Two ' or "
1012 else
1013 *to++ = *str;
1014 }
1015 *to = 0;
1016 lip->yytoklen = (uint)(to - start);
1017 }
1018 return start;
1019 }
1020 }
1021 return nullptr; // unexpected end of query
1022 }
1023
get_lineno(const char * raw_ptr) const1024 uint Lex_input_stream::get_lineno(const char *raw_ptr) const {
1025 DBUG_ASSERT(m_buf <= raw_ptr && raw_ptr <= m_end_of_query);
1026 if (!(m_buf <= raw_ptr && raw_ptr <= m_end_of_query)) return 1;
1027
1028 uint ret = 1;
1029 const CHARSET_INFO *cs = m_thd->charset();
1030 for (const char *c = m_buf; c < raw_ptr; c++) {
1031 uint mb_char_len;
1032 if (use_mb(cs) && (mb_char_len = my_ismbchar(cs, c, m_end_of_query))) {
1033 c += mb_char_len - 1; // skip the rest of the multibyte character
1034 continue; // we don't expect '\n' there
1035 }
1036 if (*c == '\n') ret++;
1037 }
1038 return ret;
1039 }
1040
Partition_expr_parser_state()1041 Partition_expr_parser_state::Partition_expr_parser_state()
1042 : Parser_state(GRAMMAR_SELECTOR_PART), result(nullptr) {}
1043
Gcol_expr_parser_state()1044 Gcol_expr_parser_state::Gcol_expr_parser_state()
1045 : Parser_state(GRAMMAR_SELECTOR_GCOL), result(nullptr) {}
1046
Expression_parser_state()1047 Expression_parser_state::Expression_parser_state()
1048 : Parser_state(GRAMMAR_SELECTOR_EXPR), result(nullptr) {}
1049
Common_table_expr_parser_state()1050 Common_table_expr_parser_state::Common_table_expr_parser_state()
1051 : Parser_state(GRAMMAR_SELECTOR_CTE), result(nullptr) {}
1052
1053 /*
1054 ** Calc type of integer; long integer, longlong integer or real.
1055 ** Returns smallest type that match the string.
1056 ** When using unsigned long long values the result is converted to a real
1057 ** because else they will be unexpected sign changes because all calculation
1058 ** is done with longlong or double.
1059 */
1060
1061 static const char *long_str = "2147483647";
1062 static const uint long_len = 10;
1063 static const char *signed_long_str = "-2147483648";
1064 static const char *longlong_str = "9223372036854775807";
1065 static const uint longlong_len = 19;
1066 static const char *signed_longlong_str = "-9223372036854775808";
1067 static const uint signed_longlong_len = 19;
1068 static const char *unsigned_longlong_str = "18446744073709551615";
1069 static const uint unsigned_longlong_len = 20;
1070
int_token(const char * str,uint length)1071 static inline uint int_token(const char *str, uint length) {
1072 if (length < long_len) // quick normal case
1073 return NUM;
1074 bool neg = false;
1075
1076 if (*str == '+') // Remove sign and pre-zeros
1077 {
1078 str++;
1079 length--;
1080 } else if (*str == '-') {
1081 str++;
1082 length--;
1083 neg = true;
1084 }
1085 while (*str == '0' && length) {
1086 str++;
1087 length--;
1088 }
1089 if (length < long_len) return NUM;
1090
1091 uint smaller, bigger;
1092 const char *cmp;
1093 if (neg) {
1094 if (length == long_len) {
1095 cmp = signed_long_str + 1;
1096 smaller = NUM; // If <= signed_long_str
1097 bigger = LONG_NUM; // If >= signed_long_str
1098 } else if (length < signed_longlong_len)
1099 return LONG_NUM;
1100 else if (length > signed_longlong_len)
1101 return DECIMAL_NUM;
1102 else {
1103 cmp = signed_longlong_str + 1;
1104 smaller = LONG_NUM; // If <= signed_longlong_str
1105 bigger = DECIMAL_NUM;
1106 }
1107 } else {
1108 if (length == long_len) {
1109 cmp = long_str;
1110 smaller = NUM;
1111 bigger = LONG_NUM;
1112 } else if (length < longlong_len)
1113 return LONG_NUM;
1114 else if (length > longlong_len) {
1115 if (length > unsigned_longlong_len) return DECIMAL_NUM;
1116 cmp = unsigned_longlong_str;
1117 smaller = ULONGLONG_NUM;
1118 bigger = DECIMAL_NUM;
1119 } else {
1120 cmp = longlong_str;
1121 smaller = LONG_NUM;
1122 bigger = ULONGLONG_NUM;
1123 }
1124 }
1125 while (*cmp && *cmp++ == *str++)
1126 ;
1127 return ((uchar)str[-1] <= (uchar)cmp[-1]) ? smaller : bigger;
1128 }
1129
1130 /**
1131 Given a stream that is advanced to the first contained character in
1132 an open comment, consume the comment. Optionally, if we are allowed,
1133 recurse so that we understand comments within this current comment.
1134
1135 At this level, we do not support version-condition comments. We might
1136 have been called with having just passed one in the stream, though. In
1137 that case, we probably want to tolerate mundane comments inside. Thus,
1138 the case for recursion.
1139
1140 @retval Whether EOF reached before comment is closed.
1141 */
consume_comment(Lex_input_stream * lip,int remaining_recursions_permitted)1142 static bool consume_comment(Lex_input_stream *lip,
1143 int remaining_recursions_permitted) {
1144 // only one level of nested comments are allowed
1145 DBUG_ASSERT(remaining_recursions_permitted == 0 ||
1146 remaining_recursions_permitted == 1);
1147 uchar c;
1148 while (!lip->eof()) {
1149 c = lip->yyGet();
1150
1151 if (remaining_recursions_permitted == 1) {
1152 if ((c == '/') && (lip->yyPeek() == '*')) {
1153 push_warning(
1154 lip->m_thd, Sql_condition::SL_WARNING,
1155 ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT,
1156 ER_THD(lip->m_thd, ER_WARN_DEPRECATED_NESTED_COMMENT_SYNTAX));
1157 lip->yyUnput('('); // Replace nested "/*..." with "(*..."
1158 lip->yySkip(); // and skip "("
1159 lip->yySkip(); /* Eat asterisk */
1160 if (consume_comment(lip, 0)) return true;
1161 lip->yyUnput(')'); // Replace "...*/" with "...*)"
1162 lip->yySkip(); // and skip ")"
1163 continue;
1164 }
1165 }
1166
1167 if (c == '*') {
1168 if (lip->yyPeek() == '/') {
1169 lip->yySkip(); /* Eat slash */
1170 return false;
1171 }
1172 }
1173
1174 if (c == '\n') lip->yylineno++;
1175 }
1176
1177 return true;
1178 }
1179
1180 /**
1181 yylex() function implementation for the main parser
1182
1183 @param [out] yacc_yylval semantic value of the token being parsed (yylval)
1184 @param [out] yylloc "location" of the token being parsed (yylloc)
1185 @param thd THD
1186
1187 @return token number
1188
1189 @note
1190 MYSQLlex remember the following states from the following MYSQLlex():
1191
1192 - MY_LEX_END Found end of query
1193 */
1194
MYSQLlex(YYSTYPE * yacc_yylval,YYLTYPE * yylloc,THD * thd)1195 int MYSQLlex(YYSTYPE *yacc_yylval, YYLTYPE *yylloc, THD *thd) {
1196 auto *yylval = reinterpret_cast<Lexer_yystype *>(yacc_yylval);
1197 Lex_input_stream *lip = &thd->m_parser_state->m_lip;
1198 int token;
1199
1200 if (thd->is_error()) {
1201 if (thd->get_parser_da()->has_sql_condition(ER_CAPACITY_EXCEEDED))
1202 return ABORT_SYM;
1203 }
1204
1205 if (lip->lookahead_token >= 0) {
1206 /*
1207 The next token was already parsed in advance,
1208 return it.
1209 */
1210 token = lip->lookahead_token;
1211 lip->lookahead_token = -1;
1212 *yylval = *(lip->lookahead_yylval);
1213 yylloc->cpp.start = lip->get_cpp_tok_start();
1214 yylloc->cpp.end = lip->get_cpp_ptr();
1215 yylloc->raw.start = lip->get_tok_start();
1216 yylloc->raw.end = lip->get_ptr();
1217 lip->lookahead_yylval = nullptr;
1218 lip->add_digest_token(token, yylval);
1219 return token;
1220 }
1221
1222 token = lex_one_token(yylval, thd);
1223 yylloc->cpp.start = lip->get_cpp_tok_start();
1224 yylloc->raw.start = lip->get_tok_start();
1225
1226 switch (token) {
1227 case WITH:
1228 /*
1229 Parsing 'WITH' 'ROLLUP' requires 2 look ups,
1230 which makes the grammar LALR(2).
1231 Replace by a single 'WITH_ROLLUP' token,
1232 to transform the grammar into a LALR(1) grammar,
1233 which sql_yacc.yy can process.
1234 */
1235 token = lex_one_token(yylval, thd);
1236 switch (token) {
1237 case ROLLUP_SYM:
1238 yylloc->cpp.end = lip->get_cpp_ptr();
1239 yylloc->raw.end = lip->get_ptr();
1240 lip->add_digest_token(WITH_ROLLUP_SYM, yylval);
1241 return WITH_ROLLUP_SYM;
1242 default:
1243 /*
1244 Save the token following 'WITH'
1245 */
1246 lip->lookahead_yylval = lip->yylval;
1247 lip->yylval = nullptr;
1248 lip->lookahead_token = token;
1249 yylloc->cpp.end = lip->get_cpp_ptr();
1250 yylloc->raw.end = lip->get_ptr();
1251 lip->add_digest_token(WITH, yylval);
1252 return WITH;
1253 }
1254 break;
1255 }
1256
1257 yylloc->cpp.end = lip->get_cpp_ptr();
1258 yylloc->raw.end = lip->get_ptr();
1259 if (!lip->skip_digest) lip->add_digest_token(token, yylval);
1260 lip->skip_digest = false;
1261 return token;
1262 }
1263
lex_one_token(Lexer_yystype * yylval,THD * thd)1264 static int lex_one_token(Lexer_yystype *yylval, THD *thd) {
1265 uchar c = 0;
1266 bool comment_closed;
1267 int tokval, result_state;
1268 uint length;
1269 enum my_lex_states state;
1270 Lex_input_stream *lip = &thd->m_parser_state->m_lip;
1271 const CHARSET_INFO *cs = thd->charset();
1272 const my_lex_states *state_map = cs->state_maps->main_map;
1273 const uchar *ident_map = cs->ident_map;
1274
1275 lip->yylval = yylval; // The global state
1276
1277 lip->start_token();
1278 state = lip->next_state;
1279 lip->next_state = MY_LEX_START;
1280 for (;;) {
1281 switch (state) {
1282 case MY_LEX_START: // Start of token
1283 // Skip starting whitespace
1284 while (state_map[c = lip->yyPeek()] == MY_LEX_SKIP) {
1285 if (c == '\n') lip->yylineno++;
1286
1287 lip->yySkip();
1288 }
1289
1290 /* Start of real token */
1291 lip->restart_token();
1292 c = lip->yyGet();
1293 state = state_map[c];
1294 break;
1295 case MY_LEX_CHAR: // Unknown or single char token
1296 case MY_LEX_SKIP: // This should not happen
1297 if (c == '-' && lip->yyPeek() == '-' &&
1298 (my_isspace(cs, lip->yyPeekn(1)) ||
1299 my_iscntrl(cs, lip->yyPeekn(1)))) {
1300 state = MY_LEX_COMMENT;
1301 break;
1302 }
1303
1304 if (c == '-' && lip->yyPeek() == '>') // '->'
1305 {
1306 lip->yySkip();
1307 lip->next_state = MY_LEX_START;
1308 if (lip->yyPeek() == '>') {
1309 lip->yySkip();
1310 return JSON_UNQUOTED_SEPARATOR_SYM;
1311 }
1312 return JSON_SEPARATOR_SYM;
1313 }
1314
1315 if (c != ')') lip->next_state = MY_LEX_START; // Allow signed numbers
1316
1317 /*
1318 Check for a placeholder: it should not precede a possible identifier
1319 because of binlogging: when a placeholder is replaced with its value
1320 in a query for the binlog, the query must stay grammatically correct.
1321 */
1322 if (c == '?' && lip->stmt_prepare_mode && !ident_map[lip->yyPeek()])
1323 return (PARAM_MARKER);
1324
1325 return ((int)c);
1326
1327 case MY_LEX_IDENT_OR_NCHAR:
1328 if (lip->yyPeek() != '\'') {
1329 state = MY_LEX_IDENT;
1330 break;
1331 }
1332 /* Found N'string' */
1333 lip->yySkip(); // Skip '
1334 if (!(yylval->lex_str.str = get_text(lip, 2, 1))) {
1335 state = MY_LEX_CHAR; // Read char by char
1336 break;
1337 }
1338 yylval->lex_str.length = lip->yytoklen;
1339 return (NCHAR_STRING);
1340
1341 case MY_LEX_IDENT_OR_HEX:
1342 if (lip->yyPeek() == '\'') { // Found x'hex-number'
1343 state = MY_LEX_HEX_NUMBER;
1344 break;
1345 }
1346 // Fall through.
1347 case MY_LEX_IDENT_OR_BIN:
1348 if (lip->yyPeek() == '\'') { // Found b'bin-number'
1349 state = MY_LEX_BIN_NUMBER;
1350 break;
1351 }
1352 // Fall through.
1353 case MY_LEX_IDENT:
1354 const char *start;
1355 if (use_mb(cs)) {
1356 result_state = IDENT_QUOTED;
1357 switch (my_mbcharlen(cs, lip->yyGetLast())) {
1358 case 1:
1359 break;
1360 case 0:
1361 if (my_mbmaxlenlen(cs) < 2) break;
1362 /* else fall through */
1363 default:
1364 int l =
1365 my_ismbchar(cs, lip->get_ptr() - 1, lip->get_end_of_query());
1366 if (l == 0) {
1367 state = MY_LEX_CHAR;
1368 continue;
1369 }
1370 lip->skip_binary(l - 1);
1371 }
1372 while (ident_map[c = lip->yyGet()]) {
1373 switch (my_mbcharlen(cs, c)) {
1374 case 1:
1375 break;
1376 case 0:
1377 if (my_mbmaxlenlen(cs) < 2) break;
1378 /* else fall through */
1379 default:
1380 int l;
1381 if ((l = my_ismbchar(cs, lip->get_ptr() - 1,
1382 lip->get_end_of_query())) == 0)
1383 break;
1384 lip->skip_binary(l - 1);
1385 }
1386 }
1387 } else {
1388 for (result_state = c; ident_map[c = lip->yyGet()]; result_state |= c)
1389 ;
1390 /* If there were non-ASCII characters, mark that we must convert */
1391 result_state = result_state & 0x80 ? IDENT_QUOTED : IDENT;
1392 }
1393 length = lip->yyLength();
1394 start = lip->get_ptr();
1395 if (lip->ignore_space) {
1396 /*
1397 If we find a space then this can't be an identifier. We notice this
1398 below by checking start != lex->ptr.
1399 */
1400 for (; state_map[c] == MY_LEX_SKIP; c = lip->yyGet()) {
1401 if (c == '\n') lip->yylineno++;
1402 }
1403 }
1404 if (start == lip->get_ptr() && c == '.' && ident_map[lip->yyPeek()])
1405 lip->next_state = MY_LEX_IDENT_SEP;
1406 else { // '(' must follow directly if function
1407 lip->yyUnget();
1408 if ((tokval = find_keyword(lip, length, c == '('))) {
1409 lip->next_state = MY_LEX_START; // Allow signed numbers
1410 return (tokval); // Was keyword
1411 }
1412 lip->yySkip(); // next state does a unget
1413 }
1414 yylval->lex_str = get_token(lip, 0, length);
1415
1416 /*
1417 Note: "SELECT _bla AS 'alias'"
1418 _bla should be considered as a IDENT if charset haven't been found.
1419 So we don't use MYF(MY_WME) with get_charset_by_csname to avoid
1420 producing an error.
1421 */
1422
1423 if (yylval->lex_str.str[0] == '_') {
1424 auto charset_name = yylval->lex_str.str + 1;
1425 const CHARSET_INFO *underscore_cs =
1426 get_charset_by_csname(charset_name, MY_CS_PRIMARY, MYF(0));
1427 if (underscore_cs) {
1428 lip->warn_on_deprecated_charset(underscore_cs, charset_name);
1429 if (underscore_cs == &my_charset_utf8mb4_0900_ai_ci) {
1430 /*
1431 If underscore_cs is utf8mb4, and the collation of underscore_cs
1432 is the default collation of utf8mb4, then update underscore_cs
1433 with a value of the default_collation_for_utf8mb4 system
1434 variable:
1435 */
1436 underscore_cs = thd->variables.default_collation_for_utf8mb4;
1437 }
1438 yylval->charset = underscore_cs;
1439 lip->m_underscore_cs = underscore_cs;
1440
1441 lip->body_utf8_append(lip->m_cpp_text_start,
1442 lip->get_cpp_tok_start() + length);
1443 return (UNDERSCORE_CHARSET);
1444 }
1445 }
1446
1447 lip->body_utf8_append(lip->m_cpp_text_start);
1448
1449 lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
1450 lip->m_cpp_text_end);
1451
1452 return (result_state); // IDENT or IDENT_QUOTED
1453
1454 case MY_LEX_IDENT_SEP: // Found ident and now '.'
1455 yylval->lex_str.str = const_cast<char *>(lip->get_ptr());
1456 yylval->lex_str.length = 1;
1457 c = lip->yyGet(); // should be '.'
1458 lip->next_state =
1459 MY_LEX_IDENT_START; // Next is an ident (not a keyword)
1460 if (!ident_map[lip->yyPeek()]) // Probably ` or "
1461 lip->next_state = MY_LEX_START;
1462 return ((int)c);
1463
1464 case MY_LEX_NUMBER_IDENT: // number or ident which num-start
1465 if (lip->yyGetLast() == '0') {
1466 c = lip->yyGet();
1467 if (c == 'x') {
1468 while (my_isxdigit(cs, (c = lip->yyGet())))
1469 ;
1470 if ((lip->yyLength() >= 3) && !ident_map[c]) {
1471 /* skip '0x' */
1472 yylval->lex_str = get_token(lip, 2, lip->yyLength() - 2);
1473 return (HEX_NUM);
1474 }
1475 lip->yyUnget();
1476 state = MY_LEX_IDENT_START;
1477 break;
1478 } else if (c == 'b') {
1479 while ((c = lip->yyGet()) == '0' || c == '1')
1480 ;
1481 if ((lip->yyLength() >= 3) && !ident_map[c]) {
1482 /* Skip '0b' */
1483 yylval->lex_str = get_token(lip, 2, lip->yyLength() - 2);
1484 return (BIN_NUM);
1485 }
1486 lip->yyUnget();
1487 state = MY_LEX_IDENT_START;
1488 break;
1489 }
1490 lip->yyUnget();
1491 }
1492
1493 while (my_isdigit(cs, (c = lip->yyGet())))
1494 ;
1495 if (!ident_map[c]) { // Can't be identifier
1496 state = MY_LEX_INT_OR_REAL;
1497 break;
1498 }
1499 if (c == 'e' || c == 'E') {
1500 // The following test is written this way to allow numbers of type 1e1
1501 if (my_isdigit(cs, lip->yyPeek()) || (c = (lip->yyGet())) == '+' ||
1502 c == '-') { // Allow 1E+10
1503 if (my_isdigit(cs,
1504 lip->yyPeek())) // Number must have digit after sign
1505 {
1506 lip->yySkip();
1507 while (my_isdigit(cs, lip->yyGet()))
1508 ;
1509 yylval->lex_str = get_token(lip, 0, lip->yyLength());
1510 return (FLOAT_NUM);
1511 }
1512 }
1513 lip->yyUnget();
1514 }
1515 // fall through
1516 case MY_LEX_IDENT_START: // We come here after '.'
1517 result_state = IDENT;
1518 if (use_mb(cs)) {
1519 result_state = IDENT_QUOTED;
1520 while (ident_map[c = lip->yyGet()]) {
1521 switch (my_mbcharlen(cs, c)) {
1522 case 1:
1523 break;
1524 case 0:
1525 if (my_mbmaxlenlen(cs) < 2) break;
1526 /* else fall through */
1527 default:
1528 int l;
1529 if ((l = my_ismbchar(cs, lip->get_ptr() - 1,
1530 lip->get_end_of_query())) == 0)
1531 break;
1532 lip->skip_binary(l - 1);
1533 }
1534 }
1535 } else {
1536 for (result_state = 0; ident_map[c = lip->yyGet()]; result_state |= c)
1537 ;
1538 /* If there were non-ASCII characters, mark that we must convert */
1539 result_state = result_state & 0x80 ? IDENT_QUOTED : IDENT;
1540 }
1541 if (c == '.' && ident_map[lip->yyPeek()])
1542 lip->next_state = MY_LEX_IDENT_SEP; // Next is '.'
1543
1544 yylval->lex_str = get_token(lip, 0, lip->yyLength());
1545
1546 lip->body_utf8_append(lip->m_cpp_text_start);
1547
1548 lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
1549 lip->m_cpp_text_end);
1550
1551 return (result_state);
1552
1553 case MY_LEX_USER_VARIABLE_DELIMITER: // Found quote char
1554 {
1555 uint double_quotes = 0;
1556 char quote_char = c; // Used char
1557 for (;;) {
1558 c = lip->yyGet();
1559 if (c == 0) {
1560 lip->yyUnget();
1561 return ABORT_SYM; // Unmatched quotes
1562 }
1563
1564 int var_length;
1565 if ((var_length = my_mbcharlen(cs, c)) == 1) {
1566 if (c == quote_char) {
1567 if (lip->yyPeek() != quote_char) break;
1568 c = lip->yyGet();
1569 double_quotes++;
1570 continue;
1571 }
1572 } else if (use_mb(cs)) {
1573 if ((var_length = my_ismbchar(cs, lip->get_ptr() - 1,
1574 lip->get_end_of_query())))
1575 lip->skip_binary(var_length - 1);
1576 }
1577 }
1578 if (double_quotes)
1579 yylval->lex_str = get_quoted_token(
1580 lip, 1, lip->yyLength() - double_quotes - 1, quote_char);
1581 else
1582 yylval->lex_str = get_token(lip, 1, lip->yyLength() - 1);
1583 if (c == quote_char) lip->yySkip(); // Skip end `
1584 lip->next_state = MY_LEX_START;
1585
1586 lip->body_utf8_append(lip->m_cpp_text_start);
1587
1588 lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
1589 lip->m_cpp_text_end);
1590
1591 return (IDENT_QUOTED);
1592 }
1593 case MY_LEX_INT_OR_REAL: // Complete int or incomplete real
1594 if (c != '.') { // Found complete integer number.
1595 yylval->lex_str = get_token(lip, 0, lip->yyLength());
1596 return int_token(yylval->lex_str.str, (uint)yylval->lex_str.length);
1597 }
1598 // fall through
1599 case MY_LEX_REAL: // Incomplete real number
1600 while (my_isdigit(cs, c = lip->yyGet()))
1601 ;
1602
1603 if (c == 'e' || c == 'E') {
1604 c = lip->yyGet();
1605 if (c == '-' || c == '+') c = lip->yyGet(); // Skip sign
1606 if (!my_isdigit(cs, c)) { // No digit after sign
1607 state = MY_LEX_CHAR;
1608 break;
1609 }
1610 while (my_isdigit(cs, lip->yyGet()))
1611 ;
1612 yylval->lex_str = get_token(lip, 0, lip->yyLength());
1613 return (FLOAT_NUM);
1614 }
1615 yylval->lex_str = get_token(lip, 0, lip->yyLength());
1616 return (DECIMAL_NUM);
1617
1618 case MY_LEX_HEX_NUMBER: // Found x'hexstring'
1619 lip->yySkip(); // Accept opening '
1620 while (my_isxdigit(cs, (c = lip->yyGet())))
1621 ;
1622 if (c != '\'') return (ABORT_SYM); // Illegal hex constant
1623 lip->yySkip(); // Accept closing '
1624 length = lip->yyLength(); // Length of hexnum+3
1625 if ((length % 2) == 0) return (ABORT_SYM); // odd number of hex digits
1626 yylval->lex_str = get_token(lip,
1627 2, // skip x'
1628 length - 3); // don't count x' and last '
1629 return (HEX_NUM);
1630
1631 case MY_LEX_BIN_NUMBER: // Found b'bin-string'
1632 lip->yySkip(); // Accept opening '
1633 while ((c = lip->yyGet()) == '0' || c == '1')
1634 ;
1635 if (c != '\'') return (ABORT_SYM); // Illegal hex constant
1636 lip->yySkip(); // Accept closing '
1637 length = lip->yyLength(); // Length of bin-num + 3
1638 yylval->lex_str = get_token(lip,
1639 2, // skip b'
1640 length - 3); // don't count b' and last '
1641 return (BIN_NUM);
1642
1643 case MY_LEX_CMP_OP: // Incomplete comparison operator
1644 if (state_map[lip->yyPeek()] == MY_LEX_CMP_OP ||
1645 state_map[lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
1646 lip->yySkip();
1647 if ((tokval = find_keyword(lip, lip->yyLength() + 1, false))) {
1648 lip->next_state = MY_LEX_START; // Allow signed numbers
1649 return (tokval);
1650 }
1651 state = MY_LEX_CHAR; // Something fishy found
1652 break;
1653
1654 case MY_LEX_LONG_CMP_OP: // Incomplete comparison operator
1655 if (state_map[lip->yyPeek()] == MY_LEX_CMP_OP ||
1656 state_map[lip->yyPeek()] == MY_LEX_LONG_CMP_OP) {
1657 lip->yySkip();
1658 if (state_map[lip->yyPeek()] == MY_LEX_CMP_OP) lip->yySkip();
1659 }
1660 if ((tokval = find_keyword(lip, lip->yyLength() + 1, false))) {
1661 lip->next_state = MY_LEX_START; // Found long op
1662 return (tokval);
1663 }
1664 state = MY_LEX_CHAR; // Something fishy found
1665 break;
1666
1667 case MY_LEX_BOOL:
1668 if (c != lip->yyPeek()) {
1669 state = MY_LEX_CHAR;
1670 break;
1671 }
1672 lip->yySkip();
1673 tokval = find_keyword(lip, 2, false); // Is a bool operator
1674 lip->next_state = MY_LEX_START; // Allow signed numbers
1675 return (tokval);
1676
1677 case MY_LEX_STRING_OR_DELIMITER:
1678 if (thd->variables.sql_mode & MODE_ANSI_QUOTES) {
1679 state = MY_LEX_USER_VARIABLE_DELIMITER;
1680 break;
1681 }
1682 /* " used for strings */
1683 // Fall through.
1684 case MY_LEX_STRING: // Incomplete text string
1685 if (!(yylval->lex_str.str = get_text(lip, 1, 1))) {
1686 state = MY_LEX_CHAR; // Read char by char
1687 break;
1688 }
1689 yylval->lex_str.length = lip->yytoklen;
1690
1691 lip->body_utf8_append(lip->m_cpp_text_start);
1692
1693 lip->body_utf8_append_literal(
1694 thd, &yylval->lex_str,
1695 lip->m_underscore_cs ? lip->m_underscore_cs : cs,
1696 lip->m_cpp_text_end);
1697
1698 lip->m_underscore_cs = nullptr;
1699
1700 return (TEXT_STRING);
1701
1702 case MY_LEX_COMMENT: // Comment
1703 thd->m_parser_state->add_comment();
1704 while ((c = lip->yyGet()) != '\n' && c)
1705 ;
1706 lip->yyUnget(); // Safety against eof
1707 state = MY_LEX_START; // Try again
1708 break;
1709 case MY_LEX_LONG_COMMENT: /* Long C comment? */
1710 if (lip->yyPeek() != '*') {
1711 state = MY_LEX_CHAR; // Probable division
1712 break;
1713 }
1714 thd->m_parser_state->add_comment();
1715 /* Reject '/' '*', since we might need to turn off the echo */
1716 lip->yyUnget();
1717
1718 lip->save_in_comment_state();
1719
1720 if (lip->yyPeekn(2) == '!') {
1721 lip->in_comment = DISCARD_COMMENT;
1722 /* Accept '/' '*' '!', but do not keep this marker. */
1723 lip->set_echo(false);
1724 lip->yySkip();
1725 lip->yySkip();
1726 lip->yySkip();
1727
1728 /*
1729 The special comment format is very strict:
1730 '/' '*' '!', followed by exactly
1731 1 digit (major), 2 digits (minor), then 2 digits (dot).
1732 32302 -> 3.23.02
1733 50032 -> 5.0.32
1734 50114 -> 5.1.14
1735 */
1736 char version_str[6];
1737 if (my_isdigit(cs, (version_str[0] = lip->yyPeekn(0))) &&
1738 my_isdigit(cs, (version_str[1] = lip->yyPeekn(1))) &&
1739 my_isdigit(cs, (version_str[2] = lip->yyPeekn(2))) &&
1740 my_isdigit(cs, (version_str[3] = lip->yyPeekn(3))) &&
1741 my_isdigit(cs, (version_str[4] = lip->yyPeekn(4)))) {
1742 version_str[5] = 0;
1743 ulong version;
1744 version = strtol(version_str, nullptr, 10);
1745
1746 if (version <= MYSQL_VERSION_ID) {
1747 /* Accept 'M' 'm' 'm' 'd' 'd' */
1748 lip->yySkipn(5);
1749 /* Expand the content of the special comment as real code */
1750 lip->set_echo(true);
1751 state = MY_LEX_START;
1752 break; /* Do not treat contents as a comment. */
1753 } else {
1754 /*
1755 Patch and skip the conditional comment to avoid it
1756 being propagated infinitely (eg. to a slave).
1757 */
1758 char *pcom = lip->yyUnput(' ');
1759 comment_closed = !consume_comment(lip, 1);
1760 if (!comment_closed) {
1761 *pcom = '!';
1762 }
1763 /* version allowed to have one level of comment inside. */
1764 }
1765 } else {
1766 /* Not a version comment. */
1767 state = MY_LEX_START;
1768 lip->set_echo(true);
1769 break;
1770 }
1771 } else {
1772 if (lip->in_comment != NO_COMMENT) {
1773 push_warning(
1774 lip->m_thd, Sql_condition::SL_WARNING,
1775 ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT,
1776 ER_THD(lip->m_thd, ER_WARN_DEPRECATED_NESTED_COMMENT_SYNTAX));
1777 }
1778 lip->in_comment = PRESERVE_COMMENT;
1779 lip->yySkip(); // Accept /
1780 lip->yySkip(); // Accept *
1781 comment_closed = !consume_comment(lip, 0);
1782 /* regular comments can have zero comments inside. */
1783 }
1784 /*
1785 Discard:
1786 - regular '/' '*' comments,
1787 - special comments '/' '*' '!' for a future version,
1788 by scanning until we find a closing '*' '/' marker.
1789
1790 Nesting regular comments isn't allowed. The first
1791 '*' '/' returns the parser to the previous state.
1792
1793 /#!VERSI oned containing /# regular #/ is allowed #/
1794
1795 Inside one versioned comment, another versioned comment
1796 is treated as a regular discardable comment. It gets
1797 no special parsing.
1798 */
1799
1800 /* Unbalanced comments with a missing '*' '/' are a syntax error */
1801 if (!comment_closed) return (ABORT_SYM);
1802 state = MY_LEX_START; // Try again
1803 lip->restore_in_comment_state();
1804 break;
1805 case MY_LEX_END_LONG_COMMENT:
1806 if ((lip->in_comment != NO_COMMENT) && lip->yyPeek() == '/') {
1807 /* Reject '*' '/' */
1808 lip->yyUnget();
1809 /* Accept '*' '/', with the proper echo */
1810 lip->set_echo(lip->in_comment == PRESERVE_COMMENT);
1811 lip->yySkipn(2);
1812 /* And start recording the tokens again */
1813 lip->set_echo(true);
1814
1815 /*
1816 C-style comments are replaced with a single space (as it
1817 is in C and C++). If there is already a whitespace
1818 character at this point in the stream, the space is
1819 not inserted.
1820
1821 See also ISO/IEC 9899:1999 §5.1.1.2
1822 ("Programming languages — C")
1823 */
1824 if (!my_isspace(cs, lip->yyPeek()) &&
1825 lip->get_cpp_ptr() != lip->get_cpp_buf() &&
1826 !my_isspace(cs, *(lip->get_cpp_ptr() - 1)))
1827 lip->cpp_inject(' ');
1828
1829 lip->in_comment = NO_COMMENT;
1830 state = MY_LEX_START;
1831 } else
1832 state = MY_LEX_CHAR; // Return '*'
1833 break;
1834 case MY_LEX_SET_VAR: // Check if ':='
1835 if (lip->yyPeek() != '=') {
1836 state = MY_LEX_CHAR; // Return ':'
1837 break;
1838 }
1839 lip->yySkip();
1840 return (SET_VAR);
1841 case MY_LEX_SEMICOLON: // optional line terminator
1842 state = MY_LEX_CHAR; // Return ';'
1843 break;
1844 case MY_LEX_EOL:
1845 if (lip->eof()) {
1846 lip->yyUnget(); // Reject the last '\0'
1847 lip->set_echo(false);
1848 lip->yySkip();
1849 lip->set_echo(true);
1850 /* Unbalanced comments with a missing '*' '/' are a syntax error */
1851 if (lip->in_comment != NO_COMMENT) return (ABORT_SYM);
1852 lip->next_state = MY_LEX_END; // Mark for next loop
1853 return (END_OF_INPUT);
1854 }
1855 state = MY_LEX_CHAR;
1856 break;
1857 case MY_LEX_END:
1858 lip->next_state = MY_LEX_END;
1859 return (0); // We found end of input last time
1860
1861 /* Actually real shouldn't start with . but allow them anyhow */
1862 case MY_LEX_REAL_OR_POINT:
1863 if (my_isdigit(cs, lip->yyPeek()))
1864 state = MY_LEX_REAL; // Real
1865 else {
1866 state = MY_LEX_IDENT_SEP; // return '.'
1867 lip->yyUnget(); // Put back '.'
1868 }
1869 break;
1870 case MY_LEX_USER_END: // end '@' of user@hostname
1871 switch (state_map[lip->yyPeek()]) {
1872 case MY_LEX_STRING:
1873 case MY_LEX_USER_VARIABLE_DELIMITER:
1874 case MY_LEX_STRING_OR_DELIMITER:
1875 break;
1876 case MY_LEX_USER_END:
1877 lip->next_state = MY_LEX_SYSTEM_VAR;
1878 break;
1879 default:
1880 lip->next_state = MY_LEX_HOSTNAME;
1881 break;
1882 }
1883 yylval->lex_str.str = const_cast<char *>(lip->get_ptr());
1884 yylval->lex_str.length = 1;
1885 return ((int)'@');
1886 case MY_LEX_HOSTNAME: // end '@' of user@hostname
1887 for (c = lip->yyGet();
1888 my_isalnum(cs, c) || c == '.' || c == '_' || c == '$';
1889 c = lip->yyGet())
1890 ;
1891 yylval->lex_str = get_token(lip, 0, lip->yyLength());
1892 return (LEX_HOSTNAME);
1893 case MY_LEX_SYSTEM_VAR:
1894 yylval->lex_str.str = const_cast<char *>(lip->get_ptr());
1895 yylval->lex_str.length = 1;
1896 lip->yySkip(); // Skip '@'
1897 lip->next_state =
1898 (state_map[lip->yyPeek()] == MY_LEX_USER_VARIABLE_DELIMITER
1899 ? MY_LEX_START
1900 : MY_LEX_IDENT_OR_KEYWORD);
1901 return ((int)'@');
1902 case MY_LEX_IDENT_OR_KEYWORD:
1903 /*
1904 We come here when we have found two '@' in a row.
1905 We should now be able to handle:
1906 [(global | local | session) .]variable_name
1907 */
1908
1909 for (result_state = 0; ident_map[c = lip->yyGet()]; result_state |= c)
1910 ;
1911 /* If there were non-ASCII characters, mark that we must convert */
1912 result_state = result_state & 0x80 ? IDENT_QUOTED : IDENT;
1913
1914 if (c == '.') lip->next_state = MY_LEX_IDENT_SEP;
1915 length = lip->yyLength();
1916 if (length == 0) return (ABORT_SYM); // Names must be nonempty.
1917 if ((tokval = find_keyword(lip, length, false))) {
1918 lip->yyUnget(); // Put back 'c'
1919 return (tokval); // Was keyword
1920 }
1921 yylval->lex_str = get_token(lip, 0, length);
1922
1923 lip->body_utf8_append(lip->m_cpp_text_start);
1924
1925 lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
1926 lip->m_cpp_text_end);
1927
1928 return (result_state);
1929 }
1930 }
1931 }
1932
trim_whitespace(const CHARSET_INFO * cs,LEX_STRING * str)1933 void trim_whitespace(const CHARSET_INFO *cs, LEX_STRING *str) {
1934 /*
1935 TODO:
1936 This code assumes that there are no multi-bytes characters
1937 that can be considered white-space.
1938 */
1939
1940 while ((str->length > 0) && (my_isspace(cs, str->str[0]))) {
1941 str->length--;
1942 str->str++;
1943 }
1944
1945 /*
1946 FIXME:
1947 Also, parsing backward is not safe with multi bytes characters
1948 */
1949 while ((str->length > 0) && (my_isspace(cs, str->str[str->length - 1]))) {
1950 str->length--;
1951 /* set trailing spaces to 0 as there're places that don't respect length */
1952 str->str[str->length] = 0;
1953 }
1954 }
1955
1956 /**
1957 Prints into 'str' a comma-separated list of column names, enclosed in
1958 parenthesis.
1959 @param thd Thread handler
1960 @param str Where to print
1961 @param column_names List to print, or NULL
1962 */
1963
print_derived_column_names(const THD * thd,String * str,const Create_col_name_list * column_names)1964 void print_derived_column_names(const THD *thd, String *str,
1965 const Create_col_name_list *column_names) {
1966 if (!column_names) return;
1967 str->append(" (");
1968 for (auto s : *column_names) {
1969 append_identifier(thd, str, s.str, s.length);
1970 str->append(',');
1971 }
1972 str->length(str->length() - 1);
1973 str->append(')');
1974 }
1975
1976 /**
1977 Construct and initialize SELECT_LEX_UNIT object.
1978 */
1979
SELECT_LEX_UNIT(enum_parsing_context parsing_context)1980 SELECT_LEX_UNIT::SELECT_LEX_UNIT(enum_parsing_context parsing_context)
1981 : next(nullptr),
1982 prev(nullptr),
1983 master(nullptr),
1984 slave(nullptr),
1985 explain_marker(CTX_NONE),
1986 prepared(false),
1987 optimized(false),
1988 executed(false),
1989 result_table_list(),
1990 union_result(nullptr),
1991 table(nullptr),
1992 m_query_result(nullptr),
1993 uncacheable(0),
1994 cleaned(UC_DIRTY),
1995 item_list(),
1996 types(),
1997 select_limit_cnt(HA_POS_ERROR),
1998 offset_limit_cnt(0),
1999 item(nullptr),
2000 fake_select_lex(nullptr),
2001 saved_fake_select_lex(nullptr),
2002 union_distinct(nullptr),
2003 m_with_clause(nullptr),
2004 derived_table(nullptr),
2005 first_recursive(nullptr),
2006 m_lateral_deps(0) {
2007 switch (parsing_context) {
2008 case CTX_ORDER_BY:
2009 explain_marker = CTX_ORDER_BY_SQ; // A subquery in ORDER BY
2010 break;
2011 case CTX_GROUP_BY:
2012 explain_marker = CTX_GROUP_BY_SQ; // A subquery in GROUP BY
2013 break;
2014 case CTX_ON:
2015 explain_marker = CTX_WHERE;
2016 break;
2017 case CTX_HAVING: // A subquery elsewhere
2018 case CTX_SELECT_LIST:
2019 case CTX_UPDATE_VALUE:
2020 case CTX_INSERT_VALUES:
2021 case CTX_INSERT_UPDATE:
2022 case CTX_WHERE:
2023 case CTX_DERIVED:
2024 case CTX_NONE: // A subquery in a non-select
2025 explain_marker = parsing_context;
2026 break;
2027 default:
2028 /* Subquery can't happen outside of those ^. */
2029 DBUG_ASSERT(false); /* purecov: inspected */
2030 break;
2031 }
2032 }
2033
2034 /**
2035 Construct and initialize SELECT_LEX object.
2036 */
2037
SELECT_LEX(MEM_ROOT * mem_root,Item * where,Item * having)2038 SELECT_LEX::SELECT_LEX(MEM_ROOT *mem_root, Item *where, Item *having)
2039 : ftfunc_list(&ftfunc_list_alloc),
2040 sj_nests(mem_root),
2041 first_context(&context),
2042 top_join_list(mem_root),
2043 join_list(&top_join_list),
2044 m_where_cond(where),
2045 m_having_cond(having) {}
2046
2047 /**
2048 Set the name resolution context for the specified query block.
2049
2050 @param outer_context Outer name resolution context.
2051 NULL if none or it will be set later.
2052 */
2053
set_context(Name_resolution_context * outer_context)2054 bool SELECT_LEX::set_context(Name_resolution_context *outer_context) {
2055 context.init();
2056 context.select_lex = this;
2057 context.outer_context = outer_context;
2058 /*
2059 Add the name resolution context of this query block to the
2060 stack of contexts for the whole query.
2061 */
2062 return parent_lex->push_context(&context);
2063 }
2064
2065 /**
2066 Add tables from an array to a list of used tables.
2067
2068 @param thd Current session.
2069 @param tables Tables to add.
2070 @param table_options A set of the following bits:
2071 - TL_OPTION_UPDATING : Table will be updated,
2072 - TL_OPTION_FORCE_INDEX : Force usage of index,
2073 - TL_OPTION_ALIAS : an alias in multi table DELETE.
2074 @param lock_type How table should be locked.
2075 @param mdl_type Type of metadata lock to acquire on the table.
2076
2077 @returns true if error (reported), otherwise false.
2078 */
2079
add_tables(THD * thd,const Mem_root_array<Table_ident * > * tables,ulong table_options,thr_lock_type lock_type,enum_mdl_type mdl_type)2080 bool SELECT_LEX::add_tables(THD *thd,
2081 const Mem_root_array<Table_ident *> *tables,
2082 ulong table_options, thr_lock_type lock_type,
2083 enum_mdl_type mdl_type) {
2084 if (tables == nullptr) return false;
2085
2086 for (auto *table : *tables) {
2087 if (!add_table_to_list(thd, table, nullptr, table_options, lock_type,
2088 mdl_type))
2089 return true;
2090 }
2091 return false;
2092 }
2093
2094 /**
2095 Exclude this unit and its immediately contained select_lex objects
2096 from query expression / query block chain.
2097
2098 @note
2099 Units that belong to the select_lex objects of the current unit will be
2100 brought up one level and will replace the current unit in the list of units.
2101 */
exclude_level()2102 void SELECT_LEX_UNIT::exclude_level() {
2103 /*
2104 This change to the unit tree is done only during statement resolution
2105 so doesn't need LOCK_query_plan
2106 */
2107 SELECT_LEX_UNIT *units = nullptr;
2108 SELECT_LEX_UNIT **units_last = &units;
2109 SELECT_LEX *sl = first_select();
2110 while (sl) {
2111 // Exclusion can only be done prior to optimization or if the subquery is
2112 // already executed because it might not be using any tables (const item).
2113 DBUG_ASSERT(sl->join == nullptr || is_executed());
2114 if (sl->join != nullptr) sl->join->destroy();
2115
2116 SELECT_LEX *next_select = sl->next_select();
2117
2118 // unlink current level from global SELECTs list
2119 if (sl->link_prev && (*sl->link_prev = sl->link_next))
2120 sl->link_next->link_prev = sl->link_prev;
2121
2122 // bring up underlay levels
2123 SELECT_LEX_UNIT **last = nullptr;
2124 for (SELECT_LEX_UNIT *u = sl->first_inner_unit(); u; u = u->next_unit()) {
2125 /*
2126 We are excluding a SELECT_LEX from the hierarchy of
2127 SELECT_LEX_UNITs and SELECT_LEXes. Since this level is
2128 removed, we must also exclude the Name_resolution_context
2129 belonging to this level. Do this by looping through inner
2130 subqueries and changing their contexts' outer context pointers
2131 to point to the outer select's context.
2132 */
2133 for (SELECT_LEX *s = u->first_select(); s; s = s->next_select()) {
2134 if (s->context.outer_context == &sl->context)
2135 s->context.outer_context = &sl->outer_select()->context;
2136 }
2137 if (u->fake_select_lex &&
2138 u->fake_select_lex->context.outer_context == &sl->context)
2139 u->fake_select_lex->context.outer_context =
2140 &sl->outer_select()->context;
2141 u->master = master;
2142 last = &(u->next);
2143 }
2144 if (last) {
2145 (*units_last) = sl->first_inner_unit();
2146 units_last = last;
2147 }
2148
2149 sl->invalidate();
2150 sl = next_select;
2151 }
2152 if (units) {
2153 // include brought up levels in place of current
2154 (*prev) = units;
2155 (*units_last) = next;
2156 if (next) next->prev = units_last;
2157 units->prev = prev;
2158 } else {
2159 // exclude currect unit from list of nodes
2160 if (prev) (*prev) = next;
2161 if (next) next->prev = prev;
2162 }
2163
2164 invalidate();
2165 }
2166
2167 /**
2168 Exclude subtree of current unit from tree of SELECTs
2169 */
exclude_tree(THD * thd)2170 void SELECT_LEX_UNIT::exclude_tree(THD *thd) {
2171 SELECT_LEX *sl = first_select();
2172 while (sl) {
2173 SELECT_LEX *next_select = sl->next_select();
2174
2175 // unlink current level from global SELECTs list
2176 if (sl->link_prev && (*sl->link_prev = sl->link_next))
2177 sl->link_next->link_prev = sl->link_prev;
2178
2179 // Exclude subtrees of all the inner query expressions of this query block
2180 for (SELECT_LEX_UNIT *u = sl->first_inner_unit(); u; u = u->next_unit()) {
2181 u->exclude_tree(thd);
2182 }
2183
2184 /*
2185 Reference to this query block is lost after it's excluded. Cleanup must
2186 be done at this point to free memory.
2187 */
2188 sl->cleanup(thd, true);
2189 sl->invalidate();
2190 sl = next_select;
2191 }
2192 // exclude currect unit from list of nodes
2193 if (prev) (*prev) = next;
2194 if (next) next->prev = prev;
2195
2196 invalidate();
2197 }
2198
2199 /**
2200 Invalidate by nulling out pointers to other SELECT_LEX_UNITs and
2201 SELECT_LEXes.
2202 */
invalidate()2203 void SELECT_LEX_UNIT::invalidate() {
2204 next = nullptr;
2205 prev = nullptr;
2206 master = nullptr;
2207 slave = nullptr;
2208 }
2209
2210 /**
2211 Make active options from base options, supplied options, any statement
2212 options and the environment.
2213
2214 @param added_options Options that are added to the active options
2215 @param removed_options Options that are removed from the active options
2216 */
2217
make_active_options(ulonglong added_options,ulonglong removed_options)2218 void SELECT_LEX::make_active_options(ulonglong added_options,
2219 ulonglong removed_options) {
2220 m_active_options =
2221 (m_base_options | added_options | parent_lex->statement_options() |
2222 parent_lex->thd->variables.option_bits) &
2223 ~removed_options;
2224 }
2225
2226 /**
2227 Mark all query blocks from this to 'last' as dependent
2228
2229 @param last Pointer to last SELECT_LEX struct, before which all
2230 SELECT_LEX are marked as as dependent.
2231 @param aggregate true if the dependency is due to a set function, such as
2232 COUNT(*), which is aggregated within the query block 'last'.
2233 Such functions must have a dependency on all tables of
2234 the aggregating query block.
2235
2236 @note
2237 last should be reachable from this SELECT_LEX
2238
2239 @todo Update OUTER_REF_TABLE_BIT for intermediate subquery items, by
2240 replacing the below "if (aggregate)" block with:
2241 if (last == s->outer_select())
2242 {
2243 if (aggregate)
2244 munit->item->accumulate_used_tables(last->all_tables_map());
2245 }
2246 else
2247 {
2248 munit->item->accumulate_used_tables(OUTER_REF_TABLE_BIT);
2249 }
2250 and remove settings from Item_field::fix_outer_field(),
2251 Item_ref::fix_fields().
2252 */
2253
mark_as_dependent(SELECT_LEX * last,bool aggregate)2254 void SELECT_LEX::mark_as_dependent(SELECT_LEX *last, bool aggregate) {
2255 // The top level query block cannot be dependent, so do not go above this:
2256 DBUG_ASSERT(last != nullptr);
2257
2258 /*
2259 Mark all selects from resolved to 1 before select where was
2260 found table as depended (of select where was found table)
2261 */
2262 for (SELECT_LEX *s = this; s && s != last; s = s->outer_select()) {
2263 SELECT_LEX_UNIT *munit = s->master_unit();
2264 if (!(s->uncacheable & UNCACHEABLE_DEPENDENT)) {
2265 // Select is dependent of outer select
2266 s->uncacheable =
2267 (s->uncacheable & ~UNCACHEABLE_UNITED) | UNCACHEABLE_DEPENDENT;
2268 munit->uncacheable =
2269 (munit->uncacheable & ~UNCACHEABLE_UNITED) | UNCACHEABLE_DEPENDENT;
2270 for (SELECT_LEX *sl = munit->first_select(); sl; sl = sl->next_select()) {
2271 if (sl != s &&
2272 !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED))) {
2273 // Prevent early freeing in JOIN::join_free()
2274 sl->uncacheable |= UNCACHEABLE_UNITED;
2275 }
2276 }
2277 }
2278 if (aggregate) {
2279 munit->accumulate_used_tables(last == s->outer_select()
2280 ? last->all_tables_map()
2281 : OUTER_REF_TABLE_BIT);
2282 }
2283 }
2284 }
2285
2286 /*
2287 prohibit using LIMIT clause
2288 */
test_limit()2289 bool SELECT_LEX::test_limit() {
2290 if (select_limit != nullptr) {
2291 my_error(ER_NOT_SUPPORTED_YET, MYF(0), "LIMIT & IN/ALL/ANY/SOME subquery");
2292 return (true);
2293 }
2294 return (false);
2295 }
2296
get_explain_marker(const THD * thd) const2297 enum_parsing_context SELECT_LEX_UNIT::get_explain_marker(const THD *thd) const {
2298 thd->query_plan.assert_plan_is_locked_if_other();
2299 return explain_marker;
2300 }
2301
set_explain_marker(THD * thd,enum_parsing_context m)2302 void SELECT_LEX_UNIT::set_explain_marker(THD *thd, enum_parsing_context m) {
2303 thd->lock_query_plan();
2304 explain_marker = m;
2305 thd->unlock_query_plan();
2306 }
2307
set_explain_marker_from(THD * thd,const SELECT_LEX_UNIT * u)2308 void SELECT_LEX_UNIT::set_explain_marker_from(THD *thd,
2309 const SELECT_LEX_UNIT *u) {
2310 thd->lock_query_plan();
2311 explain_marker = u->explain_marker;
2312 thd->unlock_query_plan();
2313 }
2314
get_offset(THD * thd)2315 ha_rows SELECT_LEX::get_offset(THD *thd) {
2316 ulonglong val = 0;
2317
2318 if (offset_limit) {
2319 // see comment for st_select_lex::get_limit()
2320 bool fix_fields_successful = true;
2321 if (!offset_limit->fixed) {
2322 fix_fields_successful = !offset_limit->fix_fields(thd, nullptr);
2323 DBUG_ASSERT(fix_fields_successful);
2324 }
2325 val = fix_fields_successful ? offset_limit->val_uint() : HA_POS_ERROR;
2326 }
2327
2328 return ha_rows(val);
2329 }
2330
get_limit(THD * thd)2331 ha_rows SELECT_LEX::get_limit(THD *thd) {
2332 ulonglong val = HA_POS_ERROR;
2333
2334 if (select_limit) {
2335 /*
2336 fix_fields() has not been called for select_limit. That's due to the
2337 historical reasons -- this item could be only of type Item_int, and
2338 Item_int does not require fix_fields(). Thus, fix_fields() was never
2339 called for select_limit.
2340
2341 Some time ago, Item_splocal was also allowed for LIMIT / OFFSET clauses.
2342 However, the fix_fields() behavior was not updated, which led to a crash
2343 in some cases.
2344
2345 There is no single place where to call fix_fields() for LIMIT / OFFSET
2346 items during the fix-fields-phase. Thus, for the sake of readability,
2347 it was decided to do it here, on the evaluation phase (which is a
2348 violation of design, but we chose the lesser of two evils).
2349
2350 We can call fix_fields() here, because select_limit can be of two
2351 types only: Item_int and Item_splocal. Item_int::fix_fields() is trivial,
2352 and Item_splocal::fix_fields() (or rather Item_sp_variable::fix_fields())
2353 has the following properties:
2354 1) it does not affect other items;
2355 2) it does not fail.
2356 Nevertheless DBUG_ASSERT was added to catch future changes in
2357 fix_fields() implementation. Also added runtime check against a result
2358 of fix_fields() in order to handle error condition in non-debug build.
2359 */
2360 bool fix_fields_successful = true;
2361 if (!select_limit->fixed) {
2362 fix_fields_successful = !select_limit->fix_fields(thd, nullptr);
2363 DBUG_ASSERT(fix_fields_successful);
2364 }
2365 val = fix_fields_successful ? select_limit->val_uint() : HA_POS_ERROR;
2366 }
2367 return ha_rows(val);
2368 }
2369
add_order_to_list(ORDER * order)2370 void SELECT_LEX::add_order_to_list(ORDER *order) {
2371 add_to_list(order_list, order);
2372 }
2373
add_item_to_list(Item * item)2374 bool SELECT_LEX::add_item_to_list(Item *item) {
2375 DBUG_TRACE;
2376 DBUG_PRINT("info", ("Item: %p", item));
2377 return fields_list.push_back(item);
2378 }
2379
add_ftfunc_to_list(Item_func_match * func)2380 bool SELECT_LEX::add_ftfunc_to_list(Item_func_match *func) {
2381 return !func || ftfunc_list->push_back(func); // end of memory?
2382 }
2383
2384 /**
2385 Invalidate by nulling out pointers to other SELECT_LEX_UNITs and
2386 SELECT_LEXes.
2387 */
invalidate()2388 void SELECT_LEX::invalidate() {
2389 next = nullptr;
2390 prev = nullptr;
2391 master = nullptr;
2392 slave = nullptr;
2393 link_next = nullptr;
2394 link_prev = nullptr;
2395 }
2396
setup_base_ref_items(THD * thd)2397 bool SELECT_LEX::setup_base_ref_items(THD *thd) {
2398 uint order_group_num = order_list.elements + group_list.elements;
2399
2400 // find_order_in_list() may need some extra space, so multiply by two.
2401 order_group_num *= 2;
2402
2403 // create_distinct_group() may need some extra space
2404 if (is_distinct()) {
2405 uint bitcount = 0;
2406 Item *item;
2407 List_iterator<Item> li(fields_list);
2408 while ((item = li++)) {
2409 /*
2410 Same test as in create_distinct_group, when it pushes new items to the
2411 end of base_ref_items. An extra test for 'fixed' which, at this
2412 stage, will be true only for columns inserted for a '*' wildcard.
2413 */
2414 if (item->fixed && item->type() == Item::FIELD_ITEM &&
2415 item->data_type() == MYSQL_TYPE_BIT)
2416 ++bitcount;
2417 }
2418 order_group_num += bitcount;
2419 }
2420
2421 /*
2422 We have to create array in prepared statement memory if it is
2423 prepared statement
2424 */
2425 Query_arena *arena = thd->stmt_arena;
2426 uint n_elems = n_sum_items + n_child_sum_items + fields_list.elements +
2427 select_n_having_items + select_n_where_fields +
2428 order_group_num + n_scalar_subqueries;
2429
2430 /*
2431 If it is possible that we transform IN(subquery) to a join to a derived
2432 table, we will be adding DISTINCT (this possibly has the problem of BIT
2433 columns as in the logic above), and we will also be adding one expression to
2434 the SELECT list per decorrelated equality in WHERE. So we have to allocate
2435 more space.
2436
2437 The number of decorrelatable equalities is bounded by
2438 select_n_where_fields. Indeed an equality isn't counted in
2439 select_n_where_fields if it's:
2440 expr-without_Item_field = expr-without_Item_field;
2441 but we decorrelate an equality if one member has OUTER_REF_TABLE_BIT, so
2442 it has an Item_field inside.
2443
2444 Note that cond_count cannot be used, as setup_cond() hasn't run yet. So we
2445 use select_n_where_fields instead.
2446 */
2447 if (master_unit()->item &&
2448 (thd->optimizer_switch_flag(OPTIMIZER_SWITCH_SUBQUERY_TO_DERIVED) ||
2449 (thd->lex->m_sql_cmd != nullptr &&
2450 thd->secondary_engine_optimization() ==
2451 Secondary_engine_optimization::SECONDARY))) {
2452 Item_subselect *subq_predicate = master_unit()->item;
2453 if (subq_predicate->substype() == Item_subselect::EXISTS_SUBS ||
2454 subq_predicate->substype() == Item_subselect::IN_SUBS) {
2455 // might be transformed to derived table, so:
2456 n_elems +=
2457 // possible additions to SELECT list from decorrelation of WHERE
2458 select_n_where_fields +
2459 // add size of new SELECT list, for DISTINCT and BIT type
2460 (select_n_where_fields + fields_list.elements);
2461 }
2462 }
2463
2464 DBUG_PRINT("info",
2465 ("setup_ref_array this %p %4u : %4u %4u %4u %4u %4u %4u %4u", this,
2466 n_elems, // :
2467 n_sum_items, n_child_sum_items, fields_list.elements,
2468 select_n_having_items, select_n_where_fields, order_group_num,
2469 n_scalar_subqueries));
2470 if (!base_ref_items.is_null()) {
2471 /*
2472 We need to take 'n_sum_items' into account when allocating the array,
2473 and this may actually increase during the optimization phase due to
2474 MIN/MAX rewrite in Item_in_subselect::single_value_transformer.
2475 In the usual case we can reuse the array from the prepare phase.
2476 If we need a bigger array, we must allocate a new one.
2477 It looks like this branch is used for a MIN/MAX transformed subquery
2478 when we prepare it for the 2nd time (prepared statement), and could go
2479 away after WL#6570.
2480 */
2481 if (base_ref_items.size() >= n_elems) return false;
2482 }
2483 /*
2484 base_ref_items could become bigger when a subquery gets transformed
2485 into a MIN/MAX subquery. Reallocate array in this case.
2486 */
2487 Item **array = static_cast<Item **>(arena->alloc(sizeof(Item *) * n_elems));
2488 if (array == nullptr) return true;
2489
2490 base_ref_items = Ref_item_array(array, n_elems);
2491
2492 return false;
2493 }
2494
print(const THD * thd,String * str,enum_query_type query_type)2495 void SELECT_LEX_UNIT::print(const THD *thd, String *str,
2496 enum_query_type query_type) {
2497 if (m_with_clause) m_with_clause->print(thd, str, query_type);
2498 bool union_all = !union_distinct;
2499 for (SELECT_LEX *sl = first_select(); sl; sl = sl->next_select()) {
2500 if (sl != first_select()) {
2501 str->append(STRING_WITH_LEN(" union "));
2502 if (union_all)
2503 str->append(STRING_WITH_LEN("all "));
2504 else if (union_distinct == sl)
2505 union_all = true;
2506 }
2507 bool parentheses_are_needed =
2508 sl->has_explicit_limit_or_order() &&
2509 (is_union() || (fake_select_lex != nullptr &&
2510 fake_select_lex->has_explicit_limit_or_order()));
2511 if (parentheses_are_needed) str->append('(');
2512 sl->print(thd, str, query_type);
2513 if (parentheses_are_needed) str->append(')');
2514 }
2515 if (fake_select_lex) {
2516 if (fake_select_lex->order_list.elements) {
2517 str->append(STRING_WITH_LEN(" order by "));
2518 fake_select_lex->print_order(thd, str, fake_select_lex->order_list.first,
2519 query_type);
2520 }
2521 fake_select_lex->print_limit(thd, str, query_type);
2522 } else if (saved_fake_select_lex)
2523 saved_fake_select_lex->print_limit(thd, str, query_type);
2524 }
2525
print_order(const THD * thd,String * str,ORDER * order,enum_query_type query_type)2526 void SELECT_LEX::print_order(const THD *thd, String *str, ORDER *order,
2527 enum_query_type query_type) {
2528 for (; order; order = order->next) {
2529 (*order->item)->print_for_order(thd, str, query_type, order->used_alias);
2530 if (order->direction == ORDER_DESC) str->append(STRING_WITH_LEN(" desc"));
2531 if (order->next) str->append(',');
2532 }
2533 }
2534
print_limit(const THD * thd,String * str,enum_query_type query_type)2535 void SELECT_LEX::print_limit(const THD *thd, String *str,
2536 enum_query_type query_type) {
2537 SELECT_LEX_UNIT *unit = master_unit();
2538 Item_subselect *item = unit->item;
2539
2540 if (item && unit->global_parameters() == this) {
2541 Item_subselect::subs_type subs_type = item->substype();
2542 if (subs_type == Item_subselect::EXISTS_SUBS ||
2543 subs_type == Item_subselect::IN_SUBS ||
2544 subs_type == Item_subselect::ALL_SUBS)
2545 return;
2546 }
2547 if (explicit_limit) {
2548 str->append(STRING_WITH_LEN(" limit "));
2549 if (offset_limit) {
2550 offset_limit->print(thd, str, query_type);
2551 str->append(',');
2552 }
2553 select_limit->print(thd, str, query_type);
2554 }
2555 }
2556
2557 /**
2558 @brief Print an index hint
2559
2560 @details Prints out the USE|FORCE|IGNORE index hint.
2561
2562 @param thd the current thread
2563 @param[out] str appends the index hint here
2564 */
2565
print(const THD * thd,String * str)2566 void Index_hint::print(const THD *thd, String *str) {
2567 switch (type) {
2568 case INDEX_HINT_IGNORE:
2569 str->append(STRING_WITH_LEN("IGNORE INDEX"));
2570 break;
2571 case INDEX_HINT_USE:
2572 str->append(STRING_WITH_LEN("USE INDEX"));
2573 break;
2574 case INDEX_HINT_FORCE:
2575 str->append(STRING_WITH_LEN("FORCE INDEX"));
2576 break;
2577 }
2578 switch (clause) {
2579 case INDEX_HINT_MASK_ALL:
2580 break;
2581 case INDEX_HINT_MASK_JOIN:
2582 str->append(STRING_WITH_LEN(" FOR JOIN"));
2583 break;
2584 case INDEX_HINT_MASK_ORDER:
2585 str->append(STRING_WITH_LEN(" FOR ORDER BY"));
2586 break;
2587 case INDEX_HINT_MASK_GROUP:
2588 str->append(STRING_WITH_LEN(" FOR GROUP BY"));
2589 break;
2590 }
2591
2592 str->append(STRING_WITH_LEN(" ("));
2593 if (key_name.length) {
2594 if (thd && !my_strnncoll(system_charset_info, (const uchar *)key_name.str,
2595 key_name.length, (const uchar *)primary_key_name,
2596 strlen(primary_key_name)))
2597 str->append(primary_key_name);
2598 else
2599 append_identifier(thd, str, key_name.str, key_name.length);
2600 }
2601 str->append(')');
2602 }
2603
2604 typedef Prealloced_array<TABLE_LIST *, 8> Table_array;
2605
print_table_array(const THD * thd,String * str,const Table_array & tables,enum_query_type query_type)2606 static void print_table_array(const THD *thd, String *str,
2607 const Table_array &tables,
2608 enum_query_type query_type) {
2609 DBUG_ASSERT(!tables.empty());
2610
2611 Table_array::const_iterator it = tables.begin();
2612 bool first = true;
2613 for (; it != tables.end(); ++it) {
2614 TABLE_LIST *curr = *it;
2615
2616 const bool is_optimized =
2617 curr->select_lex->join && curr->select_lex->join->is_optimized();
2618
2619 // the JOIN ON condition
2620 Item *const cond =
2621 is_optimized ? curr->join_cond_optim() : curr->join_cond();
2622
2623 // Print the join operator which relates this table to the previous one
2624 const char *op = nullptr;
2625 if (curr->is_aj_nest())
2626 op = " anti join ";
2627 else if (curr->is_sj_nest())
2628 op = " semi join ";
2629 else if (curr->outer_join) {
2630 /* MySQL converts right to left joins */
2631 op = " left join ";
2632 } else if (!first || cond) {
2633 /*
2634 If it's the first table, and it has an ON condition (can happen due to
2635 query transformations, e.g. merging a single-table view moves view's
2636 WHERE to table's ON): ON also needs JOIN.
2637 */
2638 op = curr->straight ? " straight_join " : " join ";
2639 }
2640
2641 if (op) {
2642 if (first) {
2643 // Add a dummy table before the operator, to have sensible SQL:
2644 str->append(STRING_WITH_LEN("<constant table>"));
2645 }
2646 str->append(op);
2647 }
2648 curr->print(thd, str, query_type); // Print table
2649 /*
2650 Print table hint info after the table name. Used only
2651 for explaining views. There is no functionality, just
2652 additional info for user.
2653 */
2654 if (thd->lex->is_explain() && curr->opt_hints_table &&
2655 curr->belong_to_view) {
2656 str->append(STRING_WITH_LEN(" /*+ "));
2657 curr->opt_hints_table->print(thd, str, query_type);
2658 str->append(STRING_WITH_LEN("*/ "));
2659 }
2660 // Print join condition
2661 if (cond) {
2662 str->append(STRING_WITH_LEN(" on("));
2663 cond->print(thd, str, query_type);
2664 str->append(')');
2665 }
2666 first = false;
2667 }
2668 }
2669
2670 /**
2671 Print joins from the FROM clause.
2672
2673 @param thd thread handler
2674 @param str string where table should be printed
2675 @param tables list of tables in join
2676 @param query_type type of the query is being generated
2677 */
2678
print_join(const THD * thd,String * str,mem_root_deque<TABLE_LIST * > * tables,enum_query_type query_type)2679 static void print_join(const THD *thd, String *str,
2680 mem_root_deque<TABLE_LIST *> *tables,
2681 enum_query_type query_type) {
2682 /* List is reversed => we should reverse it before using */
2683
2684 /*
2685 If the QT_NO_DATA_EXPANSION flag is specified, we print the
2686 original table list, including constant tables that have been
2687 optimized away, as the constant tables may be referenced in the
2688 expression printed by Item_field::print() when this flag is given.
2689 Otherwise, only non-const tables are printed.
2690
2691 Example:
2692
2693 Original SQL:
2694 select * from (select 1) t
2695
2696 Printed without QT_NO_DATA_EXPANSION:
2697 select '1' AS `1` from dual
2698
2699 Printed with QT_NO_DATA_EXPANSION:
2700 select `t`.`1` from (select 1 AS `1`) `t`
2701 */
2702 const bool print_const_tables = (query_type & QT_NO_DATA_EXPANSION);
2703 Table_array tables_to_print(PSI_NOT_INSTRUMENTED);
2704
2705 for (TABLE_LIST *t : *tables) {
2706 if (print_const_tables || !t->optimized_away)
2707 if (tables_to_print.push_back(t)) return; /* purecov: inspected */
2708 }
2709
2710 if (tables_to_print.empty()) {
2711 str->append(STRING_WITH_LEN("dual"));
2712 return; // all tables were optimized away
2713 }
2714
2715 std::reverse(tables_to_print.begin(), tables_to_print.end());
2716 print_table_array(thd, str, tables_to_print, query_type);
2717 }
2718
2719 /**
2720 @returns whether a database is equal to the connection's default database
2721 */
db_is_default_db(const char * db,size_t db_len,const THD * thd)2722 bool db_is_default_db(const char *db, size_t db_len, const THD *thd) {
2723 return thd != nullptr && thd->db().str != nullptr &&
2724 thd->db().length == db_len && !memcmp(db, thd->db().str, db_len);
2725 }
2726
2727 /*.*
2728 Print table as it should be in join list.
2729
2730 @param str string where table should be printed
2731 */
2732
print(const THD * thd,String * str,enum_query_type query_type) const2733 void TABLE_LIST::print(const THD *thd, String *str,
2734 enum_query_type query_type) const {
2735 if (nested_join) {
2736 str->append('(');
2737 print_join(thd, str, &nested_join->join_list, query_type);
2738 str->append(')');
2739 } else {
2740 const char *cmp_name; // Name to compare with alias
2741 if (view_name.length) {
2742 // A view or CTE
2743 if (view_db.length && !(query_type & QT_NO_DB) &&
2744 !((query_type & QT_NO_DEFAULT_DB) &&
2745 db_is_default_db(view_db.str, view_db.length, thd))) {
2746 append_identifier(thd, str, view_db.str, view_db.length);
2747 str->append('.');
2748 }
2749 append_identifier(thd, str, view_name.str, view_name.length);
2750 cmp_name = view_name.str;
2751 } else if (is_table_function()) {
2752 table_function->print(str, query_type);
2753 cmp_name = table_name;
2754 } else if (is_derived() && !is_merged()) {
2755 // A derived table that is materialized or without specified algorithm
2756 if (!(query_type & QT_DERIVED_TABLE_ONLY_ALIAS)) {
2757 if (derived_unit()->m_lateral_deps)
2758 str->append(STRING_WITH_LEN("lateral "));
2759 str->append('(');
2760 derived->print(thd, str, query_type);
2761 str->append(')');
2762 }
2763 cmp_name = ""; // Force printing of alias
2764 } else {
2765 // A normal table
2766
2767 if (!(query_type & QT_NO_DB) && !((query_type & QT_NO_DEFAULT_DB) &&
2768 db_is_default_db(db, db_length, thd))) {
2769 append_identifier(thd, str, db, db_length);
2770 str->append('.');
2771 }
2772 if (schema_table) {
2773 append_identifier(thd, str, schema_table_name,
2774 strlen(schema_table_name));
2775 cmp_name = schema_table_name;
2776 } else {
2777 append_identifier(thd, str, table_name, table_name_length);
2778 cmp_name = table_name;
2779 }
2780 if (partition_names && partition_names->elements) {
2781 int i, num_parts = partition_names->elements;
2782 List_iterator<String> name_it(*(partition_names));
2783 str->append(STRING_WITH_LEN(" PARTITION ("));
2784 for (i = 1; i <= num_parts; i++) {
2785 String *name = name_it++;
2786 append_identifier(thd, str, name->c_ptr(), name->length());
2787 if (i != num_parts) str->append(',');
2788 }
2789 str->append(')');
2790 }
2791 }
2792 if (my_strcasecmp(table_alias_charset, cmp_name, alias)) {
2793 char t_alias_buff[MAX_ALIAS_NAME];
2794 const char *t_alias = alias;
2795
2796 str->append(' ');
2797 if (lower_case_table_names == 1) {
2798 if (alias && alias[0]) // Print alias in lowercase
2799 {
2800 my_stpcpy(t_alias_buff, alias);
2801 my_casedn_str(files_charset_info, t_alias_buff);
2802 t_alias = t_alias_buff;
2803 }
2804 }
2805
2806 append_identifier(thd, str, t_alias, strlen(t_alias));
2807 }
2808
2809 /*
2810 The optional column list is to be specified in the definition. For a
2811 CTE, the definition is in WITH, and here we only have a
2812 reference. For a Derived Table, the definition is here.
2813 */
2814 if (!view_name.length)
2815 print_derived_column_names(thd, str, m_derived_column_names);
2816
2817 if (index_hints) {
2818 List_iterator<Index_hint> it(*index_hints);
2819 Index_hint *hint;
2820
2821 while ((hint = it++)) {
2822 str->append(STRING_WITH_LEN(" "));
2823 hint->print(thd, str);
2824 }
2825 }
2826 }
2827 }
2828
print(const THD * thd,String * str,enum_query_type query_type)2829 void SELECT_LEX::print(const THD *thd, String *str,
2830 enum_query_type query_type) {
2831 /* QQ: thd may not be set for sub queries, but this should be fixed */
2832 if (!thd) thd = current_thd;
2833
2834 if (select_number == 1) {
2835 if (print_error(thd, str)) return;
2836
2837 switch (parent_lex->sql_command) {
2838 case SQLCOM_UPDATE: // Fall through
2839 case SQLCOM_UPDATE_MULTI:
2840 print_update(thd, str, query_type);
2841 return;
2842 case SQLCOM_DELETE: // Fall through
2843 case SQLCOM_DELETE_MULTI:
2844 print_delete(thd, str, query_type);
2845 return;
2846 case SQLCOM_INSERT: // Fall through
2847 case SQLCOM_INSERT_SELECT:
2848 case SQLCOM_REPLACE:
2849 case SQLCOM_REPLACE_SELECT:
2850 print_insert(thd, str, query_type);
2851 return;
2852 case SQLCOM_SELECT: // Fall through
2853 default:
2854 break;
2855 }
2856 }
2857 if (is_table_value_constructor) {
2858 print_values(thd, str, query_type, *row_value_list, "row");
2859 } else {
2860 print_select(thd, str, query_type);
2861 }
2862 }
2863
print_select(const THD * thd,String * str,enum_query_type query_type)2864 void SELECT_LEX::print_select(const THD *thd, String *str,
2865 enum_query_type query_type) {
2866 if (query_type & QT_SHOW_SELECT_NUMBER) {
2867 /* it makes EXPLAIN's "id" column understandable */
2868 str->append("/* select#");
2869 if (unlikely(select_number >= INT_MAX))
2870 str->append("fake");
2871 else
2872 str->append_ulonglong(select_number);
2873 str->append(" */ select ");
2874 } else
2875 str->append(STRING_WITH_LEN("select "));
2876
2877 print_hints(thd, str, query_type);
2878 print_select_options(str);
2879 print_item_list(thd, str, query_type);
2880 print_from_clause(thd, str, query_type);
2881 print_where_cond(thd, str, query_type);
2882 print_group_by(thd, str, query_type);
2883 print_having(thd, str, query_type);
2884 print_windows(thd, str, query_type);
2885 print_order_by(thd, str, query_type);
2886 print_limit(thd, str, query_type);
2887 // PROCEDURE unsupported here
2888 }
2889
print_update(const THD * thd,String * str,enum_query_type query_type)2890 void SELECT_LEX::print_update(const THD *thd, String *str,
2891 enum_query_type query_type) {
2892 Sql_cmd_update *sql_cmd_update =
2893 (static_cast<Sql_cmd_update *>(parent_lex->m_sql_cmd));
2894 str->append(STRING_WITH_LEN("update "));
2895 print_hints(thd, str, query_type);
2896 print_update_options(str);
2897 if (parent_lex->sql_command == SQLCOM_UPDATE) {
2898 // Single table update
2899 auto *t = table_list.first;
2900 t->print(thd, str, query_type); // table identifier
2901 str->append(STRING_WITH_LEN(" set "));
2902 print_update_list(thd, str, query_type, fields_list,
2903 *sql_cmd_update->update_value_list);
2904 /*
2905 Print join condition (may happen with a merged view's WHERE condition
2906 and disappears in simplify_joins(); visible in opt trace only).
2907 */
2908 Item *const cond = t->join_cond();
2909 if (cond) {
2910 str->append(STRING_WITH_LEN(" on("));
2911 cond->print(thd, str, query_type);
2912 str->append(')');
2913 }
2914 print_where_cond(thd, str, query_type);
2915 print_order_by(thd, str, query_type);
2916 print_limit(thd, str, query_type);
2917 } else {
2918 // Multi table update
2919 print_join(thd, str, &top_join_list, query_type);
2920 str->append(STRING_WITH_LEN(" set "));
2921 print_update_list(thd, str, query_type, fields_list,
2922 *sql_cmd_update->update_value_list);
2923 print_where_cond(thd, str, query_type);
2924 }
2925 }
2926
print_delete(const THD * thd,String * str,enum_query_type query_type)2927 void SELECT_LEX::print_delete(const THD *thd, String *str,
2928 enum_query_type query_type) {
2929 str->append(STRING_WITH_LEN("delete "));
2930 print_hints(thd, str, query_type);
2931 print_delete_options(str);
2932 if (parent_lex->sql_command == SQLCOM_DELETE) {
2933 TABLE_LIST *t = table_list.first;
2934 // Single table delete
2935 str->append(STRING_WITH_LEN("from "));
2936 t->print(thd, str, query_type); // table identifier
2937 /*
2938 Print join condition (may happen with a merged view's WHERE condition
2939 and disappears in simplify_joins(); visible in opt trace only).
2940 */
2941 Item *const cond = t->join_cond();
2942 if (cond) {
2943 str->append(STRING_WITH_LEN(" on("));
2944 cond->print(thd, str, query_type);
2945 str->append(')');
2946 }
2947 print_where_cond(thd, str, query_type);
2948 print_order_by(thd, str, query_type);
2949 print_limit(thd, str, query_type);
2950 } else {
2951 // Multi table delete
2952 print_table_references(thd, str, parent_lex->query_tables, query_type);
2953 str->append(STRING_WITH_LEN(" from "));
2954 print_join(thd, str, &top_join_list, query_type);
2955 print_where_cond(thd, str, query_type);
2956 }
2957 }
2958
print_insert(const THD * thd,String * str,enum_query_type query_type)2959 void SELECT_LEX::print_insert(const THD *thd, String *str,
2960 enum_query_type query_type) {
2961 /**
2962 USES: 'INSERT INTO table (fields) VALUES values' syntax over
2963 'INSERT INTO table SET field = value, ...'
2964 */
2965 Sql_cmd_insert_base *sql_cmd_insert =
2966 down_cast<Sql_cmd_insert_base *>(parent_lex->m_sql_cmd);
2967
2968 if (parent_lex->sql_command == SQLCOM_REPLACE ||
2969 parent_lex->sql_command == SQLCOM_REPLACE_SELECT)
2970 str->append(STRING_WITH_LEN("replace "));
2971 else
2972 str->append(STRING_WITH_LEN("insert "));
2973
2974 // Don't print QB name hints since it will be printed through print_select.
2975 print_hints(thd, str, enum_query_type(query_type | QT_IGNORE_QB_NAME));
2976 print_insert_options(str);
2977 str->append(STRING_WITH_LEN("into "));
2978
2979 TABLE_LIST *tbl = (parent_lex->insert_table_leaf)
2980 ? parent_lex->insert_table_leaf
2981 : table_list.first;
2982 tbl->print(thd, str, query_type); // table identifier
2983
2984 print_insert_fields(thd, str, query_type);
2985 str->append(STRING_WITH_LEN(" "));
2986
2987 if (parent_lex->sql_command == SQLCOM_INSERT ||
2988 parent_lex->sql_command == SQLCOM_REPLACE) {
2989 print_values(thd, str, query_type, sql_cmd_insert->insert_many_values,
2990 nullptr);
2991 } else {
2992 /*
2993 Print only QB name hint here since other hints were printed in the
2994 earlier call to print_hints.
2995 */
2996 print_select(thd, str, enum_query_type(query_type | QT_ONLY_QB_NAME));
2997 }
2998
2999 if (sql_cmd_insert->update_field_list.elements > 0) {
3000 str->append(STRING_WITH_LEN(" on duplicate key update "));
3001 print_update_list(thd, str, query_type, sql_cmd_insert->update_field_list,
3002 sql_cmd_insert->update_value_list);
3003 }
3004 }
3005
print_hints(const THD * thd,String * str,enum_query_type query_type)3006 void SELECT_LEX::print_hints(const THD *thd, String *str,
3007 enum_query_type query_type) {
3008 if (thd->lex->opt_hints_global) {
3009 char buff[NAME_LEN];
3010 String hint_str(buff, sizeof(buff), system_charset_info);
3011 hint_str.length(0);
3012
3013 if (select_number == 1 ||
3014 // First select number is 2 for SHOW CREATE VIEW
3015 (select_number == 2 && parent_lex->sql_command == SQLCOM_SHOW_CREATE)) {
3016 if (opt_hints_qb && !(query_type & QT_IGNORE_QB_NAME))
3017 opt_hints_qb->append_qb_hint(thd, &hint_str);
3018 if (!(query_type & QT_ONLY_QB_NAME))
3019 thd->lex->opt_hints_global->print(thd, &hint_str, query_type);
3020 } else if (opt_hints_qb)
3021 opt_hints_qb->append_qb_hint(thd, &hint_str);
3022
3023 if (hint_str.length() > 0) {
3024 str->append(STRING_WITH_LEN("/*+ "));
3025 str->append(hint_str.ptr(), hint_str.length());
3026 str->append(STRING_WITH_LEN("*/ "));
3027 }
3028 }
3029 }
3030
print_error(const THD * thd,String * str)3031 bool SELECT_LEX::print_error(const THD *thd, String *str) {
3032 if (thd->is_error()) {
3033 /*
3034 It is possible that this query block had an optimization error, but the
3035 caller didn't notice (caller evaluted this as a subquery and Item::val*()
3036 don't have an error status). In this case the query block may be broken
3037 and printing it may crash.
3038 */
3039 str->append(STRING_WITH_LEN("had some error"));
3040 return true;
3041 }
3042 /*
3043 In order to provide info for EXPLAIN FOR CONNECTION units shouldn't be
3044 completely cleaned till the end of the query. This is valid only for
3045 explainable commands.
3046 */
3047 DBUG_ASSERT(!(master_unit()->cleaned == SELECT_LEX_UNIT::UC_CLEAN &&
3048 is_explainable_query(thd->lex->sql_command)));
3049 return false;
3050 }
3051
print_select_options(String * str)3052 void SELECT_LEX::print_select_options(String *str) {
3053 /* First add options */
3054 if (active_options() & SELECT_STRAIGHT_JOIN)
3055 str->append(STRING_WITH_LEN("straight_join "));
3056 if (active_options() & SELECT_HIGH_PRIORITY)
3057 str->append(STRING_WITH_LEN("high_priority "));
3058 if (active_options() & SELECT_DISTINCT)
3059 str->append(STRING_WITH_LEN("distinct "));
3060 if (active_options() & SELECT_SMALL_RESULT)
3061 str->append(STRING_WITH_LEN("sql_small_result "));
3062 if (active_options() & SELECT_BIG_RESULT)
3063 str->append(STRING_WITH_LEN("sql_big_result "));
3064 if (active_options() & OPTION_BUFFER_RESULT)
3065 str->append(STRING_WITH_LEN("sql_buffer_result "));
3066 if (active_options() & OPTION_FOUND_ROWS)
3067 str->append(STRING_WITH_LEN("sql_calc_found_rows "));
3068 }
3069
print_update_options(String * str)3070 void SELECT_LEX::print_update_options(String *str) {
3071 if (table_list.first &&
3072 table_list.first->mdl_request.type == MDL_SHARED_WRITE_LOW_PRIO)
3073 str->append(STRING_WITH_LEN("low_priority "));
3074 if (parent_lex->is_ignore()) str->append(STRING_WITH_LEN("ignore "));
3075 }
3076
print_delete_options(String * str)3077 void SELECT_LEX::print_delete_options(String *str) {
3078 if (table_list.first &&
3079 table_list.first->mdl_request.type == MDL_SHARED_WRITE_LOW_PRIO)
3080 str->append(STRING_WITH_LEN("low_priority "));
3081 if (active_options() & OPTION_QUICK) str->append(STRING_WITH_LEN("quick "));
3082 if (parent_lex->is_ignore()) str->append(STRING_WITH_LEN("ignore "));
3083 }
3084
print_insert_options(String * str)3085 void SELECT_LEX::print_insert_options(String *str) {
3086 if (table_list.first) {
3087 int type = static_cast<int>(table_list.first->lock_descriptor().type);
3088
3089 // Lock option
3090 if (type == static_cast<int>(TL_WRITE_LOW_PRIORITY))
3091 str->append(STRING_WITH_LEN("low_priority "));
3092 else if (type == static_cast<int>(TL_WRITE))
3093 str->append(STRING_WITH_LEN("high_priority "));
3094 }
3095
3096 if (parent_lex->is_ignore()) str->append(STRING_WITH_LEN("ignore "));
3097 }
3098
print_table_references(const THD * thd,String * str,TABLE_LIST * table_list,enum_query_type query_type)3099 void SELECT_LEX::print_table_references(const THD *thd, String *str,
3100 TABLE_LIST *table_list,
3101 enum_query_type query_type) {
3102 bool first = true;
3103 for (TABLE_LIST *tbl = table_list; tbl; tbl = tbl->next_local) {
3104 if (tbl->updating) {
3105 if (first)
3106 first = false;
3107 else
3108 str->append(STRING_WITH_LEN(", "));
3109
3110 TABLE_LIST *t = tbl;
3111
3112 /*
3113 Query Rewrite Plugin will not have is_view() set even for a view. This
3114 is because operations like open_table haven't happend yet. So the
3115 underlying target tables will not be added, only the original
3116 table/view list will be reproduced. Ideally, it would be better if
3117 TABLE_LIST::updatable_base_table() were used here, but that isn't
3118 possible due to QRP.
3119 */
3120 while (t->is_view()) t = t->merge_underlying_list;
3121
3122 if (!(query_type & QT_NO_DB) &&
3123 !((query_type & QT_NO_DEFAULT_DB) &&
3124 db_is_default_db(t->db, t->db_length, thd))) {
3125 append_identifier(thd, str, t->db, t->db_length);
3126 str->append('.');
3127 }
3128 append_identifier(thd, str, t->table_name, t->table_name_length);
3129 }
3130 }
3131 }
3132
print_item_list(const THD * thd,String * str,enum_query_type query_type)3133 void SELECT_LEX::print_item_list(const THD *thd, String *str,
3134 enum_query_type query_type) {
3135 // Item List
3136 bool first = true;
3137 List_iterator_fast<Item> it(fields_list);
3138 Item *item;
3139 while ((item = it++)) {
3140 if (first)
3141 first = false;
3142 else
3143 str->append(',');
3144
3145 if ((master_unit()->item && item->item_name.is_autogenerated()) ||
3146 (query_type & QT_NORMALIZED_FORMAT)) {
3147 /*
3148 Do not print auto-generated aliases in subqueries. It has no purpose
3149 in a view definition or other contexts where the query is printed.
3150 */
3151 item->print(thd, str, query_type);
3152 } else
3153 item->print_item_w_name(thd, str, query_type);
3154 /** @note that 'INTO variable' clauses are not printed */
3155 }
3156 }
3157
print_update_list(const THD * thd,String * str,enum_query_type query_type,List<Item> fields,List<Item> values)3158 void SELECT_LEX::print_update_list(const THD *thd, String *str,
3159 enum_query_type query_type,
3160 List<Item> fields, List<Item> values) {
3161 List_iterator<Item> it_column(fields), it_value(values);
3162 Item *column, *value;
3163 bool first = true;
3164 while ((column = it_column++) && (value = it_value++)) {
3165 if (first)
3166 first = false;
3167 else
3168 str->append(',');
3169
3170 column->print(thd, str, query_type);
3171 str->append(STRING_WITH_LEN(" = "));
3172 value->print(thd, str, enum_query_type(query_type & ~QT_NO_DATA_EXPANSION));
3173 }
3174 }
3175
print_insert_fields(const THD * thd,String * str,enum_query_type query_type)3176 void SELECT_LEX::print_insert_fields(const THD *thd, String *str,
3177 enum_query_type query_type) {
3178 List<Item> fields = static_cast<Sql_cmd_insert_base *>(parent_lex->m_sql_cmd)
3179 ->insert_field_list;
3180 if (fields.elements > 0) {
3181 str->append(STRING_WITH_LEN(" ("));
3182 List_iterator<Item> it_field(fields);
3183 bool first = true;
3184 while (Item *field = it_field++) {
3185 if (first)
3186 first = false;
3187 else
3188 str->append(',');
3189
3190 field->print(thd, str, query_type);
3191 }
3192 str->append(')');
3193 }
3194 }
3195
print_values(const THD * thd,String * str,enum_query_type query_type,List<List<Item>> values,const char * prefix)3196 void SELECT_LEX::print_values(const THD *thd, String *str,
3197 enum_query_type query_type,
3198 List<List<Item>> values, const char *prefix) {
3199 str->append(STRING_WITH_LEN("values "));
3200 bool row_first = true;
3201 for (List<Item> &row : values) {
3202 if (row_first)
3203 row_first = false;
3204 else
3205 str->append(',');
3206
3207 if (prefix != nullptr) str->append(prefix);
3208
3209 str->append('(');
3210 List_iterator<Item> it_col(row);
3211 bool col_first = true;
3212 while (Item *item = it_col++) {
3213 if (col_first)
3214 col_first = false;
3215 else
3216 str->append(',');
3217
3218 item->print(thd, str, query_type);
3219 }
3220 str->append(')');
3221 }
3222 }
3223
print_from_clause(const THD * thd,String * str,enum_query_type query_type)3224 void SELECT_LEX::print_from_clause(const THD *thd, String *str,
3225 enum_query_type query_type) {
3226 /*
3227 from clause
3228 */
3229 if (table_list.elements) {
3230 str->append(STRING_WITH_LEN(" from "));
3231 /* go through join tree */
3232 print_join(thd, str, &top_join_list, query_type);
3233 } else if (m_where_cond) {
3234 /*
3235 "SELECT 1 FROM DUAL WHERE 2" should not be printed as
3236 "SELECT 1 WHERE 2": the 1st syntax is valid, but the 2nd is not.
3237 */
3238 str->append(STRING_WITH_LEN(" from DUAL "));
3239 }
3240 }
3241
print_where_cond(const THD * thd,String * str,enum_query_type query_type)3242 void SELECT_LEX::print_where_cond(const THD *thd, String *str,
3243 enum_query_type query_type) {
3244 // Where
3245 Item *const cur_where =
3246 (join && join->is_optimized()) ? join->where_cond : m_where_cond;
3247
3248 if (cur_where || cond_value != Item::COND_UNDEF) {
3249 str->append(STRING_WITH_LEN(" where "));
3250 if (cur_where)
3251 cur_where->print(thd, str, query_type);
3252 else
3253 str->append(cond_value != Item::COND_FALSE ? "true" : "false");
3254 }
3255 }
3256
print_group_by(const THD * thd,String * str,enum_query_type query_type)3257 void SELECT_LEX::print_group_by(const THD *thd, String *str,
3258 enum_query_type query_type) {
3259 // group by & olap
3260 if (group_list.elements) {
3261 str->append(STRING_WITH_LEN(" group by "));
3262 print_order(thd, str, group_list.first, query_type);
3263 switch (olap) {
3264 case ROLLUP_TYPE:
3265 str->append(STRING_WITH_LEN(" with rollup"));
3266 break;
3267 default:; // satisfy compiler
3268 }
3269 }
3270 }
3271
print_having(const THD * thd,String * str,enum_query_type query_type)3272 void SELECT_LEX::print_having(const THD *thd, String *str,
3273 enum_query_type query_type) {
3274 // having
3275 Item *const cur_having = (join && join->having_for_explain != (Item *)1)
3276 ? join->having_for_explain
3277 : m_having_cond;
3278
3279 if (cur_having || having_value != Item::COND_UNDEF) {
3280 str->append(STRING_WITH_LEN(" having "));
3281 if (cur_having)
3282 cur_having->print(thd, str, query_type);
3283 else
3284 str->append(having_value != Item::COND_FALSE ? "true" : "false");
3285 }
3286 }
3287
print_windows(const THD * thd,String * str,enum_query_type query_type)3288 void SELECT_LEX::print_windows(const THD *thd, String *str,
3289 enum_query_type query_type) {
3290 List_iterator<Window> li(m_windows);
3291 Window *w;
3292 bool first = true;
3293 while ((w = li++)) {
3294 if (w->name() == nullptr) continue; // will be printed with function
3295
3296 if (first) {
3297 first = false;
3298 str->append(" window ");
3299 } else {
3300 str->append(", ");
3301 }
3302
3303 append_identifier(thd, str, w->name()->item_name.ptr(),
3304 strlen(w->name()->item_name.ptr()));
3305 str->append(" AS ");
3306 w->print(thd, str, query_type, true);
3307 }
3308 }
3309
print_order_by(const THD * thd,String * str,enum_query_type query_type)3310 void SELECT_LEX::print_order_by(const THD *thd, String *str,
3311 enum_query_type query_type) {
3312 if (order_list.elements) {
3313 str->append(STRING_WITH_LEN(" order by "));
3314 print_order(thd, str, order_list.first, query_type);
3315 }
3316 }
3317
get_walk_flags(const Select_lex_visitor * visitor)3318 static enum_walk get_walk_flags(const Select_lex_visitor *visitor) {
3319 if (visitor->visits_in_prefix_order())
3320 return enum_walk::SUBQUERY_PREFIX;
3321 else
3322 return enum_walk::SUBQUERY_POSTFIX;
3323 }
3324
walk_item(Item * item,Select_lex_visitor * visitor)3325 bool walk_item(Item *item, Select_lex_visitor *visitor) {
3326 if (item == nullptr) return false;
3327 return item->walk(&Item::visitor_processor, get_walk_flags(visitor),
3328 pointer_cast<uchar *>(visitor));
3329 }
3330
accept_for_order(SQL_I_List<ORDER> orders,Select_lex_visitor * visitor)3331 bool accept_for_order(SQL_I_List<ORDER> orders, Select_lex_visitor *visitor) {
3332 if (orders.elements == 0) return false;
3333
3334 for (ORDER *order = orders.first; order != nullptr; order = order->next)
3335 if (walk_item(*order->item, visitor)) return true;
3336 return false;
3337 }
3338
accept(Select_lex_visitor * visitor)3339 bool SELECT_LEX_UNIT::accept(Select_lex_visitor *visitor) {
3340 SELECT_LEX *end = nullptr;
3341 for (SELECT_LEX *sl = first_select(); sl != end; sl = sl->next_select())
3342 if (sl->accept(visitor)) return true;
3343
3344 if (fake_select_lex && accept_for_order(fake_select_lex->order_list, visitor))
3345 return true;
3346
3347 return visitor->visit(this);
3348 }
3349
accept_for_join(mem_root_deque<TABLE_LIST * > * tables,Select_lex_visitor * visitor)3350 bool accept_for_join(mem_root_deque<TABLE_LIST *> *tables,
3351 Select_lex_visitor *visitor) {
3352 for (TABLE_LIST *t : *tables) {
3353 if (accept_table(t, visitor)) return true;
3354 }
3355 return false;
3356 }
3357
accept_table(TABLE_LIST * t,Select_lex_visitor * visitor)3358 bool accept_table(TABLE_LIST *t, Select_lex_visitor *visitor) {
3359 if (t->nested_join && accept_for_join(&t->nested_join->join_list, visitor))
3360 return true;
3361 else if (t->is_derived())
3362 t->derived_unit()->accept(visitor);
3363 if (walk_item(t->join_cond(), visitor)) return true;
3364 return false;
3365 }
3366
accept(Select_lex_visitor * visitor)3367 bool SELECT_LEX::accept(Select_lex_visitor *visitor) {
3368 // Select clause
3369 List_iterator<Item> it(fields_list);
3370 Item *end = nullptr;
3371 for (Item *item = it++; item != end; item = it++)
3372 if (walk_item(item, visitor)) return true;
3373
3374 // From clause
3375 if (table_list.elements != 0 && accept_for_join(join_list, visitor))
3376 return true;
3377
3378 // Where clause
3379 Item *where_condition = join != nullptr ? join->where_cond : m_where_cond;
3380 if (where_condition != nullptr && walk_item(where_condition, visitor))
3381 return true;
3382
3383 // Group by and olap clauses
3384 if (accept_for_order(group_list, visitor)) return true;
3385
3386 // Having clause
3387 Item *having_condition =
3388 join != nullptr ? join->having_for_explain : m_having_cond;
3389 if (walk_item(having_condition, visitor)) return true;
3390
3391 // Order clause
3392 if (accept_for_order(order_list, visitor)) return true;
3393
3394 // Limit clause
3395 if (explicit_limit)
3396 if (walk_item(offset_limit, visitor) || walk_item(select_limit, visitor))
3397 return true;
3398
3399 return visitor->visit(this);
3400 }
3401
clear_privileges()3402 void LEX::clear_privileges() {
3403 users_list.empty();
3404 columns.empty();
3405 grant = grant_tot_col = grant_privilege = false;
3406 all_privileges = false;
3407 ssl_type = SSL_TYPE_NOT_SPECIFIED;
3408 ssl_cipher = x509_subject = x509_issuer = nullptr;
3409 alter_password.cleanup();
3410 memset(&mqh, 0, sizeof(mqh));
3411 dynamic_privileges.empty();
3412 default_roles = nullptr;
3413 }
3414
3415 /*
3416 Initialize (or reset) Query_tables_list object.
3417
3418 SYNOPSIS
3419 reset_query_tables_list()
3420 init true - we should perform full initialization of object with
3421 allocating needed memory
3422 false - object is already initialized so we should only reset
3423 its state so it can be used for parsing/processing
3424 of new statement
3425
3426 DESCRIPTION
3427 This method initializes Query_tables_list so it can be used as part
3428 of LEX object for parsing/processing of statement. One can also use
3429 this method to reset state of already initialized Query_tables_list
3430 so it can be used for processing of new statement.
3431 */
3432
reset_query_tables_list(bool init)3433 void Query_tables_list::reset_query_tables_list(bool init) {
3434 sql_command = SQLCOM_END;
3435 if (!init && query_tables) {
3436 TABLE_LIST *table = query_tables;
3437 for (;;) {
3438 delete table->view_query();
3439 if (query_tables_last == &table->next_global ||
3440 !(table = table->next_global))
3441 break;
3442 }
3443 }
3444 query_tables = nullptr;
3445 query_tables_last = &query_tables;
3446 query_tables_own_last = nullptr;
3447 if (init) {
3448 /*
3449 We delay real initialization of hash (and therefore related
3450 memory allocation) until first insertion into this hash.
3451 */
3452 sroutines.reset();
3453 } else if (sroutines != nullptr) {
3454 sroutines->clear();
3455 }
3456 sroutines_list.empty();
3457 sroutines_list_own_last = sroutines_list.next;
3458 sroutines_list_own_elements = 0;
3459 binlog_stmt_flags = 0;
3460 stmt_accessed_table_flag = 0;
3461 lock_tables_state = LTS_NOT_LOCKED;
3462 table_count = 0;
3463 using_match = false;
3464 stmt_unsafe_with_mixed_mode = false;
3465
3466 /* Check the max size of the enum to control new enum values definitions. */
3467 static_assert(BINLOG_STMT_UNSAFE_COUNT <= 32, "");
3468 }
3469
3470 /*
3471 Destroy Query_tables_list object with freeing all resources used by it.
3472
3473 SYNOPSIS
3474 destroy_query_tables_list()
3475 */
3476
destroy_query_tables_list()3477 void Query_tables_list::destroy_query_tables_list() { sroutines.reset(); }
3478
3479 /*
3480 Initialize LEX object.
3481
3482 SYNOPSIS
3483 LEX::LEX()
3484
3485 NOTE
3486 LEX object initialized with this constructor can be used as part of
3487 THD object for which one can safely call open_tables(), lock_tables()
3488 and close_thread_tables() functions. But it is not yet ready for
3489 statement parsing. On should use lex_start() function to prepare LEX
3490 for this.
3491 */
3492
LEX()3493 LEX::LEX()
3494 : result(nullptr),
3495 thd(nullptr),
3496 opt_hints_global(nullptr),
3497 // Quite unlikely to overflow initial allocation, so no instrumentation.
3498 plugins(PSI_NOT_INSTRUMENTED),
3499 insert_update_values_map(nullptr),
3500 option_type(OPT_DEFAULT),
3501 drop_temporary(false),
3502 sphead(nullptr),
3503 // Initialize here to avoid uninitialized variable warnings.
3504 contains_plaintext_password(false),
3505 keep_diagnostics(DA_KEEP_UNSPECIFIED),
3506 is_lex_started(false),
3507 in_update_value_clause(false),
3508 will_contextualize(true) {
3509 reset_query_tables_list(true);
3510 }
3511
3512 /**
3513 check if command can use VIEW with MERGE algorithm (for top VIEWs)
3514
3515 @details
3516 Only listed here commands can use merge algorithm in top level
3517 SELECT_LEX (for subqueries will be used merge algorithm if
3518 LEX::can_not_use_merged() is not true).
3519
3520 @todo - Add SET as a command that can use merged views. Due to how
3521 all uses would be embedded in subqueries, this test is worthless
3522 for the SET command anyway.
3523
3524 @returns true if command can use merged VIEWs, false otherwise
3525 */
3526
can_use_merged()3527 bool LEX::can_use_merged() {
3528 switch (sql_command) {
3529 case SQLCOM_SELECT:
3530 case SQLCOM_CREATE_TABLE:
3531 case SQLCOM_UPDATE:
3532 case SQLCOM_UPDATE_MULTI:
3533 case SQLCOM_DELETE:
3534 case SQLCOM_DELETE_MULTI:
3535 case SQLCOM_INSERT:
3536 case SQLCOM_INSERT_SELECT:
3537 case SQLCOM_REPLACE:
3538 case SQLCOM_REPLACE_SELECT:
3539 case SQLCOM_LOAD:
3540
3541 /*
3542 With WL#6599 following SHOW commands are implemented over the
3543 INFORMATION_SCHEMA system views, and we do not create
3544 temporary tables anymore now. So these queries should be
3545 allowed to be mergeable, which makes the INFORMATION_SCHEMA
3546 query execution faster.
3547
3548 According to optimizer team (Roy), making this decision based on
3549 the command type here is a hack. This should probably change when
3550 we introduce Sql_cmd_show class, which should treat the following
3551 SHOW commands same as SQLCOM_SELECT.
3552 */
3553 case SQLCOM_SHOW_CHARSETS:
3554 case SQLCOM_SHOW_COLLATIONS:
3555 case SQLCOM_SHOW_DATABASES:
3556 case SQLCOM_SHOW_EVENTS:
3557 case SQLCOM_SHOW_FIELDS:
3558 case SQLCOM_SHOW_KEYS:
3559 case SQLCOM_SHOW_STATUS_FUNC:
3560 case SQLCOM_SHOW_STATUS_PROC:
3561 case SQLCOM_SHOW_TABLES:
3562 case SQLCOM_SHOW_TABLE_STATUS:
3563 case SQLCOM_SHOW_TRIGGERS:
3564 return true;
3565 default:
3566 return false;
3567 }
3568 }
3569
3570 /**
3571 Check if command can't use merged views in any part of command
3572
3573 @details
3574 Temporary table algorithm will be used on all SELECT levels for queries
3575 listed here (see also LEX::can_use_merged()).
3576
3577 @returns true if command cannot use merged view, false otherwise
3578 */
3579
can_not_use_merged()3580 bool LEX::can_not_use_merged() {
3581 switch (sql_command) {
3582 case SQLCOM_CREATE_VIEW:
3583 case SQLCOM_SHOW_CREATE:
3584 return true;
3585 default:
3586 return false;
3587 }
3588 }
3589
3590 /*
3591 case SQLCOM_REVOKE_ROLE:
3592 case SQLCOM_GRANT_ROLE:
3593 Should Items_ident be printed correctly
3594
3595 SYNOPSIS
3596 need_correct_ident()
3597
3598 RETURN
3599 true yes, we need only structure
3600 false no, we need data
3601 */
3602
need_correct_ident()3603 bool LEX::need_correct_ident() {
3604 switch (sql_command) {
3605 case SQLCOM_SHOW_CREATE:
3606 case SQLCOM_SHOW_TABLES:
3607 case SQLCOM_CREATE_VIEW:
3608 return true;
3609 default:
3610 return false;
3611 }
3612 }
3613
3614 /**
3615 This method should be called only during parsing.
3616 It is aware of compound statements (stored routine bodies)
3617 and will initialize the destination with the default
3618 database of the stored routine, rather than the default
3619 database of the connection it is parsed in.
3620 E.g. if one has no current database selected, or current database
3621 set to 'bar' and then issues:
3622
3623 CREATE PROCEDURE foo.p1() BEGIN SELECT * FROM t1 END//
3624
3625 t1 is meant to refer to foo.t1, not to bar.t1.
3626
3627 This method is needed to support this rule.
3628
3629 @return true in case of error (parsing should be aborted, false in
3630 case of success
3631 */
3632
copy_db_to(char const ** p_db,size_t * p_db_length) const3633 bool LEX::copy_db_to(char const **p_db, size_t *p_db_length) const {
3634 if (sphead) {
3635 DBUG_ASSERT(sphead->m_db.str && sphead->m_db.length);
3636 /*
3637 It is safe to assign the string by-pointer, both sphead and
3638 its statements reside in the same memory root.
3639 */
3640 *p_db = sphead->m_db.str;
3641 if (p_db_length) *p_db_length = sphead->m_db.length;
3642 return false;
3643 }
3644 return thd->copy_db_to(p_db, p_db_length);
3645 }
3646
3647 /**
3648 Prepare sources for offset and limit counters.
3649
3650 @param thd thread handler
3651 @param provider SELECT_LEX to get offset and limit from.
3652
3653 @returns false if success, true if error
3654 */
prepare_limit(THD * thd,SELECT_LEX * provider)3655 bool SELECT_LEX_UNIT::prepare_limit(THD *thd, SELECT_LEX *provider) {
3656 if (provider->offset_limit &&
3657 provider->offset_limit->fix_fields(thd, nullptr))
3658 return true; /* purecov: inspected */
3659
3660 if (provider->select_limit &&
3661 provider->select_limit->fix_fields(thd, nullptr))
3662 return true; /* purecov: inspected */
3663
3664 return false;
3665 }
3666
3667 /**
3668 Set limit and offset for query expression object
3669
3670 @param thd thread handler
3671 @param provider SELECT_LEX to get offset and limit from.
3672
3673 @returns false if success, true if error
3674 */
set_limit(THD * thd,SELECT_LEX * provider)3675 bool SELECT_LEX_UNIT::set_limit(THD *thd, SELECT_LEX *provider) {
3676 if (provider->offset_limit)
3677 offset_limit_cnt = provider->get_offset(thd);
3678 else
3679 offset_limit_cnt = 0;
3680
3681 if (provider->select_limit)
3682 select_limit_cnt = provider->get_limit(thd);
3683 else
3684 select_limit_cnt = HA_POS_ERROR;
3685
3686 if (select_limit_cnt + offset_limit_cnt >= select_limit_cnt)
3687 select_limit_cnt += offset_limit_cnt;
3688 else
3689 select_limit_cnt = HA_POS_ERROR;
3690
3691 return false;
3692 }
3693
3694 /**
3695 Decide if a temporary table is needed for the UNION.
3696
3697 @retval true A temporary table is needed.
3698 @retval false A temporary table is not needed.
3699
3700 @todo figure out if the test for "top-level unit" is necessary - see
3701 bug#23022426.
3702 */
union_needs_tmp_table(LEX * lex)3703 bool SELECT_LEX_UNIT::union_needs_tmp_table(LEX *lex) {
3704 return union_distinct != nullptr ||
3705 global_parameters()->order_list.elements != 0 ||
3706 ((lex->sql_command == SQLCOM_INSERT_SELECT ||
3707 lex->sql_command == SQLCOM_REPLACE_SELECT) &&
3708 lex->unit == this);
3709 }
3710
3711 /**
3712 Include a query expression below a query block.
3713
3714 @param lex Containing LEX object
3715 @param outer The query block that this query expression is included below.
3716 */
include_down(LEX * lex,SELECT_LEX * outer)3717 void SELECT_LEX_UNIT::include_down(LEX *lex, SELECT_LEX *outer) {
3718 if ((next = outer->slave)) next->prev = &next;
3719 prev = &outer->slave;
3720 outer->slave = this;
3721 master = outer;
3722
3723 renumber_selects(lex);
3724 }
3725
3726 /**
3727 Return true if query expression can be merged into an outer query, based on
3728 technical constraints.
3729 Being mergeable also means that derived table/view is updatable.
3730
3731 A view/derived table is not mergeable if it is one of the following:
3732 - A union (implementation restriction).
3733 - An aggregated query, or has HAVING, or has DISTINCT
3734 (A general aggregated query cannot be merged with a non-aggregated one).
3735 - A table-less query (unimportant special case).
3736 - A query with a LIMIT (limit applies to subquery, so the implementation
3737 strategy is to materialize this subquery, including row count constraint).
3738 - It has windows
3739 */
3740
is_mergeable() const3741 bool SELECT_LEX_UNIT::is_mergeable() const {
3742 if (is_union()) return false;
3743
3744 SELECT_LEX *const select = first_select();
3745 return !select->is_grouped() && !select->having_cond() &&
3746 !select->is_distinct() && select->table_list.elements > 0 &&
3747 !select->has_limit() && select->m_windows.elements == 0;
3748 }
3749
3750 /**
3751 True if heuristics suggest to merge this query expression.
3752
3753 A view/derived table is not suggested for merging if it contains subqueries
3754 in the SELECT list that depend on columns from itself.
3755 Merging such objects is possible, but we assume they are made derived
3756 tables because the user wants them to be materialized, for performance
3757 reasons.
3758
3759 One possible case is a derived table with dependent subqueries in the select
3760 list, used as the inner table of a left outer join. Such tables will always
3761 be read as many times as there are qualifying rows in the outer table,
3762 and the select list subqueries are evaluated for each row combination.
3763 The select list subqueries are evaluated the same number of times also with
3764 join buffering enabled, even though the table then only will be read once.
3765
3766 Another case is, a query that modifies variables: then try to preserve the
3767 original structure of the query. This is less likely to cause changes in
3768 variable assignment order.
3769 */
merge_heuristic(const LEX * lex) const3770 bool SELECT_LEX_UNIT::merge_heuristic(const LEX *lex) const {
3771 if (lex->set_var_list.elements != 0) return false;
3772
3773 SELECT_LEX *const select = first_select();
3774 Item *item;
3775 List_iterator<Item> it(select->fields_list);
3776 while ((item = it++)) {
3777 if (item->has_subquery() && !item->const_for_execution()) return false;
3778 }
3779 return true;
3780 }
3781
3782 /**
3783 Renumber contained select_lex objects.
3784
3785 @param lex Containing LEX object
3786 */
3787
renumber_selects(LEX * lex)3788 void SELECT_LEX_UNIT::renumber_selects(LEX *lex) {
3789 for (SELECT_LEX *select = first_select(); select;
3790 select = select->next_select())
3791 select->renumber(lex);
3792 if (fake_select_lex) fake_select_lex->renumber(lex);
3793 }
3794
3795 /**
3796 @brief Set the initial purpose of this TABLE_LIST object in the list of used
3797 tables.
3798
3799 We need to track this information on table-by-table basis, since when this
3800 table becomes an element of the pre-locked list, it's impossible to identify
3801 which SQL sub-statement it has been originally used in.
3802
3803 E.g.:
3804
3805 User request: SELECT * FROM t1 WHERE f1();
3806 FUNCTION f1(): DELETE FROM t2; RETURN 1;
3807 BEFORE DELETE trigger on t2: INSERT INTO t3 VALUES (old.a);
3808
3809 For this user request, the pre-locked list will contain t1, t2, t3
3810 table elements, each needed for different DML.
3811
3812 The trigger event map is updated to reflect INSERT, UPDATE, DELETE,
3813 REPLACE, LOAD DATA, CREATE TABLE .. SELECT, CREATE TABLE ..
3814 REPLACE SELECT statements, and additionally ON DUPLICATE KEY UPDATE
3815 clause.
3816 */
3817
set_trg_event_type_for_tables()3818 void LEX::set_trg_event_type_for_tables() {
3819 uint8 new_trg_event_map = 0;
3820
3821 /*
3822 Some auxiliary operations
3823 (e.g. GRANT processing) create TABLE_LIST instances outside
3824 the parser. Additionally, some commands (e.g. OPTIMIZE) change
3825 the lock type for a table only after parsing is done. Luckily,
3826 these do not fire triggers and do not need to pre-load them.
3827 For these TABLE_LISTs set_trg_event_type is never called, and
3828 trg_event_map is always empty. That means that the pre-locking
3829 algorithm will ignore triggers defined on these tables, if
3830 any, and the execution will either fail with an assert in
3831 sql_trigger.cc or with an error that a used table was not
3832 pre-locked, in case of a production build.
3833
3834 TODO: this usage pattern creates unnecessary module dependencies
3835 and should be rewritten to go through the parser.
3836 Table list instances created outside the parser in most cases
3837 refer to mysql.* system tables. It is not allowed to have
3838 a trigger on a system table, but keeping track of
3839 initialization provides extra safety in case this limitation
3840 is circumvented.
3841 */
3842
3843 switch (sql_command) {
3844 case SQLCOM_LOCK_TABLES:
3845 /*
3846 On a LOCK TABLE, all triggers must be pre-loaded for this TABLE_LIST
3847 when opening an associated TABLE.
3848 */
3849 new_trg_event_map =
3850 static_cast<uint8>(1 << static_cast<int>(TRG_EVENT_INSERT)) |
3851 static_cast<uint8>(1 << static_cast<int>(TRG_EVENT_UPDATE)) |
3852 static_cast<uint8>(1 << static_cast<int>(TRG_EVENT_DELETE));
3853 break;
3854 /*
3855 Basic INSERT. If there is an additional ON DUPLIATE KEY UPDATE
3856 clause, it will be handled later in this method.
3857 */
3858 case SQLCOM_INSERT: /* fall through */
3859 case SQLCOM_INSERT_SELECT:
3860 /*
3861 LOAD DATA ... INFILE is expected to fire BEFORE/AFTER INSERT
3862 triggers.
3863 If the statement also has REPLACE clause, it will be
3864 handled later in this method.
3865 */
3866 case SQLCOM_LOAD: /* fall through */
3867 /*
3868 REPLACE is semantically equivalent to INSERT. In case
3869 of a primary or unique key conflict, it deletes the old
3870 record and inserts a new one. So we also may need to
3871 fire ON DELETE triggers. This functionality is handled
3872 later in this method.
3873 */
3874 case SQLCOM_REPLACE: /* fall through */
3875 case SQLCOM_REPLACE_SELECT:
3876 /*
3877 CREATE TABLE ... SELECT defaults to INSERT if the table or
3878 view already exists. REPLACE option of CREATE TABLE ...
3879 REPLACE SELECT is handled later in this method.
3880 */
3881 case SQLCOM_CREATE_TABLE:
3882 new_trg_event_map |=
3883 static_cast<uint8>(1 << static_cast<int>(TRG_EVENT_INSERT));
3884 break;
3885 /* Basic update and multi-update */
3886 case SQLCOM_UPDATE: /* fall through */
3887 case SQLCOM_UPDATE_MULTI:
3888 new_trg_event_map |=
3889 static_cast<uint8>(1 << static_cast<int>(TRG_EVENT_UPDATE));
3890 break;
3891 /* Basic delete and multi-delete */
3892 case SQLCOM_DELETE: /* fall through */
3893 case SQLCOM_DELETE_MULTI:
3894 new_trg_event_map |=
3895 static_cast<uint8>(1 << static_cast<int>(TRG_EVENT_DELETE));
3896 break;
3897 default:
3898 break;
3899 }
3900
3901 switch (duplicates) {
3902 case DUP_UPDATE:
3903 new_trg_event_map |=
3904 static_cast<uint8>(1 << static_cast<int>(TRG_EVENT_UPDATE));
3905 break;
3906 case DUP_REPLACE:
3907 new_trg_event_map |=
3908 static_cast<uint8>(1 << static_cast<int>(TRG_EVENT_DELETE));
3909 break;
3910 case DUP_ERROR:
3911 default:
3912 break;
3913 }
3914
3915 /*
3916 Do not iterate over sub-selects, only the tables in the outermost
3917 SELECT_LEX can be modified, if any.
3918 */
3919 TABLE_LIST *tables = select_lex ? select_lex->get_table_list() : nullptr;
3920 while (tables) {
3921 /*
3922 This is a fast check to filter out statements that do
3923 not change data, or tables on the right side, in case of
3924 INSERT .. SELECT, CREATE TABLE .. SELECT and so on.
3925 Here we also filter out OPTIMIZE statement and non-updateable
3926 views, for which lock_type is TL_UNLOCK or TL_READ after
3927 parsing.
3928 */
3929 if (static_cast<int>(tables->lock_descriptor().type) >=
3930 static_cast<int>(TL_WRITE_ALLOW_WRITE))
3931 tables->trg_event_map = new_trg_event_map;
3932 tables = tables->next_local;
3933 }
3934 }
3935
3936 /*
3937 Unlink the first table from the global table list and the first table from
3938 outer select (lex->select_lex) local list
3939
3940 SYNOPSIS
3941 unlink_first_table()
3942 link_to_local Set to 1 if caller should link this table to local list
3943
3944 NOTES
3945 We assume that first tables in both lists is the same table or the local
3946 list is empty.
3947
3948 RETURN
3949 0 If 'query_tables' == 0
3950 unlinked table
3951 In this case link_to_local is set.
3952
3953 */
unlink_first_table(bool * link_to_local)3954 TABLE_LIST *LEX::unlink_first_table(bool *link_to_local) {
3955 TABLE_LIST *first;
3956 if ((first = query_tables)) {
3957 /*
3958 Exclude from global table list
3959 */
3960 if ((query_tables = query_tables->next_global))
3961 query_tables->prev_global = &query_tables;
3962 else
3963 query_tables_last = &query_tables;
3964 first->next_global = nullptr;
3965
3966 if (query_tables_own_last == &first->next_global)
3967 query_tables_own_last = &query_tables;
3968
3969 /*
3970 and from local list if it is not empty
3971 */
3972 if ((*link_to_local = select_lex->get_table_list() != nullptr)) {
3973 select_lex->context.table_list =
3974 select_lex->context.first_name_resolution_table = first->next_local;
3975 select_lex->table_list.first = first->next_local;
3976 select_lex->table_list.elements--; // safety
3977 first->next_local = nullptr;
3978 /*
3979 Ensure that the global list has the same first table as the local
3980 list.
3981 */
3982 first_lists_tables_same();
3983 }
3984 }
3985 return first;
3986 }
3987
3988 /*
3989 Bring first local table of first most outer select to first place in global
3990 table list
3991
3992 SYNOPSYS
3993 LEX::first_lists_tables_same()
3994
3995 NOTES
3996 In many cases (for example, usual INSERT/DELETE/...) the first table of
3997 main SELECT_LEX have special meaning => check that it is the first table
3998 in global list and re-link to be first in the global list if it is
3999 necessary. We need such re-linking only for queries with sub-queries in
4000 the select list, as only in this case tables of sub-queries will go to
4001 the global list first.
4002 */
4003
first_lists_tables_same()4004 void LEX::first_lists_tables_same() {
4005 TABLE_LIST *first_table = select_lex->get_table_list();
4006 if (query_tables != first_table && first_table != nullptr) {
4007 TABLE_LIST *next;
4008 if (query_tables_last == &first_table->next_global)
4009 query_tables_last = first_table->prev_global;
4010
4011 if (query_tables_own_last == &first_table->next_global)
4012 query_tables_own_last = first_table->prev_global;
4013
4014 if ((next = *first_table->prev_global = first_table->next_global))
4015 next->prev_global = first_table->prev_global;
4016 /* include in new place */
4017 first_table->next_global = query_tables;
4018 /*
4019 We are sure that query_tables is not 0, because first_table was not
4020 first table in the global list => we can use
4021 query_tables->prev_global without check of query_tables
4022 */
4023 query_tables->prev_global = &first_table->next_global;
4024 first_table->prev_global = &query_tables;
4025 query_tables = first_table;
4026 }
4027 }
4028
4029 /*
4030 Link table back that was unlinked with unlink_first_table()
4031
4032 SYNOPSIS
4033 link_first_table_back()
4034 link_to_local do we need link this table to local
4035
4036 RETURN
4037 global list
4038 */
4039
link_first_table_back(TABLE_LIST * first,bool link_to_local)4040 void LEX::link_first_table_back(TABLE_LIST *first, bool link_to_local) {
4041 if (first) {
4042 if ((first->next_global = query_tables))
4043 query_tables->prev_global = &first->next_global;
4044 else
4045 query_tables_last = &first->next_global;
4046
4047 if (query_tables_own_last == &query_tables)
4048 query_tables_own_last = &first->next_global;
4049
4050 query_tables = first;
4051
4052 if (link_to_local) {
4053 first->next_local = select_lex->table_list.first;
4054 select_lex->context.table_list = first;
4055 select_lex->table_list.first = first;
4056 select_lex->table_list.elements++; // safety
4057 }
4058 }
4059 }
4060
4061 /*
4062 cleanup lex for case when we open table by table for processing
4063
4064 SYNOPSIS
4065 LEX::cleanup_after_one_table_open()
4066
4067 NOTE
4068 This method is mostly responsible for cleaning up of selects lists and
4069 derived tables state. To rollback changes in Query_tables_list one has
4070 to call Query_tables_list::reset_query_tables_list(false).
4071 */
4072
cleanup_after_one_table_open()4073 void LEX::cleanup_after_one_table_open() {
4074 if (all_selects_list != select_lex) {
4075 /* cleunup underlying units (units of VIEW) */
4076 for (SELECT_LEX_UNIT *un = select_lex->first_inner_unit(); un;
4077 un = un->next_unit())
4078 un->cleanup(thd, true);
4079 /* reduce all selects list to default state */
4080 all_selects_list = select_lex;
4081 /* remove underlying units (units of VIEW) subtree */
4082 select_lex->cut_subtree();
4083 }
4084 }
4085
4086 /*
4087 Save current state of Query_tables_list for this LEX, and prepare it
4088 for processing of new statemnt.
4089
4090 SYNOPSIS
4091 reset_n_backup_query_tables_list()
4092 backup Pointer to Query_tables_list instance to be used for backup
4093 */
4094
reset_n_backup_query_tables_list(Query_tables_list * backup)4095 void LEX::reset_n_backup_query_tables_list(Query_tables_list *backup) {
4096 backup->set_query_tables_list(this);
4097 /*
4098 We have to perform full initialization here since otherwise we
4099 will damage backed up state.
4100 */
4101 this->reset_query_tables_list(true);
4102 }
4103
4104 /*
4105 Restore state of Query_tables_list for this LEX from backup.
4106
4107 SYNOPSIS
4108 restore_backup_query_tables_list()
4109 backup Pointer to Query_tables_list instance used for backup
4110 */
4111
restore_backup_query_tables_list(Query_tables_list * backup)4112 void LEX::restore_backup_query_tables_list(Query_tables_list *backup) {
4113 this->destroy_query_tables_list();
4114 this->set_query_tables_list(backup);
4115 }
4116
4117 /*
4118 Checks for usage of routines and/or tables in a parsed statement
4119
4120 SYNOPSIS
4121 LEX:table_or_sp_used()
4122
4123 RETURN
4124 false No routines and tables used
4125 true Either or both routines and tables are used.
4126 */
4127
table_or_sp_used()4128 bool LEX::table_or_sp_used() {
4129 DBUG_TRACE;
4130
4131 if ((sroutines != nullptr && !sroutines->empty()) || query_tables)
4132 return true;
4133
4134 return false;
4135 }
4136
4137 /**
4138 Locate an assignment to a user variable with a given name, within statement.
4139
4140 @param name Name of variable to search for
4141
4142 @returns true if variable is assigned to, false otherwise.
4143 */
4144
locate_var_assignment(const Name_string & name)4145 bool LEX::locate_var_assignment(const Name_string &name) {
4146 List_iterator<Item_func_set_user_var> li(set_var_list);
4147 Item_func_set_user_var *var;
4148 while ((var = li++)) {
4149 if (var->name.eq(name)) return true;
4150 }
4151 return false;
4152 }
4153
fix_prepare_information_for_order(THD * thd,SQL_I_List<ORDER> * list,Group_list_ptrs ** list_ptrs)4154 void SELECT_LEX::fix_prepare_information_for_order(
4155 THD *thd, SQL_I_List<ORDER> *list, Group_list_ptrs **list_ptrs) {
4156 Group_list_ptrs *p = *list_ptrs;
4157 if (!p) {
4158 void *mem = thd->stmt_arena->alloc(sizeof(Group_list_ptrs));
4159 *list_ptrs = p = new (mem) Group_list_ptrs(thd->stmt_arena->mem_root);
4160 }
4161 p->reserve(list->elements);
4162 for (ORDER *order = list->first; order; order = order->next)
4163 p->push_back(order);
4164 }
4165
4166 /*
4167 Saves the chain of ORDER::next in group_list and order_list, in
4168 case the list is modified by remove_const().
4169
4170 @param thd thread handler
4171 */
4172
fix_prepare_information(THD * thd)4173 void SELECT_LEX::fix_prepare_information(THD *thd) {
4174 if (!first_execution) return;
4175 first_execution = false;
4176 if (thd->stmt_arena->is_regular()) return;
4177 if (group_list.first)
4178 fix_prepare_information_for_order(thd, &group_list, &group_list_ptrs);
4179 if (order_list.first)
4180 fix_prepare_information_for_order(thd, &order_list, &order_list_ptrs);
4181 }
4182
4183 /*
4184 There are SELECT_LEX::add_table_to_list &
4185 SELECT_LEX::set_lock_for_tables are in sql_parse.cc
4186
4187 SELECT_LEX::print is in sql_select.cc
4188
4189 SELECT_LEX_UNIT::prepare, SELECT_LEX_UNIT::exec,
4190 SELECT_LEX_UNIT::cleanup, SELECT_LEX_UNIT::reinit_exec_mechanism,
4191 SELECT_LEX_UNIT::change_query_result
4192 are in sql_union.cc
4193 */
4194
type()4195 enum_explain_type SELECT_LEX::type() {
4196 if (master_unit()->fake_select_lex == this)
4197 return enum_explain_type::EXPLAIN_UNION_RESULT;
4198 else if (!master_unit()->outer_select() &&
4199 master_unit()->first_select() == this) {
4200 if (first_inner_unit() || next_select())
4201 return enum_explain_type::EXPLAIN_PRIMARY;
4202 else
4203 return enum_explain_type::EXPLAIN_SIMPLE;
4204 } else if (this == master_unit()->first_select()) {
4205 if (linkage == DERIVED_TABLE_TYPE)
4206 return enum_explain_type::EXPLAIN_DERIVED;
4207 else
4208 return enum_explain_type::EXPLAIN_SUBQUERY;
4209 } else
4210 return enum_explain_type::EXPLAIN_UNION;
4211 }
4212
4213 /**
4214 Add this query block below the specified query expression.
4215
4216 @param lex Containing LEX object
4217 @param outer Query expression that query block is added to.
4218
4219 @note that this query block can never have any underlying query expressions,
4220 hence it is not necessary to e.g. renumber those, like e.g.
4221 SELECT_LEX_UNIT::include_down() does.
4222 */
include_down(LEX * lex,SELECT_LEX_UNIT * outer)4223 void SELECT_LEX::include_down(LEX *lex, SELECT_LEX_UNIT *outer) {
4224 DBUG_ASSERT(slave == nullptr);
4225
4226 if ((next = outer->slave)) next->prev = &next;
4227 prev = &outer->slave;
4228 outer->slave = this;
4229 master = outer;
4230
4231 select_number = ++lex->select_number;
4232
4233 nest_level = outer_select() == nullptr ? 0 : outer_select()->nest_level + 1;
4234 }
4235
4236 /**
4237 Add this query block after the specified query block.
4238
4239 @param lex Containing LEX object
4240 @param before Query block that this object is added after.
4241 */
include_neighbour(LEX * lex,SELECT_LEX * before)4242 void SELECT_LEX::include_neighbour(LEX *lex, SELECT_LEX *before) {
4243 if ((next = before->next)) next->prev = &next;
4244 prev = &before->next;
4245 before->next = this;
4246 master = before->master;
4247
4248 select_number = ++lex->select_number;
4249 nest_level = before->nest_level;
4250 }
4251
4252 /**
4253 Include query block within the supplied unit.
4254
4255 Do not link the query block into the global chain of query blocks.
4256
4257 This function is exclusive for SELECT_LEX_UNIT::add_fake_select_lex() -
4258 use it with caution.
4259
4260 @param outer Query expression this node is included below.
4261 @param ref Handle to the caller's pointer to this node.
4262 */
include_standalone(SELECT_LEX_UNIT * outer,SELECT_LEX ** ref)4263 void SELECT_LEX::include_standalone(SELECT_LEX_UNIT *outer, SELECT_LEX **ref) {
4264 next = nullptr;
4265 prev = ref;
4266 master = outer;
4267 nest_level = master->first_select()->nest_level;
4268 }
4269
4270 /**
4271 Renumber select_lex object, and apply renumbering recursively to
4272 contained objects.
4273
4274 @param lex Containing LEX object
4275 */
renumber(LEX * lex)4276 void SELECT_LEX::renumber(LEX *lex) {
4277 select_number = ++lex->select_number;
4278
4279 nest_level = outer_select() == nullptr ? 0 : outer_select()->nest_level + 1;
4280
4281 for (SELECT_LEX_UNIT *u = first_inner_unit(); u; u = u->next_unit())
4282 u->renumber_selects(lex);
4283 }
4284
4285 /**
4286 Include query block into global list.
4287
4288 @param plink - Pointer to start of list
4289 */
include_in_global(SELECT_LEX ** plink)4290 void SELECT_LEX::include_in_global(SELECT_LEX **plink) {
4291 if ((link_next = *plink)) link_next->link_prev = &link_next;
4292 link_prev = plink;
4293 *plink = this;
4294 }
4295
4296 /**
4297 Include chain of query blocks into global list.
4298
4299 @param start - Pointer to start of list
4300 */
include_chain_in_global(SELECT_LEX ** start)4301 void SELECT_LEX::include_chain_in_global(SELECT_LEX **start) {
4302 SELECT_LEX *last_select;
4303 for (last_select = this; last_select->link_next != nullptr;
4304 last_select = last_select->link_next) {
4305 }
4306 last_select->link_next = *start;
4307 last_select->link_next->link_prev = &last_select->link_next;
4308 link_prev = start;
4309 *start = this;
4310 }
4311
4312 /**
4313 Helper function which handles the "ON conditions" part of
4314 SELECT_LEX::get_optimizable_conditions().
4315 @returns true if OOM
4316 */
get_optimizable_join_conditions(THD * thd,mem_root_deque<TABLE_LIST * > & join_list)4317 static bool get_optimizable_join_conditions(
4318 THD *thd, mem_root_deque<TABLE_LIST *> &join_list) {
4319 for (TABLE_LIST *table : join_list) {
4320 NESTED_JOIN *const nested_join = table->nested_join;
4321 if (nested_join &&
4322 get_optimizable_join_conditions(thd, nested_join->join_list))
4323 return true;
4324 Item *const jc = table->join_cond();
4325 if (jc && !thd->stmt_arena->is_regular()) {
4326 table->set_join_cond_optim(jc->copy_andor_structure(thd));
4327 if (!table->join_cond_optim()) return true;
4328 } else
4329 table->set_join_cond_optim(jc);
4330 }
4331 return false;
4332 }
4333
4334 /**
4335 Returns disposable copies of WHERE/HAVING/ON conditions.
4336
4337 This function returns a copy which can be thrashed during
4338 this execution of the statement. Only AND/OR items are trashable!
4339 If in conventional execution, no copy is created, the permanent clauses are
4340 returned instead, as trashing them is no problem.
4341
4342 @param thd thread handle
4343 @param[out] new_where copy of WHERE
4344 @param[out] new_having copy of HAVING (if passed pointer is not NULL)
4345
4346 Copies of join (ON) conditions are placed in TABLE_LIST::m_join_cond_optim.
4347
4348 @returns true if OOM
4349 */
get_optimizable_conditions(THD * thd,Item ** new_where,Item ** new_having)4350 bool SELECT_LEX::get_optimizable_conditions(THD *thd, Item **new_where,
4351 Item **new_having) {
4352 /*
4353 We want to guarantee that
4354 join->optimized is true => conditions are ready for reading.
4355 So if we are here, this should hold:
4356 */
4357 DBUG_ASSERT(!(join && join->is_optimized()));
4358 if (m_where_cond && !thd->stmt_arena->is_regular()) {
4359 *new_where = m_where_cond->copy_andor_structure(thd);
4360 if (!*new_where) return true;
4361 } else
4362 *new_where = m_where_cond;
4363 if (new_having) {
4364 if (m_having_cond && !thd->stmt_arena->is_regular()) {
4365 *new_having = m_having_cond->copy_andor_structure(thd);
4366 if (!*new_having) return true;
4367 } else
4368 *new_having = m_having_cond;
4369 }
4370 return get_optimizable_join_conditions(thd, top_join_list);
4371 }
4372
subquery_strategy(const THD * thd) const4373 Subquery_strategy SELECT_LEX::subquery_strategy(const THD *thd) const {
4374 if (m_windows.elements > 0)
4375 /*
4376 A window function is in the SELECT list.
4377 In-to-exists could not work: it would attach an equality like
4378 outer_expr = WF to either WHERE or HAVING; but a WF is not allowed in
4379 those clauses, and even if we allowed it, it would modify the result
4380 rows over which the WF is supposed to be calculated.
4381 So, subquery materialization is imposed. Grep for (and read) WL#10431.
4382 */
4383 return Subquery_strategy::SUBQ_MATERIALIZATION;
4384
4385 if (opt_hints_qb) {
4386 Subquery_strategy strategy = opt_hints_qb->subquery_strategy();
4387 if (strategy != Subquery_strategy::UNSPECIFIED) return strategy;
4388 }
4389
4390 // No SUBQUERY hint given, base possible strategies on optimizer_switch
4391 if (thd->optimizer_switch_flag(OPTIMIZER_SWITCH_MATERIALIZATION))
4392 return thd->optimizer_switch_flag(OPTIMIZER_SWITCH_SUBQ_MAT_COST_BASED)
4393 ? Subquery_strategy::CANDIDATE_FOR_IN2EXISTS_OR_MAT
4394 : Subquery_strategy::SUBQ_MATERIALIZATION;
4395
4396 return Subquery_strategy::SUBQ_EXISTS;
4397 }
4398
semijoin_enabled(const THD * thd) const4399 bool SELECT_LEX::semijoin_enabled(const THD *thd) const {
4400 return opt_hints_qb ? opt_hints_qb->semijoin_enabled(thd)
4401 : thd->optimizer_switch_flag(OPTIMIZER_SWITCH_SEMIJOIN);
4402 }
4403
update_semijoin_strategies(THD * thd)4404 void SELECT_LEX::update_semijoin_strategies(THD *thd) {
4405 uint sj_strategy_mask =
4406 OPTIMIZER_SWITCH_FIRSTMATCH | OPTIMIZER_SWITCH_LOOSE_SCAN |
4407 OPTIMIZER_SWITCH_MATERIALIZATION | OPTIMIZER_SWITCH_DUPSWEEDOUT;
4408
4409 uint opt_switches = thd->variables.optimizer_switch & sj_strategy_mask;
4410
4411 bool is_secondary_engine_optimization =
4412 parent_lex->m_sql_cmd != nullptr &&
4413 parent_lex->m_sql_cmd->using_secondary_storage_engine();
4414
4415 for (TABLE_LIST *sj_nest : sj_nests) {
4416 /*
4417 After semi-join transformation, original SELECT_LEX with hints is lost.
4418 Fetch hints from last table in semijoin nest, as join_list has the
4419 convention to list join operators' arguments in reverse order.
4420 */
4421 TABLE_LIST *table = sj_nest->nested_join->join_list.back();
4422 /*
4423 Do not respect opt_hints_qb for secondary engine optimization.
4424 Secondary storage engines may not support all strategies that are
4425 supported by the MySQL executor. Secondary engines should set their
4426 supported semi-join strategies in thd->variables.optimizer_switch and not
4427 respect optimizer hints or optimizer switches specified by the user.
4428 */
4429 sj_nest->nested_join->sj_enabled_strategies =
4430 (table->opt_hints_qb && !is_secondary_engine_optimization)
4431 ? table->opt_hints_qb->sj_enabled_strategies(opt_switches)
4432 : opt_switches;
4433 if (sj_nest->is_aj_nest()) {
4434 // only these are possible with NOT EXISTS/IN:
4435 sj_nest->nested_join->sj_enabled_strategies &=
4436 OPTIMIZER_SWITCH_FIRSTMATCH | OPTIMIZER_SWITCH_MATERIALIZATION |
4437 OPTIMIZER_SWITCH_DUPSWEEDOUT;
4438 }
4439 }
4440 }
4441
4442 /**
4443 Removes pointer to a sub query from sj_candidates array. Called from
4444 Item_subselect::clean_up_after_removal to clean the pointer
4445 to the subquery which is getting destroyed.
4446
4447 @param sub_query the sub_query whose pointer needs to be removed
4448 */
remove_semijoin_candidate(Item_exists_subselect * sub_query)4449 void SELECT_LEX::remove_semijoin_candidate(Item_exists_subselect *sub_query) {
4450 if (sj_candidates && !sj_candidates->empty())
4451 sj_candidates->erase_value(sub_query);
4452 }
4453
4454 /**
4455 Check if an option that can be used only for an outer-most query block is
4456 applicable to this query block.
4457
4458 @param lex LEX of current statement
4459 @param option option name to output within the error message
4460
4461 @returns false if valid, true if invalid, error is sent to client
4462 */
4463
validate_outermost_option(LEX * lex,const char * option) const4464 bool SELECT_LEX::validate_outermost_option(LEX *lex, const char *option) const {
4465 if (this != lex->select_lex) {
4466 my_error(ER_CANT_USE_OPTION_HERE, MYF(0), option);
4467 return true;
4468 }
4469 return false;
4470 }
4471
4472 /**
4473 Validate base options for a query block.
4474
4475 @param lex LEX of current statement
4476 @param options_arg base options for a SELECT statement.
4477
4478 @returns false if success, true if validation failed
4479
4480 These options are supported, per DML statement:
4481
4482 SELECT: SELECT_STRAIGHT_JOIN
4483 SELECT_HIGH_PRIORITY
4484 SELECT_DISTINCT
4485 SELECT_ALL
4486 SELECT_SMALL_RESULT
4487 SELECT_BIG_RESULT
4488 OPTION_BUFFER_RESULT
4489 OPTION_FOUND_ROWS
4490 OPTION_SELECT_FOR_SHOW
4491 DELETE: OPTION_QUICK
4492 LOW_PRIORITY
4493 INSERT: LOW_PRIORITY
4494 HIGH_PRIORITY
4495 UPDATE: LOW_PRIORITY
4496
4497 Note that validation is only performed for SELECT statements.
4498 */
4499
validate_base_options(LEX * lex,ulonglong options_arg) const4500 bool SELECT_LEX::validate_base_options(LEX *lex, ulonglong options_arg) const {
4501 DBUG_ASSERT(
4502 !(options_arg &
4503 ~(SELECT_STRAIGHT_JOIN | SELECT_HIGH_PRIORITY | SELECT_DISTINCT |
4504 SELECT_ALL | SELECT_SMALL_RESULT | SELECT_BIG_RESULT |
4505 OPTION_BUFFER_RESULT | OPTION_FOUND_ROWS | OPTION_SELECT_FOR_SHOW)));
4506
4507 if (options_arg & SELECT_DISTINCT && options_arg & SELECT_ALL) {
4508 my_error(ER_WRONG_USAGE, MYF(0), "ALL", "DISTINCT");
4509 return true;
4510 }
4511 if (options_arg & SELECT_HIGH_PRIORITY &&
4512 validate_outermost_option(lex, "HIGH_PRIORITY"))
4513 return true;
4514 if (options_arg & OPTION_BUFFER_RESULT &&
4515 validate_outermost_option(lex, "SQL_BUFFER_RESULT"))
4516 return true;
4517 if (options_arg & OPTION_FOUND_ROWS &&
4518 validate_outermost_option(lex, "SQL_CALC_FOUND_ROWS"))
4519 return true;
4520
4521 return false;
4522 }
4523
4524 /**
4525 Apply walk() processor to join conditions.
4526
4527 JOINs may be nested. Walk nested joins recursively to apply the
4528 processor.
4529 */
walk_join_condition(mem_root_deque<TABLE_LIST * > * tables,Item_processor processor,enum_walk walk,uchar * arg)4530 static bool walk_join_condition(mem_root_deque<TABLE_LIST *> *tables,
4531 Item_processor processor, enum_walk walk,
4532 uchar *arg) {
4533 for (const TABLE_LIST *table : *tables) {
4534 if (table->join_cond() && table->join_cond()->walk(processor, walk, arg))
4535 return true;
4536
4537 if (table->nested_join != nullptr &&
4538 walk_join_condition(&table->nested_join->join_list, processor, walk,
4539 arg))
4540 return true;
4541 }
4542 return false;
4543 }
4544
accumulate_used_tables(table_map map)4545 void SELECT_LEX_UNIT::accumulate_used_tables(table_map map) {
4546 DBUG_ASSERT(outer_select());
4547 if (item)
4548 item->accumulate_used_tables(map);
4549 else if (m_lateral_deps)
4550 m_lateral_deps |= map;
4551 }
4552
place() const4553 enum_parsing_context SELECT_LEX_UNIT::place() const {
4554 DBUG_ASSERT(outer_select());
4555 if (item != nullptr) return item->place();
4556 if (m_place_before_transform != CTX_NONE) return m_place_before_transform;
4557 return CTX_DERIVED;
4558 }
4559
walk(Item_processor processor,enum_walk walk,uchar * arg)4560 bool SELECT_LEX::walk(Item_processor processor, enum_walk walk, uchar *arg) {
4561 List_iterator<Item> li(fields_list);
4562 Item *item;
4563
4564 while ((item = li++)) {
4565 if (item->walk(processor, walk, arg)) return true;
4566 }
4567
4568 if (join_list != nullptr &&
4569 walk_join_condition(join_list, processor, walk, arg))
4570 return true;
4571
4572 if ((walk & enum_walk::SUBQUERY)) {
4573 /*
4574 for each leaf: if a materialized table, walk the unit
4575 */
4576 for (TABLE_LIST *tbl = leaf_tables; tbl; tbl = tbl->next_leaf) {
4577 if (!tbl->uses_materialization()) continue;
4578 if (tbl->is_derived()) {
4579 if (tbl->derived_unit()->walk(processor, walk, arg)) return true;
4580 } else if (tbl->is_table_function()) {
4581 if (tbl->table_function->walk(processor, walk, arg)) return true;
4582 }
4583 }
4584 }
4585
4586 // @todo: Roy thinks that we should always use where_cond.
4587 Item *const where_cond =
4588 (join && join->is_optimized()) ? join->where_cond : this->where_cond();
4589
4590 if (where_cond && where_cond->walk(processor, walk, arg)) return true;
4591
4592 for (auto order = group_list.first; order; order = order->next) {
4593 if ((*order->item)->walk(processor, walk, arg)) return true;
4594 }
4595
4596 if (having_cond() && having_cond()->walk(processor, walk, arg)) return true;
4597
4598 for (auto order = order_list.first; order; order = order->next) {
4599 if ((*order->item)->walk(processor, walk, arg)) return true;
4600 }
4601
4602 // walk windows' ORDER BY and PARTITION BY clauses.
4603 List_iterator<Window> liw(m_windows);
4604 for (Window *w = liw++; w != nullptr; w = liw++) {
4605 /*
4606 We use first_order_by() instead of order() because if a window
4607 references another window and they thus share the same ORDER BY,
4608 we want to walk that clause only once here
4609 (Same for partition as well)".
4610 */
4611 for (auto it : {w->first_partition_by(), w->first_order_by()}) {
4612 if (it != nullptr) {
4613 for (ORDER *o = it; o != nullptr; o = o->next) {
4614 if ((*o->item)->walk(processor, walk, arg)) return true;
4615 }
4616 }
4617 }
4618 }
4619 return false;
4620 }
4621
4622 /**
4623 Finds a (possibly unresolved) table reference in the from clause by name.
4624
4625 There is a hack in the parser which adorns table references with the current
4626 database. This function piggy-backs on that hack to find fully qualified
4627 table references without having to resolve the name.
4628
4629 @param ident The table name, may be qualified or unqualified.
4630
4631 @retval NULL If not found.
4632 */
find_table_by_name(const Table_ident * ident)4633 TABLE_LIST *SELECT_LEX::find_table_by_name(const Table_ident *ident) {
4634 LEX_CSTRING db_name = ident->db;
4635 LEX_CSTRING table_name = ident->table;
4636
4637 for (TABLE_LIST *table = table_list.first; table; table = table->next_local) {
4638 if ((db_name.length == 0 || strcmp(db_name.str, table->db) == 0) &&
4639 strcmp(table_name.str, table->alias) == 0)
4640 return table;
4641 }
4642 return nullptr;
4643 }
4644
merge(const Query_options & a,const Query_options & b)4645 bool Query_options::merge(const Query_options &a, const Query_options &b) {
4646 query_spec_options = a.query_spec_options | b.query_spec_options;
4647 return false;
4648 }
4649
save_to(Parse_context * pc)4650 bool Query_options::save_to(Parse_context *pc) {
4651 LEX *lex = pc->thd->lex;
4652 ulonglong options = query_spec_options;
4653 if (pc->select->validate_base_options(lex, options)) return true;
4654 pc->select->set_base_options(options);
4655
4656 return false;
4657 }
4658
accept(Select_lex_visitor * visitor)4659 bool LEX::accept(Select_lex_visitor *visitor) {
4660 return m_sql_cmd->accept(thd, visitor);
4661 }
4662
set_wild(LEX_STRING w)4663 bool LEX::set_wild(LEX_STRING w) {
4664 if (w.str == nullptr) {
4665 wild = nullptr;
4666 return false;
4667 }
4668 wild = new (thd->mem_root) String(w.str, w.length, system_charset_info);
4669 return wild == nullptr;
4670 }
4671
initialize()4672 void LEX_MASTER_INFO::initialize() {
4673 host = user = password = log_file_name = bind_addr = nullptr;
4674 network_namespace = nullptr;
4675 port = connect_retry = 0;
4676 heartbeat_period = 0;
4677 sql_delay = 0;
4678 pos = 0;
4679 server_id = retry_count = 0;
4680 gtid = nullptr;
4681 gtid_until_condition = UNTIL_SQL_BEFORE_GTIDS;
4682 view_id = nullptr;
4683 until_after_gaps = false;
4684 ssl = ssl_verify_server_cert = heartbeat_opt = repl_ignore_server_ids_opt =
4685 retry_count_opt = auto_position = port_opt = get_public_key =
4686 LEX_MI_UNCHANGED;
4687 ssl_key = ssl_cert = ssl_ca = ssl_capath = ssl_cipher = nullptr;
4688 ssl_crl = ssl_crlpath = nullptr;
4689 public_key_path = nullptr;
4690 tls_version = nullptr;
4691 tls_ciphersuites = UNSPECIFIED;
4692 tls_ciphersuites_string = nullptr;
4693 relay_log_name = nullptr;
4694 relay_log_pos = 0;
4695 repl_ignore_server_ids.clear();
4696 channel = nullptr;
4697 for_channel = false;
4698 compression_algorithm = nullptr;
4699 zstd_compression_level = 0;
4700 privilege_checks_none = false;
4701 privilege_checks_username = privilege_checks_hostname = nullptr;
4702 require_row_format = -1;
4703 require_table_primary_key_check = LEX_MI_PK_CHECK_UNCHANGED;
4704 }
4705
set_unspecified()4706 void LEX_MASTER_INFO::set_unspecified() {
4707 initialize();
4708 sql_delay = -1;
4709 }
4710
4711 uint binlog_unsafe_map[256];
4712
4713 #define UNSAFE(a, b, c) \
4714 { \
4715 DBUG_PRINT("unsafe_mixed_statement", \
4716 ("SETTING BASE VALUES: %s, %s, %02X\n", \
4717 LEX::stmt_accessed_table_string(a), \
4718 LEX::stmt_accessed_table_string(b), c)); \
4719 unsafe_mixed_statement(a, b, c); \
4720 }
4721
4722 /*
4723 Sets the combination given by "a" and "b" and automatically combinations
4724 given by other types of access, i.e. 2^(8 - 2), as unsafe.
4725
4726 It may happen a colision when automatically defining a combination as unsafe.
4727 For that reason, a combination has its unsafe condition redefined only when
4728 the new_condition is greater then the old. For instance,
4729
4730 . (BINLOG_DIRECT_ON & TRX_CACHE_NOT_EMPTY) is never overwritten by
4731 . (BINLOG_DIRECT_ON | BINLOG_DIRECT_OFF).
4732 */
unsafe_mixed_statement(LEX::enum_stmt_accessed_table a,LEX::enum_stmt_accessed_table b,uint condition)4733 static void unsafe_mixed_statement(LEX::enum_stmt_accessed_table a,
4734 LEX::enum_stmt_accessed_table b,
4735 uint condition) {
4736 int type = 0;
4737 int index = (1U << a) | (1U << b);
4738
4739 for (type = 0; type < 256; type++) {
4740 if ((type & index) == index) {
4741 binlog_unsafe_map[type] |= condition;
4742 }
4743 }
4744 }
4745
4746 /**
4747 Uses parse_tree to instantiate an Sql_cmd object and assigns it to the Lex.
4748
4749 @param parse_tree The parse tree.
4750
4751 @returns false on success, true on error.
4752 */
make_sql_cmd(Parse_tree_root * parse_tree)4753 bool LEX::make_sql_cmd(Parse_tree_root *parse_tree) {
4754 if (!will_contextualize) return false;
4755
4756 m_sql_cmd = parse_tree->make_cmd(thd);
4757 if (m_sql_cmd == nullptr) return true;
4758
4759 DBUG_ASSERT(m_sql_cmd->sql_command_code() == sql_command);
4760
4761 return false;
4762 }
4763
4764 /*
4765 The BINLOG_* AND TRX_CACHE_* values can be combined by using '&' or '|',
4766 which means that both conditions need to be satisfied or any of them is
4767 enough. For example,
4768
4769 . BINLOG_DIRECT_ON & TRX_CACHE_NOT_EMPTY means that the statment is
4770 unsafe when the option is on and trx-cache is not empty;
4771
4772 . BINLOG_DIRECT_ON | BINLOG_DIRECT_OFF means the statement is unsafe
4773 in all cases.
4774
4775 . TRX_CACHE_EMPTY | TRX_CACHE_NOT_EMPTY means the statement is unsafe
4776 in all cases. Similar as above.
4777 */
binlog_unsafe_map_init()4778 void binlog_unsafe_map_init() {
4779 memset((void *)binlog_unsafe_map, 0, sizeof(uint) * 256);
4780
4781 /*
4782 Classify a statement as unsafe when there is a mixed statement and an
4783 on-going transaction at any point of the execution if:
4784
4785 1. The mixed statement is about to update a transactional table and
4786 a non-transactional table.
4787
4788 2. The mixed statement is about to update a transactional table and
4789 read from a non-transactional table.
4790
4791 3. The mixed statement is about to update a non-transactional table
4792 and temporary transactional table.
4793
4794 4. The mixed statement is about to update a temporary transactional
4795 table and read from a non-transactional table.
4796
4797 5. The mixed statement is about to update a transactional table and
4798 a temporary non-transactional table.
4799
4800 6. The mixed statement is about to update a transactional table and
4801 read from a temporary non-transactional table.
4802
4803 7. The mixed statement is about to update a temporary transactional
4804 table and temporary non-transactional table.
4805
4806 8. The mixed statement is about to update a temporary transactional
4807 table and read from a temporary non-transactional table.
4808 After updating a transactional table if:
4809
4810 9. The mixed statement is about to update a non-transactional table
4811 and read from a transactional table.
4812
4813 10. The mixed statement is about to update a non-transactional table
4814 and read from a temporary transactional table.
4815
4816 11. The mixed statement is about to update a temporary non-transactional
4817 table and read from a transactional table.
4818
4819 12. The mixed statement is about to update a temporary non-transactional
4820 table and read from a temporary transactional table.
4821
4822 13. The mixed statement is about to update a temporary non-transactional
4823 table and read from a non-transactional table.
4824
4825 The reason for this is that locks acquired may not protected a concurrent
4826 transaction of interfering in the current execution and by consequence in
4827 the result.
4828 */
4829 /* Case 1. */
4830 UNSAFE(LEX::STMT_WRITES_TRANS_TABLE, LEX::STMT_WRITES_NON_TRANS_TABLE,
4831 BINLOG_DIRECT_ON | BINLOG_DIRECT_OFF);
4832 /* Case 2. */
4833 UNSAFE(LEX::STMT_WRITES_TRANS_TABLE, LEX::STMT_READS_NON_TRANS_TABLE,
4834 BINLOG_DIRECT_ON | BINLOG_DIRECT_OFF);
4835 /* Case 3. */
4836 UNSAFE(LEX::STMT_WRITES_NON_TRANS_TABLE, LEX::STMT_WRITES_TEMP_TRANS_TABLE,
4837 BINLOG_DIRECT_ON | BINLOG_DIRECT_OFF);
4838 /* Case 4. */
4839 UNSAFE(LEX::STMT_WRITES_TEMP_TRANS_TABLE, LEX::STMT_READS_NON_TRANS_TABLE,
4840 BINLOG_DIRECT_ON | BINLOG_DIRECT_OFF);
4841 /* Case 5. */
4842 UNSAFE(LEX::STMT_WRITES_TRANS_TABLE, LEX::STMT_WRITES_TEMP_NON_TRANS_TABLE,
4843 BINLOG_DIRECT_ON);
4844 /* Case 6. */
4845 UNSAFE(LEX::STMT_WRITES_TRANS_TABLE, LEX::STMT_READS_TEMP_NON_TRANS_TABLE,
4846 BINLOG_DIRECT_ON);
4847 /* Case 7. */
4848 UNSAFE(LEX::STMT_WRITES_TEMP_TRANS_TABLE,
4849 LEX::STMT_WRITES_TEMP_NON_TRANS_TABLE, BINLOG_DIRECT_ON);
4850 /* Case 8. */
4851 UNSAFE(LEX::STMT_WRITES_TEMP_TRANS_TABLE,
4852 LEX::STMT_READS_TEMP_NON_TRANS_TABLE, BINLOG_DIRECT_ON);
4853 /* Case 9. */
4854 UNSAFE(LEX::STMT_WRITES_NON_TRANS_TABLE, LEX::STMT_READS_TRANS_TABLE,
4855 (BINLOG_DIRECT_ON | BINLOG_DIRECT_OFF) & TRX_CACHE_NOT_EMPTY);
4856 /* Case 10 */
4857 UNSAFE(LEX::STMT_WRITES_NON_TRANS_TABLE, LEX::STMT_READS_TEMP_TRANS_TABLE,
4858 (BINLOG_DIRECT_ON | BINLOG_DIRECT_OFF) & TRX_CACHE_NOT_EMPTY);
4859 /* Case 11. */
4860 UNSAFE(LEX::STMT_WRITES_TEMP_NON_TRANS_TABLE, LEX::STMT_READS_TRANS_TABLE,
4861 BINLOG_DIRECT_ON & TRX_CACHE_NOT_EMPTY);
4862 /* Case 12. */
4863 UNSAFE(LEX::STMT_WRITES_TEMP_NON_TRANS_TABLE,
4864 LEX::STMT_READS_TEMP_TRANS_TABLE,
4865 BINLOG_DIRECT_ON & TRX_CACHE_NOT_EMPTY);
4866 /* Case 13. */
4867 UNSAFE(LEX::STMT_WRITES_TEMP_NON_TRANS_TABLE, LEX::STMT_READS_NON_TRANS_TABLE,
4868 BINLOG_DIRECT_OFF & TRX_CACHE_NOT_EMPTY);
4869 }
4870
set_secondary_engine_execution_context(Secondary_engine_execution_context * context)4871 void LEX::set_secondary_engine_execution_context(
4872 Secondary_engine_execution_context *context) {
4873 DBUG_ASSERT(m_secondary_engine_context == nullptr || context == nullptr);
4874 destroy(m_secondary_engine_context);
4875 m_secondary_engine_context = context;
4876 }
4877
cleanup()4878 void LEX_GRANT_AS::cleanup() {
4879 grant_as_used = false;
4880 role_type = role_enum::ROLE_NONE;
4881 user = nullptr;
4882 role_list = nullptr;
4883 }
4884
LEX_GRANT_AS()4885 LEX_GRANT_AS::LEX_GRANT_AS() { cleanup(); }
4886