1 /* Copyright (c) 2013, 2021, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23 #include "parse_tree_nodes.h"
24
25 #include "sp_instr.h" // sp_instr_set
26 #include "sql_delete.h" // Sql_cmd_delete_multi, Sql_cmd_delete
27 #include "sql_insert.h" // Sql_cmd_insert...
28
29
contextualize(Parse_context * pc)30 bool PT_group::contextualize(Parse_context *pc)
31 {
32 if (super::contextualize(pc))
33 return true;
34
35 SELECT_LEX *select= pc->select;
36 select->parsing_place= CTX_GROUP_BY;
37
38 if (group_list->contextualize(pc))
39 return true;
40 assert(select == pc->select);
41
42 select->group_list= group_list->value;
43
44 // Ensure we're resetting parsing place of the right select
45 assert(select->parsing_place == CTX_GROUP_BY);
46 select->parsing_place= CTX_NONE;
47
48 switch (olap) {
49 case UNSPECIFIED_OLAP_TYPE:
50 break;
51 case CUBE_TYPE:
52 if (select->linkage == GLOBAL_OPTIONS_TYPE)
53 {
54 my_error(ER_WRONG_USAGE, MYF(0), "WITH CUBE",
55 "global union parameters");
56 return true;
57 }
58 select->olap= CUBE_TYPE;
59 my_error(ER_NOT_SUPPORTED_YET, MYF(0), "CUBE");
60 return true;
61 case ROLLUP_TYPE:
62 if (select->linkage == GLOBAL_OPTIONS_TYPE)
63 {
64 my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP",
65 "global union parameters");
66 return true;
67 }
68 if (select->is_distinct())
69 {
70 // DISTINCT+ROLLUP does not work
71 my_error(ER_WRONG_USAGE, MYF(0), "WITH ROLLUP", "DISTINCT");
72 return true;
73 }
74 select->olap= ROLLUP_TYPE;
75 break;
76 default:
77 assert(!"unexpected OLAP type!");
78 }
79 return false;
80 }
81
82
contextualize(Parse_context * pc)83 bool PT_order::contextualize(Parse_context *pc)
84 {
85 if (super::contextualize(pc))
86 return true;
87
88 THD *thd= pc->thd;
89 LEX *lex= thd->lex;
90 SELECT_LEX_UNIT * const unit= pc->select->master_unit();
91 const bool braces= pc->select->braces;
92
93 if (pc->select->linkage != GLOBAL_OPTIONS_TYPE &&
94 pc->select->olap != UNSPECIFIED_OLAP_TYPE &&
95 (pc->select->linkage != UNION_TYPE || braces))
96 {
97 my_error(ER_WRONG_USAGE, MYF(0),
98 "CUBE/ROLLUP", "ORDER BY");
99 return true;
100 }
101 if (lex->sql_command != SQLCOM_ALTER_TABLE && !unit->fake_select_lex)
102 {
103 /*
104 A query of the of the form (SELECT ...) ORDER BY order_list is
105 executed in the same way as the query
106 SELECT ... ORDER BY order_list
107 unless the SELECT construct contains ORDER BY or LIMIT clauses.
108 Otherwise we create a fake SELECT_LEX if it has not been created
109 yet.
110 */
111 SELECT_LEX *first_sl= unit->first_select();
112 if (!unit->is_union() &&
113 (first_sl->order_list.elements || first_sl->select_limit))
114 {
115 if (unit->add_fake_select_lex(lex->thd))
116 return true;
117 pc->select= unit->fake_select_lex;
118 }
119 }
120
121 bool context_is_pushed= false;
122 if (pc->select->parsing_place == CTX_NONE)
123 {
124 if (unit->is_union() && !braces)
125 {
126 /*
127 At this point we don't know yet whether this is the last
128 select in union or not, but we move ORDER BY to
129 fake_select_lex anyway. If there would be one more select
130 in union mysql_new_select will correctly throw error.
131 */
132 pc->select= unit->fake_select_lex;
133 lex->push_context(&pc->select->context);
134 context_is_pushed= true;
135 }
136 /*
137 To preserve correct markup for the case
138 SELECT group_concat(... ORDER BY (subquery))
139 we do not change parsing_place if it's not NONE.
140 */
141 pc->select->parsing_place= CTX_ORDER_BY;
142 }
143
144 if (order_list->contextualize(pc))
145 return true;
146
147 if (context_is_pushed)
148 lex->pop_context();
149
150 pc->select->order_list= order_list->value;
151
152 // Reset parsing place only for ORDER BY
153 if (pc->select->parsing_place == CTX_ORDER_BY)
154 pc->select->parsing_place= CTX_NONE;
155 return false;
156 }
157
158
contextualize(Parse_context * pc)159 bool PT_table_factor_select_sym::contextualize(Parse_context *pc)
160 {
161 if (super::contextualize(pc))
162 return true;
163
164 LEX * const lex= pc->thd->lex;
165 SELECT_LEX *const outer_select= pc->select;
166
167 if (!outer_select->embedding || outer_select->end_nested_join(pc->thd))
168 {
169 /* we are not in parentheses */
170 error(pc, pos);
171 return true;
172 }
173 TABLE_LIST *embedding= outer_select->embedding;
174 const bool is_deeply_nested= embedding &&
175 !embedding->nested_join->join_list.elements;
176
177 lex->derived_tables|= DERIVED_SUBQUERY;
178 if (!lex->expr_allows_subselect ||
179 lex->sql_command == (int)SQLCOM_PURGE)
180 {
181 error(pc, pos);
182 return true;
183 }
184
185 outer_select->parsing_place= CTX_DERIVED;
186 if (outer_select->linkage == GLOBAL_OPTIONS_TYPE)
187 return true; // TODO: error(pc, pos)?
188
189 SELECT_LEX * const child= lex->new_query(pc->select);
190 if (child == NULL)
191 return true;
192
193 // Note that this current select is different from the one above
194 pc->select= child;
195 pc->select->linkage= DERIVED_TABLE_TYPE;
196 pc->select->parsing_place= CTX_SELECT_LIST;
197 outer_select->parsing_place= CTX_NONE;
198
199 Yacc_state *yyps= &pc->thd->m_parser_state->m_yacc;
200
201 if (select_options.query_spec_options & SELECT_HIGH_PRIORITY)
202 {
203 yyps->m_lock_type= TL_READ_HIGH_PRIORITY;
204 yyps->m_mdl_type= MDL_SHARED_READ;
205 }
206 if (select_options.save_to(pc))
207 return true;
208
209 if (select_item_list->contextualize(pc))
210 return true;
211 assert(child == pc->select);
212
213 // Ensure we're resetting parsing place of the right select
214 assert(child->parsing_place == CTX_SELECT_LIST);
215 child->parsing_place= CTX_NONE;
216
217 if (table_expression->contextualize(pc))
218 return true;
219
220 if (is_deeply_nested)
221 {
222 if (child->set_braces(1))
223 {
224 error(pc, pos);
225 return true;
226 }
227 }
228 if (outer_select->init_nested_join(pc->thd))
229 return true;
230
231 /* incomplete derived tables return NULL, we must be
232 nested in select_derived rule to be here. */
233 value= NULL;
234
235 if (opt_hint_list != NULL && opt_hint_list->contextualize(pc))
236 return true;
237
238 return false;
239 }
240
241
contextualize(Parse_context * pc)242 bool PT_table_factor_parenthesis::contextualize(Parse_context *pc)
243 {
244 if (super::contextualize(pc))
245 return true;
246
247 SELECT_LEX * const outer_select= pc->select;
248
249 if (select_derived_union->contextualize(pc))
250 return true;
251
252 /*
253 Use outer_select instead of pc->select as derived table has
254 altered value of pc->select.
255 */
256 if (!(select_derived_union->value || opt_table_alias) &&
257 outer_select->embedding &&
258 !outer_select->embedding->nested_join->join_list.elements)
259 {
260 /*
261 we have a derived table (select_derived_union->value == NULL)
262 but no alias,
263 Since we are nested in further parentheses so we
264 can pass NULL to the outer level parentheses
265 Permits parsing of "((((select ...))) as xyz)"
266 */
267 value= 0;
268 }
269 else if (!select_derived_union->value)
270 {
271 /*
272 Handle case of derived table, alias may be NULL if there
273 are no outer parentheses, add_table_to_list() will throw
274 error in this case
275 */
276 SELECT_LEX_UNIT *unit= pc->select->master_unit();
277 pc->select= outer_select;
278 Table_ident *ti= new Table_ident(unit);
279 if (ti == NULL)
280 return true;
281
282 value= pc->select->add_table_to_list(pc->thd,
283 ti, opt_table_alias, 0,
284 TL_READ, MDL_SHARED_READ);
285 if (value == NULL)
286 return true;
287 pc->select->add_joined_table(value);
288 pc->thd->lex->pop_context();
289 }
290 else if (opt_table_alias != NULL)
291 {
292 /*
293 Tables with or without joins within parentheses cannot
294 have aliases, and we ruled out derived tables above.
295 */
296 error(pc, alias_pos);
297 return true;
298 }
299 else
300 {
301 /* nested join: FROM (t1 JOIN t2 ...) */
302 value= select_derived_union->value;
303 }
304 return false;
305 }
306
307
contextualize(Parse_context * pc)308 bool PT_internal_variable_name_2d::contextualize(Parse_context *pc)
309 {
310 if (super::contextualize(pc))
311 return true;
312
313 THD *thd= pc->thd;
314 LEX *lex= thd->lex;
315 sp_head *sp= lex->sphead;
316
317 if (check_reserved_words(&ident1))
318 {
319 error(pc, pos);
320 return true;
321 }
322
323 if (sp && sp->m_type == SP_TYPE_TRIGGER &&
324 (!my_strcasecmp(system_charset_info, ident1.str, "NEW") ||
325 !my_strcasecmp(system_charset_info, ident1.str, "OLD")))
326 {
327 if (ident1.str[0]=='O' || ident1.str[0]=='o')
328 {
329 my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "OLD", "");
330 return true;
331 }
332 if (sp->m_trg_chistics.event == TRG_EVENT_DELETE)
333 {
334 my_error(ER_TRG_NO_SUCH_ROW_IN_TRG, MYF(0),
335 "NEW", "on DELETE");
336 return true;
337 }
338 if (sp->m_trg_chistics.action_time == TRG_ACTION_AFTER)
339 {
340 my_error(ER_TRG_CANT_CHANGE_ROW, MYF(0), "NEW", "after ");
341 return true;
342 }
343 /* This special combination will denote field of NEW row */
344 value.var= trg_new_row_fake_var;
345 value.base_name= ident2;
346 }
347 else
348 {
349 sys_var *tmp=find_sys_var(thd, ident2.str, ident2.length);
350 if (!tmp)
351 return true;
352 if (!tmp->is_struct())
353 my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), ident2.str);
354 value.var= tmp;
355 value.base_name= ident1;
356 }
357 return false;
358 }
359
360
contextualize(Parse_context * pc)361 bool PT_option_value_no_option_type_internal::contextualize(Parse_context *pc)
362 {
363 if (super::contextualize(pc) || name->contextualize(pc))
364 return true;
365
366 THD *thd= pc->thd;
367 LEX *lex= thd->lex;
368 sp_head *sp= lex->sphead;
369
370 if (sp)
371 sp->m_parser_data.push_expr_start_ptr(expr_pos.raw.start);
372
373 if (opt_expr != NULL && opt_expr->itemize(pc, &opt_expr))
374 return true;
375
376 const char *expr_start_ptr= NULL;
377
378 if (sp)
379 expr_start_ptr= sp->m_parser_data.pop_expr_start_ptr();
380
381 if (name->value.var == trg_new_row_fake_var)
382 {
383 assert(sp);
384 assert(expr_start_ptr);
385
386 /* We are parsing trigger and this is a trigger NEW-field. */
387
388 LEX_STRING expr_query= EMPTY_STR;
389
390 if (!opt_expr)
391 {
392 // This is: SET NEW.x = DEFAULT
393 // DEFAULT clause is not supported in triggers.
394
395 error(pc, expr_pos);
396 return true;
397 }
398 else if (lex->is_metadata_used())
399 {
400 expr_query= make_string(thd, expr_start_ptr, expr_pos.raw.end);
401
402 if (!expr_query.str)
403 return true;
404 }
405
406 if (set_trigger_new_row(pc, name->value.base_name, opt_expr, expr_query))
407 return true;
408 }
409 else if (name->value.var)
410 {
411 /* We're not parsing SP and this is a system variable. */
412
413 if (set_system_variable(thd, &name->value, lex->option_type, opt_expr))
414 return true;
415 }
416 else
417 {
418 assert(sp);
419 assert(expr_start_ptr);
420
421 /* We're parsing SP and this is an SP-variable. */
422
423 sp_pcontext *pctx= lex->get_sp_current_parsing_ctx();
424 sp_variable *spv= pctx->find_variable(name->value.base_name, false);
425
426 LEX_STRING expr_query= EMPTY_STR;
427
428 if (!opt_expr)
429 {
430 /*
431 This is: SET x = DEFAULT, where x is a SP-variable.
432 This is not supported.
433 */
434
435 error(pc, expr_pos);
436 return true;
437 }
438 else if (lex->is_metadata_used())
439 {
440 expr_query= make_string(thd, expr_start_ptr, expr_pos.raw.end);
441
442 if (!expr_query.str)
443 return true;
444 }
445
446 /*
447 NOTE: every SET-expression has its own LEX-object, even if it is
448 a multiple SET-statement, like:
449
450 SET spv1 = expr1, spv2 = expr2, ...
451
452 Every SET-expression has its own sp_instr_set. Thus, the
453 instruction owns the LEX-object, i.e. the instruction is
454 responsible for destruction of the LEX-object.
455 */
456
457 sp_instr_set *i=
458 new sp_instr_set(sp->instructions(), lex,
459 spv->offset, opt_expr, expr_query,
460 true); // The instruction owns its lex.
461
462 if (!i || sp->add_instr(thd, i))
463 return true;
464 }
465 return false;
466 }
467
468
contextualize(Parse_context * pc)469 bool PT_option_value_no_option_type_password::contextualize(Parse_context *pc)
470 {
471 if (super::contextualize(pc))
472 return true;
473
474 THD *thd= pc->thd;
475 LEX *lex= thd->lex;
476 sp_head *sp= lex->sphead;
477 sp_pcontext *pctx= lex->get_sp_current_parsing_ctx();
478 LEX_STRING pw= { C_STRING_WITH_LEN("password") };
479
480 if (pctx && pctx->find_variable(pw, false))
481 {
482 my_error(ER_SP_BAD_VAR_SHADOW, MYF(0), pw.str);
483 return true;
484 }
485
486 LEX_USER *user= (LEX_USER*) thd->alloc(sizeof(LEX_USER));
487
488 if (!user)
489 return true;
490
491 LEX_CSTRING sctx_user= thd->security_context()->user();
492 user->user.str= (char *) sctx_user.str;
493 user->user.length= sctx_user.length;
494
495 LEX_CSTRING sctx_priv_host= thd->security_context()->priv_host();
496 assert(sctx_priv_host.str);
497 user->host.str= (char *) sctx_priv_host.str;
498 user->host.length= sctx_priv_host.length;
499
500 set_var_password *var= new set_var_password(user,
501 const_cast<char *>(password));
502 if (var == NULL)
503 return true;
504
505 lex->var_list.push_back(var);
506 lex->autocommit= true;
507 lex->is_set_password_sql= true;
508
509 if (sp)
510 sp->m_flags|= sp_head::HAS_SET_AUTOCOMMIT_STMT;
511
512 if (sp_create_assignment_instr(pc->thd, expr_pos.raw.end))
513 return true;
514
515 return false;
516 }
517
518
519 /*
520 Given a table in the source list, find a correspondent table in the
521 table references list.
522
523 @param src Source table to match.
524 @param ref Table references list.
525
526 @remark The source table list (tables listed before the FROM clause
527 or tables listed in the FROM clause before the USING clause) may
528 contain table names or aliases that must match unambiguously one,
529 and only one, table in the target table list (table references list,
530 after FROM/USING clause).
531
532 @return Matching table, NULL otherwise.
533 */
534
multi_delete_table_match(TABLE_LIST * tbl,TABLE_LIST * tables)535 static TABLE_LIST *multi_delete_table_match(TABLE_LIST *tbl, TABLE_LIST *tables)
536 {
537 TABLE_LIST *match= NULL;
538 DBUG_ENTER("multi_delete_table_match");
539
540 for (TABLE_LIST *elem= tables; elem; elem= elem->next_local)
541 {
542 int cmp;
543
544 if (tbl->is_fqtn && elem->is_alias)
545 continue; /* no match */
546 if (tbl->is_fqtn && elem->is_fqtn)
547 cmp= my_strcasecmp(table_alias_charset, tbl->table_name, elem->table_name) ||
548 strcmp(tbl->db, elem->db);
549 else if (elem->is_alias)
550 cmp= my_strcasecmp(table_alias_charset, tbl->alias, elem->alias);
551 else
552 cmp= my_strcasecmp(table_alias_charset, tbl->table_name, elem->table_name) ||
553 strcmp(tbl->db, elem->db);
554
555 if (cmp)
556 continue;
557
558 if (match)
559 {
560 my_error(ER_NONUNIQ_TABLE, MYF(0), elem->alias);
561 DBUG_RETURN(NULL);
562 }
563
564 match= elem;
565 }
566
567 if (!match)
568 my_error(ER_UNKNOWN_TABLE, MYF(0), tbl->table_name, "MULTI DELETE");
569
570 DBUG_RETURN(match);
571 }
572
573
574 /**
575 Link tables in auxilary table list of multi-delete with corresponding
576 elements in main table list, and set proper locks for them.
577
578 @param pc Parse context
579
580 @retval
581 FALSE success
582 @retval
583 TRUE error
584 */
585
multi_delete_set_locks_and_link_aux_tables(Parse_context * pc)586 static bool multi_delete_set_locks_and_link_aux_tables(Parse_context *pc)
587 {
588 LEX * const lex= pc->thd->lex;
589 TABLE_LIST *tables= pc->select->table_list.first;
590 TABLE_LIST *target_tbl;
591 DBUG_ENTER("multi_delete_set_locks_and_link_aux_tables");
592
593 for (target_tbl= lex->auxiliary_table_list.first;
594 target_tbl; target_tbl= target_tbl->next_local)
595 {
596 /* All tables in aux_tables must be found in FROM PART */
597 TABLE_LIST *walk= multi_delete_table_match(target_tbl, tables);
598 if (!walk)
599 DBUG_RETURN(TRUE);
600 if (!walk->is_derived())
601 {
602 target_tbl->table_name= walk->table_name;
603 target_tbl->table_name_length= walk->table_name_length;
604 }
605 walk->updating= target_tbl->updating;
606 walk->lock_type= target_tbl->lock_type;
607 /* We can assume that tables to be deleted from are locked for write. */
608 assert(walk->lock_type >= TL_WRITE_ALLOW_WRITE);
609 walk->mdl_request.set_type(mdl_type_for_dml(walk->lock_type));
610 target_tbl->correspondent_table= walk; // Remember corresponding table
611 }
612 DBUG_RETURN(FALSE);
613 }
614
615
add_table(Parse_context * pc,Table_ident * table)616 bool PT_delete::add_table(Parse_context *pc, Table_ident *table)
617 {
618 const ulong table_opts= is_multitable() ? TL_OPTION_UPDATING | TL_OPTION_ALIAS
619 : TL_OPTION_UPDATING;
620 const thr_lock_type lock_type=
621 (opt_delete_options & DELETE_LOW_PRIORITY) ? TL_WRITE_LOW_PRIORITY
622 : TL_WRITE_DEFAULT;
623 const enum_mdl_type mdl_type=
624 (opt_delete_options & DELETE_LOW_PRIORITY) ? MDL_SHARED_WRITE_LOW_PRIO
625 : MDL_SHARED_WRITE;
626 return !pc->select->add_table_to_list(pc->thd, table, NULL, table_opts,
627 lock_type, mdl_type, NULL,
628 opt_use_partition);
629 }
630
contextualize(Parse_context * pc)631 bool PT_delete::contextualize(Parse_context *pc)
632 {
633 if (super::contextualize(pc))
634 return true;
635
636 LEX * const lex= pc->thd->lex;
637
638 lex->sql_command= is_multitable() ? SQLCOM_DELETE_MULTI : SQLCOM_DELETE;
639 lex->set_ignore(MY_TEST(opt_delete_options & DELETE_IGNORE));
640 lex->select_lex->init_order();
641 if (opt_delete_options & DELETE_QUICK)
642 pc->select->add_base_options(OPTION_QUICK);
643
644 if (is_multitable())
645 {
646 for (Table_ident **i= table_list.begin(); i != table_list.end(); ++i)
647 {
648 if (add_table(pc, *i))
649 return true;
650 }
651 }
652 else if (add_table(pc, table_ident))
653 return true;
654
655 if (is_multitable())
656 mysql_init_multi_delete(lex);
657 else
658 pc->select->top_join_list.push_back(pc->select->get_table_list());
659
660 Yacc_state * const yyps= &pc->thd->m_parser_state->m_yacc;
661 yyps->m_lock_type= TL_READ_DEFAULT;
662 yyps->m_mdl_type= MDL_SHARED_READ;
663
664 if (is_multitable() && join_table_list->contextualize(pc))
665 return true;
666
667 if (opt_where_clause != NULL &&
668 opt_where_clause->itemize(pc, &opt_where_clause))
669 return true;
670 pc->select->set_where_cond(opt_where_clause);
671
672 if (opt_order_clause != NULL && opt_order_clause->contextualize(pc))
673 return true;
674
675 assert(pc->select->select_limit == NULL);
676 if (opt_delete_limit_clause != NULL)
677 {
678 if (opt_delete_limit_clause->itemize(pc, &opt_delete_limit_clause))
679 return true;
680 pc->select->select_limit= opt_delete_limit_clause;
681 if (pc->select->select_limit->fixed &&
682 pc->select->select_limit->val_int() != 0)
683 {
684 lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_LIMIT);
685 }
686 pc->select->explicit_limit= 1;
687 }
688
689 if (is_multitable() && multi_delete_set_locks_and_link_aux_tables(pc))
690 return true;
691
692 if (opt_hints != NULL && opt_hints->contextualize(pc))
693 return true;
694
695 return false;
696 }
697
698
make_cmd(THD * thd)699 Sql_cmd *PT_delete::make_cmd(THD *thd)
700 {
701 Parse_context pc(thd, thd->lex->current_select());
702 if (contextualize(&pc))
703 return NULL;
704 if (is_multitable())
705 return new (thd->mem_root) Sql_cmd_delete_multi;
706 else
707 return new (thd->mem_root) Sql_cmd_delete;
708 }
709
710
contextualize(Parse_context * pc)711 bool PT_update::contextualize(Parse_context *pc)
712 {
713 if (super::contextualize(pc))
714 return true;
715
716 LEX *lex= pc->thd->lex;
717 lex->sql_command= SQLCOM_UPDATE;
718 lex->duplicates= DUP_ERROR;
719
720 lex->set_ignore(opt_ignore);
721 if (join_table_list->contextualize(pc))
722 return true;
723 pc->select->parsing_place= CTX_UPDATE_VALUE_LIST;
724
725 if (column_list->contextualize(pc) ||
726 value_list->contextualize(pc))
727 {
728 return true;
729 }
730 pc->select->item_list= column_list->value;
731
732 // Ensure we're resetting parsing context of the right select
733 assert(pc->select->parsing_place == CTX_UPDATE_VALUE_LIST);
734 pc->select->parsing_place= CTX_NONE;
735 if (lex->select_lex->table_list.elements > 1)
736 lex->sql_command= SQLCOM_UPDATE_MULTI;
737 else if (lex->select_lex->get_table_list()->is_derived())
738 {
739 /* it is single table update and it is update of derived table */
740 my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
741 lex->select_lex->get_table_list()->alias, "UPDATE");
742 return true;
743 }
744
745 /*
746 In case of multi-update setting write lock for all tables may
747 be too pessimistic. We will decrease lock level if possible in
748 mysql_multi_update().
749 */
750 pc->select->set_lock_for_tables(opt_low_priority);
751
752 if (opt_where_clause != NULL &&
753 opt_where_clause->itemize(pc, &opt_where_clause))
754 {
755 return true;
756 }
757 pc->select->set_where_cond(opt_where_clause);
758
759 if (opt_order_clause != NULL && opt_order_clause->contextualize(pc))
760 return true;
761
762 assert(pc->select->select_limit == NULL);
763 if (opt_limit_clause != NULL)
764 {
765 if (opt_limit_clause->itemize(pc, &opt_limit_clause))
766 return true;
767 pc->select->select_limit= opt_limit_clause;
768 lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_LIMIT);
769 pc->select->explicit_limit= 1;
770 }
771
772 if (opt_hints != NULL && opt_hints->contextualize(pc))
773 return true;
774
775 return false;
776 }
777
778
make_cmd(THD * thd)779 Sql_cmd *PT_update::make_cmd(THD *thd)
780 {
781 Parse_context pc(thd, thd->lex->current_select());
782 if (contextualize(&pc))
783 return NULL;
784 sql_cmd.update_value_list= value_list->value;
785 sql_cmd.sql_command= thd->lex->sql_command;
786
787 return &sql_cmd;
788 }
789
790
contextualize(Parse_context * pc)791 bool PT_create_select::contextualize(Parse_context *pc)
792 {
793 if (super::contextualize(pc))
794 return true;
795
796 LEX *lex= pc->thd->lex;
797 if (lex->sql_command == SQLCOM_INSERT)
798 lex->sql_command= SQLCOM_INSERT_SELECT;
799 else if (lex->sql_command == SQLCOM_REPLACE)
800 lex->sql_command= SQLCOM_REPLACE_SELECT;
801 /*
802 The following work only with the local list, the global list
803 is created correctly in this case
804 */
805 assert(pc->select == lex->current_select());
806 SQL_I_List<TABLE_LIST> save_list;
807 pc->select->table_list.save_and_clear(&save_list);
808 pc->select->parsing_place= CTX_SELECT_LIST;
809
810 if (options.query_spec_options & SELECT_HIGH_PRIORITY)
811 {
812 Yacc_state *yyps= &pc->thd->m_parser_state->m_yacc;
813 yyps->m_lock_type= TL_READ_HIGH_PRIORITY;
814 yyps->m_mdl_type= MDL_SHARED_READ;
815 }
816 if (options.save_to(pc))
817 return true;
818
819 if (item_list->contextualize(pc))
820 return true;
821
822 // Ensure we're resetting parsing context of the right select
823 assert(pc->select->parsing_place == CTX_SELECT_LIST);
824 pc->select->parsing_place= CTX_NONE;
825
826 if (table_expression->contextualize(pc))
827 return true;
828 /*
829 The following work only with the local list, the global list
830 is created correctly in this case
831 */
832 pc->select->table_list.push_front(&save_list);
833
834 if (opt_hints != NULL && opt_hints->contextualize(pc))
835 return true;
836
837 return false;
838 }
839
840
contextualize(Parse_context * pc)841 bool PT_insert_values_list::contextualize(Parse_context *pc)
842 {
843 if (super::contextualize(pc))
844 return true;
845 List_iterator<List_item> it1(many_values);
846 List<Item> *item_list;
847 while ((item_list= it1++))
848 {
849 List_iterator<Item> it2(*item_list);
850 Item *item;
851 while ((item= it2++))
852 {
853 if (item->itemize(pc, &item))
854 return true;
855 it2.replace(item);
856 }
857 }
858
859 return false;
860 }
861
862
contextualize(Parse_context * pc)863 bool PT_insert_query_expression::contextualize(Parse_context *pc)
864 {
865 if (super::contextualize(pc) || create_select->contextualize(pc))
866 return true;
867
868 pc->select->set_braces(braces);
869
870 if (opt_union != NULL && opt_union->contextualize(pc))
871 return true;
872
873 return false;
874 }
875
876
contextualize(Parse_context * pc)877 bool PT_insert::contextualize(Parse_context *pc)
878 {
879 if (super::contextualize(pc))
880 return true;
881
882 LEX *lex= pc->thd->lex;
883 if (is_replace)
884 {
885 lex->sql_command = SQLCOM_REPLACE;
886 lex->duplicates= DUP_REPLACE;
887 }
888 else
889 {
890 lex->sql_command= SQLCOM_INSERT;
891 lex->duplicates= DUP_ERROR;
892 lex->set_ignore(ignore);
893 }
894
895 Yacc_state *yyps= &pc->thd->m_parser_state->m_yacc;
896 if (!pc->select->add_table_to_list(pc->thd, table_ident, NULL,
897 TL_OPTION_UPDATING,
898 yyps->m_lock_type,
899 yyps->m_mdl_type,
900 NULL,
901 opt_use_partition))
902 {
903 return true;
904 }
905 pc->select->set_lock_for_tables(lock_option);
906
907 assert(lex->current_select() == lex->select_lex);
908
909 if (column_list->contextualize(pc))
910 return true;
911
912 if (has_select())
913 {
914 if (insert_query_expression->contextualize(pc))
915 return true;
916 lex->bulk_insert_row_cnt= 0;
917 }
918 else
919 {
920 if (row_value_list->contextualize(pc))
921 return true;
922 lex->bulk_insert_row_cnt= row_value_list->get_many_values().elements;
923 }
924
925
926 if (opt_on_duplicate_column_list != NULL)
927 {
928 assert(!is_replace);
929 assert(opt_on_duplicate_value_list != NULL &&
930 opt_on_duplicate_value_list->elements() ==
931 opt_on_duplicate_column_list->elements());
932
933 lex->duplicates= DUP_UPDATE;
934 TABLE_LIST *first_table= lex->select_lex->table_list.first;
935 /* Fix lock for ON DUPLICATE KEY UPDATE */
936 if (first_table->lock_type == TL_WRITE_CONCURRENT_DEFAULT)
937 first_table->lock_type= TL_WRITE_DEFAULT;
938
939 pc->select->parsing_place= CTX_UPDATE_VALUE_LIST;
940
941 if (opt_on_duplicate_column_list->contextualize(pc) ||
942 opt_on_duplicate_value_list->contextualize(pc))
943 return true;
944
945 // Ensure we're resetting parsing context of the right select
946 assert(pc->select->parsing_place == CTX_UPDATE_VALUE_LIST);
947 pc->select->parsing_place= CTX_NONE;
948 }
949
950 if (opt_hints != NULL && opt_hints->contextualize(pc))
951 return true;
952
953 return false;
954 }
955
956
make_cmd(THD * thd)957 Sql_cmd *PT_insert::make_cmd(THD *thd)
958 {
959 Parse_context pc(thd, thd->lex->current_select());
960 if (contextualize(&pc))
961 return NULL;
962
963 Sql_cmd_insert_base *sql_cmd;
964 if (has_select())
965 sql_cmd= new (thd->mem_root) Sql_cmd_insert_select(is_replace,
966 thd->lex->duplicates);
967 else
968 sql_cmd= new (thd->mem_root) Sql_cmd_insert(is_replace, thd->lex->duplicates);
969 if (sql_cmd == NULL)
970 return NULL;
971
972 if (!has_select())
973 sql_cmd->insert_many_values= row_value_list->get_many_values();
974
975 sql_cmd->insert_field_list= column_list->value;
976 if (opt_on_duplicate_column_list != NULL)
977 {
978 assert(!is_replace);
979 sql_cmd->insert_update_list= opt_on_duplicate_column_list->value;
980 sql_cmd->insert_value_list= opt_on_duplicate_value_list->value;
981 }
982
983 return sql_cmd;
984 }
985
986
987 /**
988 @brief
989 make_cmd for PT_alter_instance.
990 Contextualize parse tree node and return sql_cmd handle.
991
992 @params thd [in] Thread handle
993
994 @returns
995 sql_cmd Success
996 NULL Failure
997 */
998
make_cmd(THD * thd)999 Sql_cmd *PT_alter_instance::make_cmd(THD *thd)
1000 {
1001 Parse_context pc(thd, thd->lex->current_select());
1002 if (contextualize(&pc))
1003 return NULL;
1004 return &sql_cmd;
1005 }
1006
1007 /**
1008 @brief
1009 Prepare parse tree node and set required information
1010
1011 @params pc [in] Parser context
1012
1013 @returns
1014 false Success
1015 true Error
1016 */
1017
contextualize(Parse_context * pc)1018 bool PT_alter_instance::contextualize(Parse_context *pc)
1019 {
1020 if (super::contextualize(pc))
1021 return true;
1022
1023 LEX *lex= pc->thd->lex;
1024 lex->no_write_to_binlog= false;
1025 return false;
1026 }
1027