forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathStorageObjectStorageCluster.cpp
361 lines (307 loc) · 12.5 KB
/
StorageObjectStorageCluster.cpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "Storages/ObjectStorage/StorageObjectStorageCluster.h"
#include <Common/Exception.h>
#include <Core/Settings.h>
#include <Formats/FormatFactory.h>
#include <Parsers/queryToString.h>
#include <Parsers/ASTSelectQuery.h>
#include <Parsers/ASTTablesInSelectQuery.h>
#include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTFunction.h>
#include <Processors/Sources/RemoteSource.h>
#include <QueryPipeline/RemoteQueryExecutor.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <Interpreters/ClusterProxy/SelectStreamFactory.h>
#include <Storages/VirtualColumnUtils.h>
#include <Storages/ObjectStorage/Utils.h>
#include <Storages/ObjectStorage/StorageObjectStorageSource.h>
#include <Storages/extractTableFunctionArgumentsFromSelectQuery.h>
namespace DB
{
namespace Setting
{
extern const SettingsBool use_hive_partitioning;
extern const SettingsString object_storage_cluster;
}
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
extern const int UNKNOWN_FUNCTION;
extern const int NOT_IMPLEMENTED;
}
String StorageObjectStorageCluster::getPathSample(StorageInMemoryMetadata metadata, ContextPtr context)
{
auto query_settings = configuration->getQuerySettings(context);
/// We don't want to throw an exception if there are no files with specified path.
query_settings.throw_on_zero_files_match = false;
if (!configuration->isArchive() && !configuration->isPathWithGlobs())
return configuration->getPath();
auto file_iterator = StorageObjectStorageSource::createFileIterator(
configuration,
query_settings,
object_storage,
false, // distributed_processing
context,
{}, // predicate
metadata.getColumns().getAll(), // virtual_columns
nullptr, // read_keys
{} // file_progress_callback
);
if (auto file = file_iterator->next(0))
return file->getPath();
return "";
}
StorageObjectStorageCluster::StorageObjectStorageCluster(
const String & cluster_name_,
ConfigurationPtr configuration_,
ObjectStoragePtr object_storage_,
ContextPtr context_,
const StorageID & table_id_,
const ColumnsDescription & columns_,
const ConstraintsDescription & constraints_,
const String & comment_,
std::optional<FormatSettings> format_settings_,
LoadingStrictnessLevel mode_,
ASTPtr partition_by_
)
: IStorageCluster(
cluster_name_, table_id_, getLogger(fmt::format("{}({})", configuration_->getEngineName(), table_id_.table_name)))
, configuration{configuration_}
, object_storage(object_storage_)
, cluster_name_in_settings(false)
{
ColumnsDescription columns{columns_};
std::string sample_path;
resolveSchemaAndFormat(columns, configuration->format, object_storage, configuration, {}, sample_path, context_);
configuration->check(context_);
StorageInMemoryMetadata metadata;
metadata.setColumns(columns);
metadata.setConstraints(constraints_);
if (sample_path.empty() && context_->getSettingsRef()[Setting::use_hive_partitioning])
sample_path = getPathSample(metadata, context_);
setVirtuals(VirtualColumnUtils::getVirtualsForFileLikeStorage(metadata.columns, context_, sample_path));
setInMemoryMetadata(metadata);
pure_storage = std::make_shared<StorageObjectStorage>(
configuration,
object_storage,
context_,
getStorageID(),
getInMemoryMetadata().getColumns(),
getInMemoryMetadata().getConstraints(),
comment_,
format_settings_,
mode_,
/* distributed_processing */false,
partition_by_);
auto virtuals_ = getVirtualsPtr();
if (virtuals_)
pure_storage->setVirtuals(*virtuals_);
pure_storage->setInMemoryMetadata(getInMemoryMetadata());
}
std::string StorageObjectStorageCluster::getName() const
{
return configuration->getEngineName();
}
void StorageObjectStorageCluster::updateQueryForDistributedEngineIfNeeded(ASTPtr & query, ContextPtr context)
{
// Change table engine on table function for distributed request
// CREATE TABLE t (...) ENGINE=IcebergS3(...)
// SELECT * FROM t
// change on
// SELECT * FROM icebergS3(...)
// to execute on cluster nodes
auto * select_query = query->as<ASTSelectQuery>();
if (!select_query || !select_query->tables())
return;
auto * tables = select_query->tables()->as<ASTTablesInSelectQuery>();
if (tables->children.empty())
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Expected SELECT query from table with engine {}, got '{}'",
configuration->getEngineName(), queryToString(query));
auto * table_expression = tables->children[0]->as<ASTTablesInSelectQueryElement>()->table_expression->as<ASTTableExpression>();
if (!table_expression)
return;
if (!table_expression->database_and_table_name)
return;
auto & table_identifier_typed = table_expression->database_and_table_name->as<ASTTableIdentifier &>();
auto table_alias = table_identifier_typed.tryGetAlias();
auto storage_engine_name = configuration->getEngineName();
if (storage_engine_name == "Iceberg")
{
switch (configuration->getType())
{
case ObjectStorageType::S3:
storage_engine_name = "IcebergS3";
break;
case ObjectStorageType::Azure:
storage_engine_name = "IcebergAzure";
break;
case ObjectStorageType::HDFS:
storage_engine_name = "IcebergHDFS";
break;
default:
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Can't find table function for engine {}",
storage_engine_name
);
}
}
static std::unordered_map<std::string, std::string> engine_to_function = {
{"S3", "s3"},
{"Azure", "azureBlobStorage"},
{"HDFS", "hdfs"},
{"IcebergS3", "icebergS3"},
{"IcebergAzure", "icebergAzure"},
{"IcebergHDFS", "icebergHDFS"},
{"DeltaLake", "deltaLake"},
{"Hudi", "hudi"}
};
auto p = engine_to_function.find(storage_engine_name);
if (p == engine_to_function.end())
{
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Can't find table function for engine {}",
storage_engine_name
);
}
std::string table_function_name = p->second;
auto function_ast = std::make_shared<ASTFunction>();
function_ast->name = table_function_name;
auto cluster_name = getClusterName(context);
if (cluster_name.empty())
{
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Can't be here without cluster name, no cluster name in query {}",
queryToString(query));
}
function_ast->arguments = configuration->createArgsWithAccessData();
function_ast->children.push_back(function_ast->arguments);
function_ast->setAlias(table_alias);
ASTPtr function_ast_ptr(function_ast);
table_expression->database_and_table_name = nullptr;
table_expression->table_function = function_ast_ptr;
table_expression->children[0] = function_ast_ptr;
auto settings = select_query->settings();
if (settings)
{
auto & settings_ast = settings->as<ASTSetQuery &>();
settings_ast.changes.insertSetting("object_storage_cluster", cluster_name);
}
else
{
auto settings_ast_ptr = std::make_shared<ASTSetQuery>();
settings_ast_ptr->is_standalone = false;
settings_ast_ptr->changes.setSetting("object_storage_cluster", cluster_name);
select_query->setExpression(ASTSelectQuery::Expression::SETTINGS, std::move(settings_ast_ptr));
}
cluster_name_in_settings = true;
}
void StorageObjectStorageCluster::updateQueryToSendIfNeeded(
ASTPtr & query,
const DB::StorageSnapshotPtr & storage_snapshot,
const ContextPtr & context)
{
updateQueryForDistributedEngineIfNeeded(query, context);
ASTExpressionList * expression_list = extractTableFunctionArgumentsFromSelectQuery(query);
if (!expression_list)
{
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Expected SELECT query from table function {}, got '{}'",
configuration->getEngineName(), queryToString(query));
}
ASTs & args = expression_list->children;
const auto & structure = storage_snapshot->metadata->getColumns().getAll().toNamesAndTypesDescription();
if (args.empty())
{
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Unexpected empty list of arguments for {}Cluster table function",
configuration->getEngineName());
}
if (cluster_name_in_settings)
{
configuration->addStructureAndFormatToArgsIfNeeded(args, structure, configuration->format, context, /*with_structure=*/true);
}
else
{
ASTPtr cluster_name_arg = args.front();
args.erase(args.begin());
configuration->addStructureAndFormatToArgsIfNeeded(args, structure, configuration->format, context, /*with_structure=*/true);
args.insert(args.begin(), cluster_name_arg);
}
}
RemoteQueryExecutor::Extension StorageObjectStorageCluster::getTaskIteratorExtension(
const ActionsDAG::Node * predicate, const ContextPtr & local_context) const
{
auto iterator = StorageObjectStorageSource::createFileIterator(
configuration, configuration->getQuerySettings(local_context), object_storage, /* distributed_processing */false,
local_context, predicate, getVirtualsList(), nullptr, local_context->getFileProgressCallback());
auto callback = std::make_shared<std::function<String()>>([iterator]() mutable -> String
{
auto object_info = iterator->next(0);
if (object_info)
return object_info->getPath();
return "";
});
return RemoteQueryExecutor::Extension{ .task_iterator = std::move(callback) };
}
void StorageObjectStorageCluster::readFallBackToPure(
QueryPlan & query_plan,
const Names & column_names,
const StorageSnapshotPtr & storage_snapshot,
SelectQueryInfo & query_info,
ContextPtr context,
QueryProcessingStage::Enum processed_stage,
size_t max_block_size,
size_t num_streams)
{
pure_storage->read(query_plan, column_names, storage_snapshot, query_info, context, processed_stage, max_block_size, num_streams);
}
SinkToStoragePtr StorageObjectStorageCluster::writeFallBackToPure(
const ASTPtr & query,
const StorageMetadataPtr & metadata_snapshot,
ContextPtr context,
bool async_insert)
{
return pure_storage->write(query, metadata_snapshot, context, async_insert);
}
String StorageObjectStorageCluster::getClusterName(ContextPtr context) const
{
/// StorageObjectStorageCluster is always created for cluster or non-cluster variants.
/// User can specify cluster name in table definition or in setting `object_storage_cluster`
/// only for several queries. When it specified in both places, priority is given to the query setting.
/// When it is empty, non-cluster realization is used.
auto cluster_name_from_settings = context->getSettingsRef()[Setting::object_storage_cluster].value;
if (cluster_name_from_settings.empty())
cluster_name_from_settings = getOriginalClusterName();
return cluster_name_from_settings;
}
QueryProcessingStage::Enum StorageObjectStorageCluster::getQueryProcessingStage(
ContextPtr context, QueryProcessingStage::Enum to_stage, const StorageSnapshotPtr & storage_snapshot, SelectQueryInfo & query_info) const
{
/// Full query if fall back to pure storage.
if (getClusterName(context).empty())
return QueryProcessingStage::Enum::FetchColumns;
/// Distributed storage.
return IStorageCluster::getQueryProcessingStage(context, to_stage, storage_snapshot, query_info);
}
void StorageObjectStorageCluster::truncate(
const ASTPtr & query,
const StorageMetadataPtr & metadata_snapshot,
ContextPtr local_context,
TableExclusiveLockHolder & lock_holder)
{
/// Full query if fall back to pure storage.
if (getClusterName(local_context).empty())
return pure_storage->truncate(query, metadata_snapshot, local_context, lock_holder);
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Truncate is not supported by storage {}", getName());
}
void StorageObjectStorageCluster::addInferredEngineArgsToCreateQuery(ASTs & args, const ContextPtr & context) const
{
configuration->addStructureAndFormatToArgsIfNeeded(args, "", configuration->format, context, /*with_structure=*/false);
}
}