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 lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_LIMIT);
682 pc->select->explicit_limit= 1;
683 }
684
685 if (is_multitable() && multi_delete_set_locks_and_link_aux_tables(pc))
686 return true;
687
688 if (opt_hints != NULL && opt_hints->contextualize(pc))
689 return true;
690
691 return false;
692 }
693
694
make_cmd(THD * thd)695 Sql_cmd *PT_delete::make_cmd(THD *thd)
696 {
697 Parse_context pc(thd, thd->lex->current_select());
698 if (contextualize(&pc))
699 return NULL;
700 if (is_multitable())
701 return new (thd->mem_root) Sql_cmd_delete_multi;
702 else
703 return new (thd->mem_root) Sql_cmd_delete;
704 }
705
706
contextualize(Parse_context * pc)707 bool PT_update::contextualize(Parse_context *pc)
708 {
709 if (super::contextualize(pc))
710 return true;
711
712 LEX *lex= pc->thd->lex;
713 lex->sql_command= SQLCOM_UPDATE;
714 lex->duplicates= DUP_ERROR;
715
716 lex->set_ignore(opt_ignore);
717 if (join_table_list->contextualize(pc))
718 return true;
719 pc->select->parsing_place= CTX_UPDATE_VALUE_LIST;
720
721 if (column_list->contextualize(pc) ||
722 value_list->contextualize(pc))
723 {
724 return true;
725 }
726 pc->select->item_list= column_list->value;
727
728 // Ensure we're resetting parsing context of the right select
729 assert(pc->select->parsing_place == CTX_UPDATE_VALUE_LIST);
730 pc->select->parsing_place= CTX_NONE;
731 if (lex->select_lex->table_list.elements > 1)
732 lex->sql_command= SQLCOM_UPDATE_MULTI;
733 else if (lex->select_lex->get_table_list()->is_derived())
734 {
735 /* it is single table update and it is update of derived table */
736 my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
737 lex->select_lex->get_table_list()->alias, "UPDATE");
738 return true;
739 }
740
741 /*
742 In case of multi-update setting write lock for all tables may
743 be too pessimistic. We will decrease lock level if possible in
744 mysql_multi_update().
745 */
746 pc->select->set_lock_for_tables(opt_low_priority);
747
748 if (opt_where_clause != NULL &&
749 opt_where_clause->itemize(pc, &opt_where_clause))
750 {
751 return true;
752 }
753 pc->select->set_where_cond(opt_where_clause);
754
755 if (opt_order_clause != NULL && opt_order_clause->contextualize(pc))
756 return true;
757
758 assert(pc->select->select_limit == NULL);
759 if (opt_limit_clause != NULL)
760 {
761 if (opt_limit_clause->itemize(pc, &opt_limit_clause))
762 return true;
763 pc->select->select_limit= opt_limit_clause;
764 lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_LIMIT);
765 pc->select->explicit_limit= 1;
766 }
767
768 if (opt_hints != NULL && opt_hints->contextualize(pc))
769 return true;
770
771 return false;
772 }
773
774
make_cmd(THD * thd)775 Sql_cmd *PT_update::make_cmd(THD *thd)
776 {
777 Parse_context pc(thd, thd->lex->current_select());
778 if (contextualize(&pc))
779 return NULL;
780 sql_cmd.update_value_list= value_list->value;
781 sql_cmd.sql_command= thd->lex->sql_command;
782
783 return &sql_cmd;
784 }
785
786
contextualize(Parse_context * pc)787 bool PT_create_select::contextualize(Parse_context *pc)
788 {
789 if (super::contextualize(pc))
790 return true;
791
792 LEX *lex= pc->thd->lex;
793 if (lex->sql_command == SQLCOM_INSERT)
794 lex->sql_command= SQLCOM_INSERT_SELECT;
795 else if (lex->sql_command == SQLCOM_REPLACE)
796 lex->sql_command= SQLCOM_REPLACE_SELECT;
797 /*
798 The following work only with the local list, the global list
799 is created correctly in this case
800 */
801 assert(pc->select == lex->current_select());
802 SQL_I_List<TABLE_LIST> save_list;
803 pc->select->table_list.save_and_clear(&save_list);
804 pc->select->parsing_place= CTX_SELECT_LIST;
805
806 if (options.query_spec_options & SELECT_HIGH_PRIORITY)
807 {
808 Yacc_state *yyps= &pc->thd->m_parser_state->m_yacc;
809 yyps->m_lock_type= TL_READ_HIGH_PRIORITY;
810 yyps->m_mdl_type= MDL_SHARED_READ;
811 }
812 if (options.save_to(pc))
813 return true;
814
815 if (item_list->contextualize(pc))
816 return true;
817
818 // Ensure we're resetting parsing context of the right select
819 assert(pc->select->parsing_place == CTX_SELECT_LIST);
820 pc->select->parsing_place= CTX_NONE;
821
822 if (table_expression->contextualize(pc))
823 return true;
824 /*
825 The following work only with the local list, the global list
826 is created correctly in this case
827 */
828 pc->select->table_list.push_front(&save_list);
829
830 if (opt_hints != NULL && opt_hints->contextualize(pc))
831 return true;
832
833 return false;
834 }
835
836
contextualize(Parse_context * pc)837 bool PT_insert_values_list::contextualize(Parse_context *pc)
838 {
839 if (super::contextualize(pc))
840 return true;
841 List_iterator<List_item> it1(many_values);
842 List<Item> *item_list;
843 while ((item_list= it1++))
844 {
845 List_iterator<Item> it2(*item_list);
846 Item *item;
847 while ((item= it2++))
848 {
849 if (item->itemize(pc, &item))
850 return true;
851 it2.replace(item);
852 }
853 }
854
855 return false;
856 }
857
858
contextualize(Parse_context * pc)859 bool PT_insert_query_expression::contextualize(Parse_context *pc)
860 {
861 if (super::contextualize(pc) || create_select->contextualize(pc))
862 return true;
863
864 pc->select->set_braces(braces);
865
866 if (opt_union != NULL && opt_union->contextualize(pc))
867 return true;
868
869 return false;
870 }
871
872
contextualize(Parse_context * pc)873 bool PT_insert::contextualize(Parse_context *pc)
874 {
875 if (super::contextualize(pc))
876 return true;
877
878 LEX *lex= pc->thd->lex;
879 if (is_replace)
880 {
881 lex->sql_command = SQLCOM_REPLACE;
882 lex->duplicates= DUP_REPLACE;
883 }
884 else
885 {
886 lex->sql_command= SQLCOM_INSERT;
887 lex->duplicates= DUP_ERROR;
888 lex->set_ignore(ignore);
889 }
890
891 Yacc_state *yyps= &pc->thd->m_parser_state->m_yacc;
892 if (!pc->select->add_table_to_list(pc->thd, table_ident, NULL,
893 TL_OPTION_UPDATING,
894 yyps->m_lock_type,
895 yyps->m_mdl_type,
896 NULL,
897 opt_use_partition))
898 {
899 return true;
900 }
901 pc->select->set_lock_for_tables(lock_option);
902
903 assert(lex->current_select() == lex->select_lex);
904
905 if (column_list->contextualize(pc))
906 return true;
907
908 if (has_select())
909 {
910 if (insert_query_expression->contextualize(pc))
911 return true;
912 lex->bulk_insert_row_cnt= 0;
913 }
914 else
915 {
916 if (row_value_list->contextualize(pc))
917 return true;
918 lex->bulk_insert_row_cnt= row_value_list->get_many_values().elements;
919 }
920
921
922 if (opt_on_duplicate_column_list != NULL)
923 {
924 assert(!is_replace);
925 assert(opt_on_duplicate_value_list != NULL &&
926 opt_on_duplicate_value_list->elements() ==
927 opt_on_duplicate_column_list->elements());
928
929 lex->duplicates= DUP_UPDATE;
930 TABLE_LIST *first_table= lex->select_lex->table_list.first;
931 /* Fix lock for ON DUPLICATE KEY UPDATE */
932 if (first_table->lock_type == TL_WRITE_CONCURRENT_DEFAULT)
933 first_table->lock_type= TL_WRITE_DEFAULT;
934
935 pc->select->parsing_place= CTX_UPDATE_VALUE_LIST;
936
937 if (opt_on_duplicate_column_list->contextualize(pc) ||
938 opt_on_duplicate_value_list->contextualize(pc))
939 return true;
940
941 // Ensure we're resetting parsing context of the right select
942 assert(pc->select->parsing_place == CTX_UPDATE_VALUE_LIST);
943 pc->select->parsing_place= CTX_NONE;
944 }
945
946 if (opt_hints != NULL && opt_hints->contextualize(pc))
947 return true;
948
949 return false;
950 }
951
952
make_cmd(THD * thd)953 Sql_cmd *PT_insert::make_cmd(THD *thd)
954 {
955 Parse_context pc(thd, thd->lex->current_select());
956 if (contextualize(&pc))
957 return NULL;
958
959 Sql_cmd_insert_base *sql_cmd;
960 if (has_select())
961 sql_cmd= new (thd->mem_root) Sql_cmd_insert_select(is_replace,
962 thd->lex->duplicates);
963 else
964 sql_cmd= new (thd->mem_root) Sql_cmd_insert(is_replace, thd->lex->duplicates);
965 if (sql_cmd == NULL)
966 return NULL;
967
968 if (!has_select())
969 sql_cmd->insert_many_values= row_value_list->get_many_values();
970
971 sql_cmd->insert_field_list= column_list->value;
972 if (opt_on_duplicate_column_list != NULL)
973 {
974 assert(!is_replace);
975 sql_cmd->insert_update_list= opt_on_duplicate_column_list->value;
976 sql_cmd->insert_value_list= opt_on_duplicate_value_list->value;
977 }
978
979 return sql_cmd;
980 }
981
982
983 /**
984 @brief
985 make_cmd for PT_alter_instance.
986 Contextualize parse tree node and return sql_cmd handle.
987
988 @params thd [in] Thread handle
989
990 @returns
991 sql_cmd Success
992 NULL Failure
993 */
994
make_cmd(THD * thd)995 Sql_cmd *PT_alter_instance::make_cmd(THD *thd)
996 {
997 Parse_context pc(thd, thd->lex->current_select());
998 if (contextualize(&pc))
999 return NULL;
1000 return &sql_cmd;
1001 }
1002
1003 /**
1004 @brief
1005 Prepare parse tree node and set required information
1006
1007 @params pc [in] Parser context
1008
1009 @returns
1010 false Success
1011 true Error
1012 */
1013
contextualize(Parse_context * pc)1014 bool PT_alter_instance::contextualize(Parse_context *pc)
1015 {
1016 if (super::contextualize(pc))
1017 return true;
1018
1019 LEX *lex= pc->thd->lex;
1020 lex->no_write_to_binlog= false;
1021 return false;
1022 }
1023