MySQL 26.7.0
Source Code Documentation
sql_load.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 2026, 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_LOAD_INCLUDED
25#define SQL_LOAD_INCLUDED
26
27#include <assert.h>
28#include <sys/types.h>
29
30#include "lex_string.h"
31
32#include "my_compress.h"
33#include "my_sqlcommand.h"
36#include "sql/current_thd.h"
37#include "sql/sql_cmd.h" /* Sql_cmd */
38#include "sql/sql_data_change.h" /* enum_duplicates */
39#include "sql/sql_exchange.h" /* sql_exchange */
40#include "sql/sql_lex.h"
41#include "sql/sql_list.h"
42#include "sql_string.h"
43
44#include <optional>
45#include <utility>
46
47class Item;
49class READ_INFO;
50struct TABLE;
51class THD;
52class Table_ref;
53
55
57 public:
59
60 Load_data_partition_spec(const LEX_STRING &name, ulong first_file,
61 ulong last_file)
62 : m_name(name), m_files_range(std::make_pair(first_file, last_file)) {}
63
64 const LEX_STRING &name() const { return m_name; }
65 const std::optional<std::pair<ulong, ulong>> &files_range() const {
66 return m_files_range;
67 }
68
69 private:
71 std::optional<std::pair<ulong, ulong>> m_files_range;
72};
73
75 public:
78
79 bool push_back_name(THD *thd, const LEX_STRING &name);
80 bool push_back_range(THD *thd, const LEX_STRING &name, ulong first_file,
81 ulong last_file);
82
85 return m_partitions;
86 }
88
89 private:
93};
94
95class Sql_cmd_load_table final : public Sql_cmd {
96 public:
98 enum_filetype filetype, bool is_local_file, enum_source_type source_type,
99 const LEX_STRING &filename, ulong file_count, bool in_key_order,
100 On_duplicate on_duplicate, Table_ident *table,
101 Load_data_partition_list *opt_partitions, const CHARSET_INFO *opt_charset,
102 LEX_CSTRING compression_algorithm, String *opt_xml_rows_identified_by,
103 const Field_separators *field_separators,
104 const Line_separators *line_separators, ulong skip_lines,
105 mem_root_deque<Item *> *opt_fields_or_vars,
106 mem_root_deque<Item *> *opt_set_fields,
107 mem_root_deque<Item *> *opt_set_exprs, List<String> *opt_set_expr_strings,
108 ulong concurrency, ulonglong memory_size, bool is_bulk_operation)
110 m_is_local_file(is_local_file),
111 m_bulk_source(source_type),
112 m_file_count(file_count),
113 m_ordered_data(in_key_order),
115 m_memory_size(memory_size),
116 m_on_duplicate(on_duplicate),
117 m_table(table),
118 m_opt_partitions(opt_partitions),
122 m_opt_set_expr_strings(opt_set_expr_strings),
123 m_is_bulk_operation(is_bulk_operation),
124 m_compression_algorithm_string(compression_algorithm) {
125 if (opt_fields_or_vars)
126 m_opt_fields_or_vars = std::move(*opt_fields_or_vars);
127 assert((opt_set_fields == nullptr) ^ (opt_set_exprs != nullptr));
128 if (opt_set_fields) {
129 assert(opt_set_fields->size() == opt_set_exprs->size());
130 m_opt_set_fields = std::move(*opt_set_fields);
131 m_opt_set_exprs = std::move(*opt_set_exprs);
132 }
133
134 m_exchange.file_info.cs = opt_charset;
135
136 if (opt_xml_rows_identified_by != nullptr)
137 m_exchange.line.line_term = opt_xml_rows_identified_by;
138
139 m_exchange.field.merge_field_separators(field_separators);
140 m_exchange.line.merge_line_separators(line_separators);
142 m_exchange.skip_lines = skip_lines;
143 }
144
145 enum_sql_command sql_command_code() const override { return SQLCOM_LOAD; }
146
147 bool execute(THD *thd) override;
148
149 bool is_bulk_load() const override { return m_is_bulk_operation; }
150
151 public:
153 const bool m_is_local_file;
154
155 private:
156 /// Source for bulk data.
158
159 /// Number of input files.
161
162 /// true if input data is in order of Primary Key.
164
165 /// Number of concurrent loaders to use
167
168 /// Maximum memory size to be used in bytes
170
171 /// Loading non empty table
173
174 public:
181
182 /**
183 A list of strings is maintained to store the SET clause command user strings
184 which are specified in load data operation. This list will be used
185 during the reconstruction of "load data" statement at the time of writing
186 to binary log.
187 */
189
190 private:
191 bool execute_inner(THD *thd, enum enum_duplicates handle_duplicates);
192
193 bool execute_bulk(THD *thd);
194
196
197 bool truncate_table_for_bulk_load(THD *thd, Table_ref *const table_ref,
199
201
202 bool validate_table_for_bulk_load(THD *thd, Table_ref *const table_ref,
204
206 THD *thd, const std::string &schema_name,
207 const std::string &old_table_name, const std::string &new_table_name);
208
209 bool duplicate_table_for_bulk_load(THD *thd, std::string &temp_name,
210 const std::string &schema_name,
211 Table_ref *new_table_ref);
212
213 bool bulk_driver_service(THD *thd, const TABLE *sql_table,
214 const TABLE *duplicate_table,
216 size_t &affected_rows);
217
218 bool run_bulk_driver_for_target(THD *thd, Table_ref *table_ref,
219 Table_ref *new_table_ref,
220 bool has_duplicate_table,
221 const std::string *partition_name_ptr,
223 size_t &affected_rows);
224
225 bool read_fixed_length(THD *thd, COPY_INFO &info, Table_ref *table_list,
226 READ_INFO &read_info, ulong skip_lines);
227
228 bool read_sep_field(THD *thd, COPY_INFO &info, Table_ref *table_list,
229 READ_INFO &read_info, const String &enclosed,
230 ulong skip_lines);
231
232 bool read_xml_field(THD *thd, COPY_INFO &info, Table_ref *table_list,
233 READ_INFO &read_info, ulong skip_lines);
234
236 THD *thd, const char *db, const char *table_name, bool is_concurrent,
237 enum enum_duplicates duplicates, bool transactional_table, int errocode);
238
239 private:
240 /// true if BULK LOAD.
243};
244
245#endif /* SQL_LOAD_INCLUDED */
This service provides interface for loading data in bulk from CSV files.
Bulk_source
Bulk loader source.
Definition: bulk_load_service.h:61
This class encapsulates a data change operation.
Definition: sql_data_change.h:74
Helper for the sql_exchange class.
Definition: sql_exchange.h:84
void merge_field_separators(const Field_separators *field_sep)
Definition: sql_exchange.h:98
const CHARSET_INFO * cs
Definition: sql_exchange.h:159
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:929
Helper for the sql_exchange class.
Definition: sql_exchange.h:65
void merge_line_separators(const Line_separators *line_sep)
Definition: sql_exchange.h:70
const String * line_term
Definition: sql_exchange.h:67
Definition: sql_list.h:494
Definition: sql_load.h:74
Load_data_partition_list(THD *thd, Load_data_partition_mode mode)
Definition: sql_load.h:76
Load_data_partition_mode mode() const
Definition: sql_load.h:83
List< String > * partition_names()
Definition: sql_load.h:87
const mem_root_deque< Load_data_partition_spec * > & partitions() const
Definition: sql_load.h:84
mem_root_deque< Load_data_partition_spec * > m_partitions
Definition: sql_load.h:91
List< String > m_partition_names
Definition: sql_load.h:92
bool push_back_range(THD *thd, const LEX_STRING &name, ulong first_file, ulong last_file)
Definition: parse_tree_nodes.cc:175
bool push_back_name(THD *thd, const LEX_STRING &name)
Definition: parse_tree_nodes.cc:163
Load_data_partition_mode m_mode
Definition: sql_load.h:90
Definition: sql_load.h:56
Load_data_partition_spec(const LEX_STRING &name)
Definition: sql_load.h:58
Load_data_partition_spec(const LEX_STRING &name, ulong first_file, ulong last_file)
Definition: sql_load.h:60
LEX_STRING m_name
Definition: sql_load.h:70
std::optional< std::pair< ulong, ulong > > m_files_range
Definition: sql_load.h:71
const std::optional< std::pair< ulong, ulong > > & files_range() const
Definition: sql_load.h:65
const LEX_STRING & name() const
Definition: sql_load.h:64
Definition: sql_load.cc:168
Definition: sql_load.h:95
sql_exchange m_exchange
Definition: sql_load.h:152
bool validate_check_constraints_for_bulk_load(THD *thd, TABLE *table)
Definition: sql_load.cc:446
const On_duplicate m_on_duplicate
Definition: sql_load.h:175
bool check_bulk_load_parameters(THD *thd)
Check bulk load parameters for limits.
Definition: sql_load.cc:275
bool is_bulk_load() const override
Definition: sql_load.h:149
Load_data_partition_list *const m_opt_partitions
Definition: sql_load.h:177
mem_root_deque< Item * > m_opt_set_fields
Definition: sql_load.h:179
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_load.h:145
bool read_sep_field(THD *thd, COPY_INFO &info, Table_ref *table_list, READ_INFO &read_info, const String &enclosed, ulong skip_lines)
Read rows in delimiter-separated formats.
Definition: sql_load.cc:1899
Sql_cmd_load_table(enum_filetype filetype, bool is_local_file, enum_source_type source_type, const LEX_STRING &filename, ulong file_count, bool in_key_order, On_duplicate on_duplicate, Table_ident *table, Load_data_partition_list *opt_partitions, const CHARSET_INFO *opt_charset, LEX_CSTRING compression_algorithm, String *opt_xml_rows_identified_by, const Field_separators *field_separators, const Line_separators *line_separators, ulong skip_lines, mem_root_deque< Item * > *opt_fields_or_vars, mem_root_deque< Item * > *opt_set_fields, mem_root_deque< Item * > *opt_set_exprs, List< String > *opt_set_expr_strings, ulong concurrency, ulonglong memory_size, bool is_bulk_operation)
Definition: sql_load.h:97
bool execute_inner(THD *thd, enum enum_duplicates handle_duplicates)
Execute LOAD DATA query.
Definition: sql_load.cc:1159
const bool m_is_local_file
Definition: sql_load.h:153
bool execute_bulk(THD *thd)
Execute BULK LOAD DATA.
Definition: sql_load.cc:654
bool read_fixed_length(THD *thd, COPY_INFO &info, Table_ref *table_list, READ_INFO &read_info, ulong skip_lines)
Read of rows of fixed size + optional garbage + optional newline.
Definition: sql_load.cc:1746
ulong m_concurrency
Number of concurrent loaders to use.
Definition: sql_load.h:166
bool m_ordered_data
true if input data is in order of Primary Key.
Definition: sql_load.h:163
bool truncate_table_for_bulk_load(THD *thd, Table_ref *const table_ref, dd::Table *table_def)
Truncate to create a new table for BULK LOAD.
Definition: sql_load.cc:252
bool run_bulk_driver_for_target(THD *thd, Table_ref *table_ref, Table_ref *new_table_ref, bool has_duplicate_table, const std::string *partition_name_ptr, Bulk_load_file_info &info, Bulk_source src, size_t &affected_rows)
Definition: sql_load.cc:563
enum_source_type m_bulk_source
Source for bulk data.
Definition: sql_load.h:157
mem_root_deque< Item * > m_opt_set_exprs
Definition: sql_load.h:180
LEX_CSTRING m_compression_algorithm_string
Definition: sql_load.h:242
bool read_xml_field(THD *thd, COPY_INFO &info, Table_ref *table_list, READ_INFO &read_info, ulong skip_lines)
Read rows in xml format.
Definition: sql_load.cc:2132
bool duplicate_table_for_bulk_load(THD *thd, std::string &temp_name, const std::string &schema_name, Table_ref *new_table_ref)
Definition: sql_load.cc:412
ulong m_file_count
Number of input files.
Definition: sql_load.h:160
Table_ident *const m_table
Definition: sql_load.h:176
ulonglong m_memory_size
Maximum memory size to be used in bytes.
Definition: sql_load.h:169
bool rename_table_for_incremental_bulk_load(THD *thd, const std::string &schema_name, const std::string &old_table_name, const std::string &new_table_name)
Definition: sql_load.cc:389
bool bulk_driver_service(THD *thd, const TABLE *sql_table, const TABLE *duplicate_table, const Bulk_load_file_info &info, Bulk_source src, size_t &affected_rows)
Definition: sql_load.cc:1023
mem_root_deque< Item * > m_opt_fields_or_vars
Definition: sql_load.h:178
bool execute(THD *thd) override
Execute this SQL statement.
Definition: sql_load.cc:3127
List< String > *const m_opt_set_expr_strings
A list of strings is maintained to store the SET clause command user strings which are specified in l...
Definition: sql_load.h:188
bool validate_table_for_bulk_load(THD *thd, Table_ref *const table_ref, dd::Table *table_def, handlerton **hton)
Validate table for bulk load operation.
Definition: sql_load.cc:336
bool m_is_bulk_operation
true if BULK LOAD.
Definition: sql_load.h:241
bool write_execute_load_query_log_event(THD *thd, const char *db, const char *table_name, bool is_concurrent, enum enum_duplicates duplicates, bool transactional_table, int errocode)
Definition: sql_load.cc:1675
bool m_non_empty_table
Loading non empty table.
Definition: sql_load.h:172
Representation of an SQL command.
Definition: sql_cmd.h:83
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:169
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: sql_lex.h:313
Definition: table.h:2958
Definition: table.h:47
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:172
size_t size() const
Definition: mem_root_deque.h:538
Definition: sql_exchange.h:196
Field_separators field
Definition: sql_exchange.h:198
File_information file_info
Definition: sql_exchange.h:201
void assign_default_values()
Definition: query_result.cc:220
unsigned long skip_lines
Definition: sql_exchange.h:205
Line_separators line
Definition: sql_exchange.h:199
A table definition from the master.
Definition: rpl_utility.h:250
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
A better implementation of the UNIX ctype(3) library.
unsigned long long int ulonglong
Definition: my_inttypes.h:56
enum_sql_command
Definition: my_sqlcommand.h:46
@ SQLCOM_LOAD
Definition: my_sqlcommand.h:77
thread_local MEM_ROOT ** THR_MALLOC
Definition: mysqld.cc:1595
static char * enclosed
Definition: mysqldump.cc:153
uint * concurrency
Definition: mysqlslap.cc:203
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1077
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
const char * table_name
Definition: rules_table_service.cc:56
Define std::hash<Gtid>.
Definition: gtid.h:355
On_duplicate
Definition: parser_yystype.h:252
const char * filename
Definition: pfs_example_component_population.cc:67
Representation of an SQL command.
Contains classes representing SQL-data change statements.
enum_duplicates
Definition: sql_data_change.h:48
enum_filetype
Definition: sql_exchange.h:34
@ UNDEFINED_DEST
Definition: sql_exchange.h:43
enum_source_type
Definition: sql_exchange.h:32
Load_data_partition_mode
Definition: sql_load.h:54
Our own string classes, used pervasively throughout the executor.
case opt name
Definition: sslopt-case.h:29
Definition: bulk_load_service.h:75
Definition: m_ctype.h:421
Definition: mysql_lex_string.h:40
Definition: mysql_lex_string.h:35
Definition: table.h:1456
handlerton is a singleton structure - one instance per storage engine - to provide access to storage ...
Definition: handler.h:2852