1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
#include <Backups/BackupInfo.h>
#include <Backups/BackupSettings.h>
#include <Backups/RestoreSettings.h>
#include <Core/SettingsFields.h>
#include <Parsers/ASTBackupQuery.h>
#include <Parsers/ASTFunction.h>
#include <Parsers/ASTSetQuery.h>
#include <boost/algorithm/string/predicate.hpp>
#include <Common/FieldVisitorConvertToNumber.h>
#include <Backups/SettingsFieldOptionalUUID.h>
#include <Backups/SettingsFieldOptionalString.h>
namespace DB
{
namespace ErrorCodes
{
extern const int CANNOT_PARSE_BACKUP_SETTINGS;
extern const int LOGICAL_ERROR;
}
namespace
{
struct SettingFieldRestoreTableCreationMode
{
RestoreTableCreationMode value;
explicit SettingFieldRestoreTableCreationMode(RestoreTableCreationMode value_) : value(value_) {}
explicit SettingFieldRestoreTableCreationMode(const Field & field)
{
if (field.getType() == Field::Types::String)
{
const String & str = field.get<const String &>();
if (str == "1" || boost::iequals(str, "true") || boost::iequals(str, "create"))
{
value = RestoreTableCreationMode::kCreate;
return;
}
if (str == "0" || boost::iequals(str, "false") || boost::iequals(str, "must exist") || boost::iequals(str, "must-exist"))
{
value = RestoreTableCreationMode::kMustExist;
return;
}
if (boost::iequals(str, "if not exists") || boost::iequals(str, "if-not-exists")
|| boost::iequals(str, "create if not exists") || boost::iequals(str, "create-if-not-exists"))
{
value = RestoreTableCreationMode::kCreateIfNotExists;
return;
}
}
if (field.getType() == Field::Types::UInt64)
{
UInt64 number = field.get<UInt64>();
if (number == 1)
{
value = RestoreTableCreationMode::kCreate;
return;
}
if (number == 0)
{
value = RestoreTableCreationMode::kMustExist;
return;
}
}
throw Exception(ErrorCodes::CANNOT_PARSE_BACKUP_SETTINGS, "Cannot parse creation mode from {}", field);
}
explicit operator Field() const
{
switch (value)
{
case RestoreTableCreationMode::kCreate: return Field{true};
case RestoreTableCreationMode::kMustExist: return Field{false};
case RestoreTableCreationMode::kCreateIfNotExists: return Field{"if-not-exists"};
}
throw Exception(ErrorCodes::LOGICAL_ERROR, "Unexpected value of enum RestoreTableCreationMode: {}", static_cast<int>(value));
}
};
using SettingFieldRestoreDatabaseCreationMode = SettingFieldRestoreTableCreationMode;
struct SettingFieldRestoreAccessCreationMode
{
RestoreAccessCreationMode value;
explicit SettingFieldRestoreAccessCreationMode(RestoreAccessCreationMode value_) : value(value_) {}
explicit SettingFieldRestoreAccessCreationMode(const Field & field)
{
if (field.getType() == Field::Types::String)
{
const String & str = field.get<const String &>();
if (str == "1" || boost::iequals(str, "true") || boost::iequals(str, "create"))
{
value = RestoreAccessCreationMode::kCreate;
return;
}
if (boost::iequals(str, "if not exists") || boost::iequals(str, "if-not-exists")
|| boost::iequals(str, "create if not exists") || boost::iequals(str, "create-if-not-exists"))
{
value = RestoreAccessCreationMode::kCreateIfNotExists;
return;
}
if (boost::iequals(str, "replace") || boost::iequals(str, "create or replace") || boost::iequals(str, "create-or-replace"))
{
value = RestoreAccessCreationMode::kReplace;
return;
}
}
if (field.getType() == Field::Types::UInt64)
{
UInt64 number = field.get<UInt64>();
if (number == 1)
{
value = RestoreAccessCreationMode::kCreate;
return;
}
}
throw Exception(ErrorCodes::CANNOT_PARSE_BACKUP_SETTINGS, "Cannot parse creation mode from {}", field);
}
explicit operator Field() const
{
switch (value)
{
case RestoreAccessCreationMode::kCreate: return Field{true};
case RestoreAccessCreationMode::kCreateIfNotExists: return Field{"if-not-exists"};
case RestoreAccessCreationMode::kReplace: return Field{"replace"};
}
throw Exception(ErrorCodes::LOGICAL_ERROR, "Unexpected value of enum RestoreAccessCreationMode: {}", static_cast<int>(value));
}
};
using SettingFieldRestoreUDFCreationMode = SettingFieldRestoreAccessCreationMode;
}
/// List of restore settings except base_backup_name and cluster_host_ids.
#define LIST_OF_RESTORE_SETTINGS(M) \
M(String, id) \
M(String, password) \
M(Bool, structure_only) \
M(RestoreTableCreationMode, create_table) \
M(RestoreDatabaseCreationMode, create_database) \
M(Bool, allow_different_table_def) \
M(Bool, allow_different_database_def) \
M(Bool, async) \
M(UInt64, shard_num) \
M(UInt64, replica_num) \
M(UInt64, shard_num_in_backup) \
M(UInt64, replica_num_in_backup) \
M(Bool, allow_non_empty_tables) \
M(RestoreAccessCreationMode, create_access) \
M(Bool, allow_unresolved_access_dependencies) \
M(RestoreUDFCreationMode, create_function) \
M(Bool, allow_s3_native_copy) \
M(Bool, use_same_s3_credentials_for_base_backup) \
M(Bool, internal) \
M(String, host_id) \
M(OptionalString, storage_policy) \
M(OptionalUUID, restore_uuid)
RestoreSettings RestoreSettings::fromRestoreQuery(const ASTBackupQuery & query)
{
RestoreSettings res;
if (query.settings)
{
const auto & settings = query.settings->as<const ASTSetQuery &>().changes;
for (const auto & setting : settings)
{
#define GET_SETTINGS_FROM_RESTORE_QUERY_HELPER(TYPE, NAME) \
if (setting.name == #NAME) \
res.NAME = SettingField##TYPE{setting.value}.value; \
else
LIST_OF_RESTORE_SETTINGS(GET_SETTINGS_FROM_RESTORE_QUERY_HELPER)
throw Exception(ErrorCodes::CANNOT_PARSE_BACKUP_SETTINGS, "Unknown setting {}", setting.name);
}
}
if (query.base_backup_name)
res.base_backup_info = BackupInfo::fromAST(*query.base_backup_name);
if (query.cluster_host_ids)
res.cluster_host_ids = BackupSettings::Util::clusterHostIDsFromAST(*query.cluster_host_ids);
return res;
}
void RestoreSettings::copySettingsToQuery(ASTBackupQuery & query) const
{
auto query_settings = std::make_shared<ASTSetQuery>();
query_settings->is_standalone = false;
static const RestoreSettings default_settings;
bool all_settings_are_default = true;
#define SET_SETTINGS_IN_RESTORE_QUERY_HELPER(TYPE, NAME) \
if ((NAME) != default_settings.NAME) \
{ \
query_settings->changes.emplace_back(#NAME, static_cast<Field>(SettingField##TYPE{NAME})); \
all_settings_are_default = false; \
}
LIST_OF_RESTORE_SETTINGS(SET_SETTINGS_IN_RESTORE_QUERY_HELPER)
if (all_settings_are_default)
query_settings = nullptr;
query.settings = query_settings;
auto base_backup_name = base_backup_info ? base_backup_info->toAST() : nullptr;
if (base_backup_name)
query.setOrReplace(query.base_backup_name, base_backup_name);
else
query.reset(query.base_backup_name);
query.cluster_host_ids = !cluster_host_ids.empty() ? BackupSettings::Util::clusterHostIDsToAST(cluster_host_ids) : nullptr;
}
}
|