MySQL 8.0.39
Source Code Documentation
sql_update.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 2024, 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 designed to work 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 either included with
13 the program or referenced in the documentation.
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#ifndef SQL_UPDATE_INCLUDED
25#define SQL_UPDATE_INCLUDED
26
27#include <sys/types.h>
28
29#include "mem_root_deque.h"
30#include "my_alloc.h"
31#include "my_sqlcommand.h"
32#include "my_table_map.h"
33#include "sql/query_result.h" // Query_result_interceptor
34#include "sql/sql_cmd_dml.h" // Sql_cmd_dml
35#include "sql/sql_list.h"
36#include "sql/thr_malloc.h"
37
38class COPY_INFO;
39class Copy_field;
40class Item;
41class JOIN;
42class Query_block;
44class RowIterator;
46class THD;
48struct TABLE;
49class Table_ref;
50
52bool compare_records(const TABLE *table);
54 const Query_block *select,
55 const Table_ref *table_list);
56
58 /// Number of tables being updated
60 /// Pointer to list of updated tables, linked via 'next_local'
62 /// Array of references to temporary tables used to store cached updates
63 TABLE **tmp_tables{nullptr};
64 /// Array of parameter structs for creation of temporary tables
66 /// The first table in the join operation
67 TABLE *main_table{nullptr};
68 /**
69 In a multi-table update, this is equal to the first table in the join
70 operation (#main_table) if that table can be updated on the fly while
71 scanning it. It is `nullptr` otherwise.
72
73 @see safe_update_on_fly
74 */
76 /// List of pointers to fields to update, in order from statement
78 /// List of pointers to values to update with, in order from statement
80 /// The fields list decomposed into separate lists per table
82 /// The values list decomposed into separate lists per table
84 /**
85 List of tables referenced in the CHECK OPTION condition of
86 the updated view excluding the updated table.
87 */
89 /// ???
91
92 /**
93 Array of update operations, arranged per _updated_ table. For each
94 _updated_ table in the multiple table update statement, a COPY_INFO
95 pointer is present at the table's position in this array.
96
97 The array is allocated and populated during Query_result_update::prepare().
98 The position that each table is assigned is also given here and is stored
99 in the member TABLE::pos_in_table_list::shared. However, this is a publicly
100 available field, so nothing can be trusted about its integrity.
101
102 This member is NULL when the Query_result_update is created.
103
104 @see Query_result_update::prepare
105 */
107
108 public:
110 mem_root_deque<Item *> *value_list)
111 : Query_result_interceptor(), fields(field_list), values(value_list) {}
112 bool need_explain_interceptor() const override { return true; }
113 bool prepare(THD *thd, const mem_root_deque<Item *> &list,
114 Query_expression *u) override;
115 bool optimize();
116 bool start_execution(THD *thd) override;
117 bool send_data(THD *thd, const mem_root_deque<Item *> &items) override;
118 bool do_updates(THD *thd);
119 bool send_eof(THD *thd) override;
120 void cleanup() override;
122 THD *thd, MEM_ROOT *mem_root,
124};
125
126class Sql_cmd_update final : public Sql_cmd_dml {
127 public:
128 Sql_cmd_update(bool multitable_arg, mem_root_deque<Item *> *update_values)
129 : multitable(multitable_arg),
131 update_value_list(update_values) {}
132
135 }
136
137 bool is_single_table_plan() const override { return !multitable; }
138
139 protected:
140 bool precheck(THD *thd) override;
141 bool check_privileges(THD *thd) override;
142
143 bool prepare_inner(THD *thd) override;
144
145 bool execute_inner(THD *thd) override;
146
147 private:
148 bool update_single_table(THD *thd);
149
151
152 /// Bitmap of all tables which are to be updated
154
155 bool accept(THD *thd, Select_lex_visitor *visitor) override;
156
157 /// Convert list of fields to update to base table fields
159
160 public:
161 /// The original list of fields to update, used for privilege checking
163 /// The values used to update fields
165};
166
167/// Find out which of the target tables can be updated immediately while
168/// scanning. This is used by the old optimizer *after* the plan has been
169/// created. The hypergraph optimizer does not use this function, as it makes
170/// the decision about immediate update *during* planning, not after planning.
171///
172/// @param join The top-level JOIN object of the UPDATE statement.
173/// @param single_target True if the UPDATE statement has exactly
174/// one target table.
175/// @return Map of tables to update while scanning.
176table_map GetImmediateUpdateTable(const JOIN *join, bool single_target);
177
178/// Makes the TABLE and handler objects ready for being used in an UPDATE
179/// statement. Called at the beginning of each execution.
180///
181/// @param join The top-level JOIN object of the UPDATE operation.
182/// @return true on error.
184
185/// Creates an UpdateRowsIterator which updates the rows returned by the given
186/// "source" iterator.
188 THD *thd, MEM_ROOT *mem_root, JOIN *join,
190
191#endif /* SQL_UPDATE_INCLUDED */
This class encapsulates a data change operation.
Definition: sql_data_change.h:74
Definition: field.h:4651
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:853
Definition: sql_optimizer.h:133
Definition: sql_list.h:434
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1156
This class represents a query expression (one query block or several query blocks combined with UNION...
Definition: sql_lex.h:623
Definition: query_result.h:174
Definition: sql_update.h:57
Table_ref * update_tables
Pointer to list of updated tables, linked via 'next_local'.
Definition: sql_update.h:61
Copy_field * copy_field
???
Definition: sql_update.h:90
bool do_updates(THD *thd)
const mem_root_deque< Item * > * values
List of pointers to values to update with, in order from statement.
Definition: sql_update.h:79
TABLE * table_to_update
In a multi-table update, this is equal to the first table in the join operation (main_table) if that ...
Definition: sql_update.h:75
bool need_explain_interceptor() const override
Definition: sql_update.h:112
mem_root_deque< Item * > ** fields_for_table
The fields list decomposed into separate lists per table.
Definition: sql_update.h:81
Temp_table_param * tmp_table_param
Array of parameter structs for creation of temporary tables.
Definition: sql_update.h:65
unique_ptr_destroy_only< RowIterator > create_iterator(THD *thd, MEM_ROOT *mem_root, unique_ptr_destroy_only< RowIterator > source)
Definition: sql_update.cc:3068
bool prepare(THD *thd, const mem_root_deque< Item * > &list, Query_expression *u) override
Perform preparation specific to the query expression or DML statement.
Definition: sql_update.cc:1825
TABLE ** tmp_tables
Array of references to temporary tables used to store cached updates.
Definition: sql_update.h:63
bool start_execution(THD *thd) override
Prepare for execution of the query expression or DML statement.
Definition: sql_update.cc:2376
List< TABLE > unupdated_check_opt_tables
List of tables referenced in the CHECK OPTION condition of the updated view excluding the updated tab...
Definition: sql_update.h:88
bool send_data(THD *thd, const mem_root_deque< Item * > &items) override
Definition: sql_update.cc:2410
COPY_INFO ** update_operations
Array of update operations, arranged per updated table.
Definition: sql_update.h:106
const mem_root_deque< Item * > * fields
List of pointers to fields to update, in order from statement.
Definition: sql_update.h:77
bool send_eof(THD *thd) override
Definition: sql_update.cc:2940
void cleanup() override
Cleanup after this execution.
Definition: sql_update.cc:2382
TABLE * main_table
The first table in the join operation.
Definition: sql_update.h:67
bool optimize()
Set up data structures for multi-table UPDATE.
Definition: sql_update.cc:2182
uint update_table_count
Number of tables being updated.
Definition: sql_update.h:59
mem_root_deque< Item * > ** values_for_table
The values list decomposed into separate lists per table.
Definition: sql_update.h:83
Query_result_update(mem_root_deque< Item * > *field_list, mem_root_deque< Item * > *value_list)
Definition: sql_update.h:109
A context for reading through a single table using a chosen access method: index read,...
Definition: row_iterator.h:82
Abstract base class for traversing the Query_block tree.
Definition: select_lex_visitor.h:40
Definition: sql_cmd_dml.h:35
Definition: sql_update.h:126
bool precheck(THD *thd) override
Perform a precheck of table privileges for the specific operation.
Definition: sql_update.cc:128
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_update.h:133
bool prepare_inner(THD *thd) override
Perform the command-specific parts of DML command preparation, to be called from prepare()
Definition: sql_update.cc:1468
Sql_cmd_update(bool multitable_arg, mem_root_deque< Item * > *update_values)
Definition: sql_update.h:128
bool check_privileges(THD *thd) override
Check privileges on a prepared statement, called at start of execution of the statement.
Definition: sql_update.cc:194
mem_root_deque< Item * > * update_value_list
The values used to update fields.
Definition: sql_update.h:164
table_map tables_for_update
Bitmap of all tables which are to be updated.
Definition: sql_update.h:153
bool multitable
Definition: sql_update.h:150
bool execute_inner(THD *thd) override
The inner parts of query optimization and execution.
Definition: sql_update.cc:1802
mem_root_deque< Item * > original_fields
The original list of fields to update, used for privilege checking.
Definition: sql_update.h:162
bool update_single_table(THD *thd)
Perform an update to a set of rows in a single table.
Definition: sql_update.cc:367
bool is_single_table_plan() const override
Definition: sql_update.h:137
bool make_base_table_fields(THD *thd, mem_root_deque< Item * > *items)
Convert list of fields to update to base table fields.
Definition: sql_update.cc:311
bool accept(THD *thd, Select_lex_visitor *visitor) override
Definition: sql_update.cc:2961
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
Definition: table.h:2791
TABLE * table
Definition: table.h:3568
Object containing parameters used when creating and using temporary tables.
Definition: temp_table_param.h:95
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:110
static MEM_ROOT mem_root
Definition: client_plugin.cc:110
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
std::unique_ptr< T, Destroy_only< T > > unique_ptr_destroy_only
std::unique_ptr, but only destroying.
Definition: my_alloc.h:489
enum_sql_command
Definition: my_sqlcommand.h:46
@ SQLCOM_UPDATE
Definition: my_sqlcommand.h:51
@ SQLCOM_UPDATE_MULTI
Definition: my_sqlcommand.h:122
uint64_t table_map
Definition: my_table_map.h:30
thread_local MEM_ROOT ** THR_MALLOC
Definition: mysqld.cc:1560
std::string join(Container cont, const std::string &delim)
join elements of an container into a string separated by a delimiter.
Definition: string.h:151
std::list< T, ut::allocator< T > > list
Specialization of list which uses ut_allocator.
Definition: ut0new.h:2878
repeated Source source
Definition: replication_asynchronous_connection_failover.proto:42
bool records_are_comparable(const TABLE *table)
True if the table's input and output record buffers are comparable using compare_records(TABLE*).
Definition: sql_update.cc:237
table_map GetImmediateUpdateTable(const JOIN *join, bool single_target)
Find out which of the target tables can be updated immediately while scanning.
Definition: sql_update.cc:2993
unique_ptr_destroy_only< RowIterator > CreateUpdateRowsIterator(THD *thd, MEM_ROOT *mem_root, JOIN *join, unique_ptr_destroy_only< RowIterator > source)
Creates an UpdateRowsIterator which updates the rows returned by the given "source" iterator.
Definition: sql_update.cc:3061
bool FinalizeOptimizationForUpdate(JOIN *join)
Makes the TABLE and handler objects ready for being used in an UPDATE statement.
Definition: sql_update.cc:3035
bool compare_records(const TABLE *table)
Compares the input and output record buffers of the table to see if a row has changed.
Definition: sql_update.cc:255
bool should_switch_to_multi_table_if_subqueries(const THD *thd, const Query_block *select, const Table_ref *table_list)
Decides if a single-table UPDATE/DELETE statement should switch to the multi-table code path,...
Definition: sql_update.cc:1435
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: table.h:1399
unsigned int uint
Definition: uca9-dump.cc:75