forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathTableFunctionObjectStorage.cpp
More file actions
479 lines (420 loc) · 19.4 KB
/
TableFunctionObjectStorage.cpp
File metadata and controls
479 lines (420 loc) · 19.4 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include <string_view>
#include "config.h"
#include <Core/Settings.h>
#include <Core/SettingsEnums.h>
#include <Analyzer/FunctionNode.h>
#include <Analyzer/TableFunctionNode.h>
#include <Interpreters/Context.h>
#include <Parsers/ASTSetQuery.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <TableFunctions/TableFunctionObjectStorage.h>
#include <TableFunctions/TableFunctionObjectStorageCluster.h>
#include <TableFunctions/registerTableFunctions.h>
#include <Interpreters/parseColumnsListForTableFunction.h>
#include <Storages/ObjectStorage/Utils.h>
#include <Storages/NamedCollectionsHelpers.h>
#include <Storages/ObjectStorage/Azure/Configuration.h>
#include <Storages/ObjectStorage/HDFS/Configuration.h>
#include <Storages/ObjectStorage/Local/Configuration.h>
#include <Storages/ObjectStorage/S3/Configuration.h>
#include <Storages/ObjectStorage/StorageObjectStorage.h>
#include <Storages/ObjectStorage/StorageObjectStorageCluster.h>
#include <Storages/ObjectStorage/DataLakes/DataLakeStorageSettings.h>
#include <Storages/ObjectStorage/DataLakes/DataLakeConfiguration.h>
#include <Storages/HivePartitioningUtils.h>
namespace DB
{
namespace Setting
{
extern const SettingsUInt64 allow_experimental_parallel_reading_from_replicas;
extern const SettingsBool parallel_replicas_for_cluster_engines;
extern const SettingsString cluster_for_parallel_replicas;
extern const SettingsParallelReplicasMode parallel_replicas_mode;
}
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
namespace DataLakeStorageSetting
{
extern const DataLakeStorageSettingsString disk;
}
template <typename Definition, typename Configuration, bool is_data_lake>
ObjectStoragePtr TableFunctionObjectStorage<Definition, Configuration, is_data_lake>::getObjectStorage(const ContextPtr & context, bool create_readonly) const
{
if (!object_storage)
object_storage = configuration->createObjectStorage(context, create_readonly, std::nullopt);
return object_storage;
}
template <typename Definition, typename Configuration, bool is_data_lake>
StorageObjectStorageConfigurationPtr TableFunctionObjectStorage<Definition, Configuration, is_data_lake>::getConfiguration(ContextPtr context) const
{
if (!configuration)
{
if constexpr (is_data_lake)
{
const auto disk_name = settings && (*settings)[DataLakeStorageSetting::disk].changed
? (*settings)[DataLakeStorageSetting::disk].value
: "";
if (!disk_name.empty())
{
auto disk = context->getDisk(disk_name);
switch (disk->getObjectStorage()->getType())
{
#if USE_AWS_S3 && USE_AVRO
case ObjectStorageType::S3:
if (Definition::object_storage_type != "s3")
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Disk type doesn't match with table engine type storage");
if (std::string_view(Definition::name).starts_with("iceberg"))
configuration = std::make_shared<StorageS3IcebergConfiguration>(settings);
#if USE_PARQUET
else
configuration = std::make_shared<StorageS3DeltaLakeConfiguration>(settings);
#endif
break;
#endif
#if USE_AZURE_BLOB_STORAGE && USE_AVRO
case ObjectStorageType::Azure:
if (Definition::name != "iceberg" &&
Definition::name != "icebergCluster" &&
Definition::name != "deltaLake" &&
Definition::name != "deltaLakeCluster" &&
Definition::object_storage_type != "azure")
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Disk type doesn't match with table engine type storage");
if (std::string_view(Definition::name).starts_with("iceberg"))
configuration = std::make_shared<StorageAzureIcebergConfiguration>(settings);
#if USE_PARQUET
else
configuration = std::make_shared<StorageAzureDeltaLakeConfiguration>(settings);
#endif
break;
#endif
#if USE_AVRO
case ObjectStorageType::Local:
if (Definition::name != "iceberg" &&
Definition::name != "icebergCluster" &&
Definition::name != "deltaLake" &&
Definition::name != "deltaLakeCluster" &&
Definition::object_storage_type != "local")
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Disk type doesn't match with table engine type storage");
if (std::string_view(Definition::name).starts_with("iceberg"))
configuration = std::make_shared<StorageLocalIcebergConfiguration>(settings);
#if USE_PARQUET
else
configuration = std::make_shared<StorageLocalDeltaLakeConfiguration>(settings);
#endif
break;
#endif
default:
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Unsupported disk type for iceberg {}", disk->getObjectStorage()->getType());
}
}
else
configuration = std::make_shared<Configuration>(settings);
}
else
configuration = std::make_shared<Configuration>();
}
return configuration;
}
template <typename Definition, typename Configuration, bool is_data_lake>
std::vector<size_t> TableFunctionObjectStorage<Definition, Configuration, is_data_lake>::skipAnalysisForArguments(
const QueryTreeNodePtr & query_node_table_function, ContextPtr) const
{
auto & table_function_node = query_node_table_function->as<TableFunctionNode &>();
auto & table_function_arguments_nodes = table_function_node.getArguments().getNodes();
size_t table_function_arguments_size = table_function_arguments_nodes.size();
std::vector<size_t> result;
for (size_t i = 0; i < table_function_arguments_size; ++i)
{
auto * function_node = table_function_arguments_nodes[i]->as<FunctionNode>();
if (function_node && function_node->getFunctionName() == "headers")
result.push_back(i);
}
return result;
}
template <typename Definition, typename Configuration, bool is_data_lake>
std::shared_ptr<typename TableFunctionObjectStorage<Definition, Configuration, is_data_lake>::Settings>
TableFunctionObjectStorage<Definition, Configuration, is_data_lake>::createEmptySettings()
{
if constexpr (is_data_lake)
return std::make_shared<DataLakeStorageSettings>();
else
return std::make_shared<StorageObjectStorageSettings>();
}
template <typename Definition, typename Configuration, bool is_data_lake>
void TableFunctionObjectStorage<Definition, Configuration, is_data_lake>::parseArguments(const ASTPtr & ast_function, ContextPtr context)
{
/// Clone ast function, because we can modify its arguments like removing headers.
auto ast_copy = ast_function->clone();
ASTs & args_func = ast_copy->children;
if (args_func.size() != 1)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function '{}' must have arguments.", getName());
settings = createEmptySettings();
auto & args = args_func.at(0)->children;
/// Support storage settings in table function,
/// e.g. `s3(endpoint, ..., SETTINGS setting=value, ..., setting=value)`
/// We do similarly for some other table functions
/// whose storage implementation supports storage settings (for example, MySQL).
for (auto it = args.begin(); it != args.end(); ++it)
{
ASTSetQuery * settings_ast = (*it)->as<ASTSetQuery>();
if (settings_ast)
{
settings->loadFromQuery(*settings_ast);
args.erase(it);
break;
}
}
parseArgumentsImpl(args, context);
}
template <typename Definition, typename Configuration, bool is_data_lake>
ColumnsDescription TableFunctionObjectStorage<
Definition, Configuration, is_data_lake>::getActualTableStructure(ContextPtr context, bool is_insert_query) const
{
if (configuration->getStructure() == "auto")
{
auto storage = getObjectStorage(context, !is_insert_query);
configuration->lazyInitializeIfNeeded(object_storage, context);
std::string sample_path;
ColumnsDescription columns;
resolveSchemaAndFormat(
columns,
std::move(storage),
configuration,
/* format_settings */std::nullopt,
sample_path,
context);
HivePartitioningUtils::setupHivePartitioningForObjectStorage(
columns,
configuration,
sample_path,
/* inferred_schema */ true,
/* format_settings */ std::nullopt,
context);
return columns;
}
return parseColumnsListFromString(configuration->getStructure(), context);
}
template <typename Definition, typename Configuration, bool is_data_lake>
StoragePtr TableFunctionObjectStorage<Definition, Configuration, is_data_lake>::executeImpl(
const ASTPtr & /* ast_function */,
ContextPtr context,
const std::string & table_name,
ColumnsDescription cached_columns,
bool is_insert_query) const
{
chassert(configuration);
ColumnsDescription columns;
if (configuration->getStructure() != "auto")
columns = parseColumnsListFromString(configuration->getStructure(), context);
else if (!structure_hint.empty())
columns = structure_hint;
else if (!cached_columns.empty())
columns = cached_columns;
StoragePtr storage;
const auto & query_settings = context->getSettingsRef();
const auto & client_info = context->getClientInfo();
const auto parallel_replicas_cluster_name = query_settings[Setting::cluster_for_parallel_replicas].toString();
const auto can_use_parallel_replicas = !parallel_replicas_cluster_name.empty()
&& query_settings[Setting::parallel_replicas_for_cluster_engines]
&& context->canUseTaskBasedParallelReplicas()
&& !context->isDistributed();
const auto is_secondary_query = context->getClientInfo().query_kind == ClientInfo::QueryKind::SECONDARY_QUERY;
if (can_use_parallel_replicas && !is_secondary_query && !is_insert_query)
{
storage = std::make_shared<StorageObjectStorageCluster>(
parallel_replicas_cluster_name,
configuration,
getObjectStorage(context, !is_insert_query),
StorageID(getDatabaseName(), table_name),
columns,
ConstraintsDescription{},
partition_by,
/* order_by */ nullptr,
context,
/* comment */ String{},
/* format_settings */ std::nullopt, /// No format_settings
/* mode */ LoadingStrictnessLevel::CREATE,
configuration->getCatalog(context, StorageID(getDatabaseName(), table_name)),
/* if_not_exists */ false,
/* is_datalake_query*/ false,
/* is_table_function */ true);
storage->startup();
return storage;
}
bool can_use_distributed_iterator =
client_info.collaborate_with_initiator &&
context->hasClusterFunctionReadTaskCallback();
std::string disk_name;
if constexpr (is_data_lake)
{
disk_name = settings && (*settings)[DataLakeStorageSetting::disk].changed
? (*settings)[DataLakeStorageSetting::disk].value
: "";
}
ObjectStoragePtr current_object_storage;
if (configuration->isDataLakeConfiguration() && !disk_name.empty())
current_object_storage = context->getDisk(disk_name)->getObjectStorage();
else
current_object_storage = getObjectStorage(context, !is_insert_query);
storage = std::make_shared<StorageObjectStorage>(
configuration,
current_object_storage,
context,
StorageID(getDatabaseName(), table_name),
columns,
ConstraintsDescription{},
/* comment */ String{},
/* format_settings */ std::nullopt,
/* mode */ LoadingStrictnessLevel::CREATE,
/* catalog*/ nullptr,
/* if_not_exists*/ false,
/* is_datalake_query*/ false,
/* distributed_processing */ can_use_distributed_iterator,
/* partition_by */ partition_by,
/* order_by */ nullptr,
/* is_table_function */true);
storage->startup();
return storage;
}
void registerTableFunctionObjectStorage(TableFunctionFactory & factory)
{
UNUSED(factory);
#if USE_AWS_S3
factory.registerFunction<TableFunctionObjectStorage<GCSDefinition, StorageS3Configuration, false>>(
{
.description=R"(The table function can be used to read the data stored on GCS.)",
.examples{{GCSDefinition::name, "SELECT * FROM gcs(url, access_key_id, secret_access_key)", ""}},
.category = FunctionDocumentation::Category::TableFunction
},
{.allow_readonly = false}
);
factory.registerFunction<TableFunctionObjectStorage<COSNDefinition, StorageS3Configuration, false>>(
{
.description=R"(The table function can be used to read the data stored on COSN.)",
.examples{{COSNDefinition::name, "SELECT * FROM cosn(url, access_key_id, secret_access_key)", ""}},
.category = FunctionDocumentation::Category::TableFunction
},
{.allow_readonly = false}
);
factory.registerFunction<TableFunctionObjectStorage<OSSDefinition, StorageS3Configuration, false>>(
{
.description=R"(The table function can be used to read the data stored on OSS.)",
.examples{{OSSDefinition::name, "SELECT * FROM oss(url, access_key_id, secret_access_key)", ""}},
.category = FunctionDocumentation::Category::TableFunction
},
{.allow_readonly = false}
);
#endif
}
#if USE_AZURE_BLOB_STORAGE
template class TableFunctionObjectStorage<AzureDefinition, StorageAzureConfiguration, false>;
template class TableFunctionObjectStorage<AzureClusterDefinition, StorageAzureConfiguration, false>;
#endif
#if USE_AWS_S3
template class TableFunctionObjectStorage<S3Definition, StorageS3Configuration, false>;
template class TableFunctionObjectStorage<S3ClusterDefinition, StorageS3Configuration, false>;
template class TableFunctionObjectStorage<GCSDefinition, StorageS3Configuration, false>;
template class TableFunctionObjectStorage<COSNDefinition, StorageS3Configuration, false>;
template class TableFunctionObjectStorage<OSSDefinition, StorageS3Configuration, false>;
#endif
#if USE_HDFS
template class TableFunctionObjectStorage<HDFSDefinition, StorageHDFSConfiguration, false>;
template class TableFunctionObjectStorage<HDFSClusterDefinition, StorageHDFSConfiguration, false>;
#endif
#if USE_AVRO
template class TableFunctionObjectStorage<IcebergClusterDefinition, StorageIcebergConfiguration, true>;
#endif
#if USE_AVRO
template class TableFunctionObjectStorage<IcebergLocalClusterDefinition, StorageLocalIcebergConfiguration, true>;
#endif
#if USE_AVRO && USE_AWS_S3
template class TableFunctionObjectStorage<IcebergS3ClusterDefinition, StorageS3IcebergConfiguration, true>;
template class TableFunctionObjectStorage<IcebergClusterDefinition, StorageS3IcebergConfiguration, true>;
#endif
#if USE_AVRO && USE_AZURE_BLOB_STORAGE
template class TableFunctionObjectStorage<IcebergAzureClusterDefinition, StorageAzureIcebergConfiguration, true>;
#endif
#if USE_AVRO && USE_HDFS
template class TableFunctionObjectStorage<IcebergHDFSClusterDefinition, StorageHDFSIcebergConfiguration, true>;
#endif
#if USE_AVRO && USE_AWS_S3
template class TableFunctionObjectStorage<PaimonS3ClusterDefinition, StorageS3PaimonConfiguration, true>;
template class TableFunctionObjectStorage<PaimonClusterDefinition, StorageS3PaimonConfiguration, true>;
#endif
#if USE_AVRO && USE_AZURE_BLOB_STORAGE
template class TableFunctionObjectStorage<PaimonAzureClusterDefinition, StorageAzurePaimonConfiguration, true>;
#endif
#if USE_AVRO && USE_HDFS
template class TableFunctionObjectStorage<PaimonHDFSClusterDefinition, StorageHDFSPaimonConfiguration, true>;
#endif
#if USE_PARQUET && USE_AWS_S3 && USE_DELTA_KERNEL_RS
template class TableFunctionObjectStorage<DeltaLakeClusterDefinition, StorageS3DeltaLakeConfiguration, true>;
template class TableFunctionObjectStorage<DeltaLakeS3ClusterDefinition, StorageS3DeltaLakeConfiguration, true>;
#endif
#if USE_PARQUET && USE_AZURE_BLOB_STORAGE && USE_DELTA_KERNEL_RS
template class TableFunctionObjectStorage<DeltaLakeAzureClusterDefinition, StorageAzureDeltaLakeConfiguration, true>;
#endif
#if USE_AWS_S3
template class TableFunctionObjectStorage<HudiClusterDefinition, StorageS3HudiConfiguration, true>;
#endif
#if USE_AVRO
void registerTableFunctionPaimon(TableFunctionFactory & factory)
{
#if USE_AWS_S3
factory.registerFunction<TableFunctionPaimon>(
{.description = R"(The table function can be used to read the Paimon table stored on S3 object store. Alias to paimonS3)",
.examples{{"paimon", "SELECT * FROM paimon(url, access_key_id, secret_access_key)", ""}},
.category = FunctionDocumentation::Category::TableFunction},
{.allow_readonly = false});
factory.registerFunction<TableFunctionPaimonS3>(
{.description = R"(The table function can be used to read the Paimon table stored on S3 object store.)",
.examples{{"paimonS3", "SELECT * FROM paimonS3(url, access_key_id, secret_access_key)", ""}},
.category = FunctionDocumentation::Category::TableFunction},
{.allow_readonly = false});
#endif
#if USE_AZURE_BLOB_STORAGE
factory.registerFunction<TableFunctionPaimonAzure>(
{.description = R"(The table function can be used to read the Paimon table stored on Azure object store.)",
.examples{{"paimonAzure", "SELECT * FROM paimonAzure(url, access_key_id, secret_access_key)", ""}},
.category = FunctionDocumentation::Category::TableFunction},
{.allow_readonly = false});
#endif
#if USE_HDFS
factory.registerFunction<TableFunctionPaimonHDFS>(
{.description = R"(The table function can be used to read the Paimon table stored on HDFS virtual filesystem.)",
.examples{{"paimonHDFS", "SELECT * FROM paimonHDFS(url)", ""}},
.category = FunctionDocumentation::Category::TableFunction},
{.allow_readonly = false});
#endif
factory.registerFunction<TableFunctionPaimonLocal>(
{.description = R"(The table function can be used to read the Paimon table stored locally.)",
.examples{{"paimonLocal", "SELECT * FROM paimonLocal(filename)", ""}},
.category = FunctionDocumentation::Category::TableFunction},
{.allow_readonly = false});
}
#endif
#if USE_PARQUET && USE_DELTA_KERNEL_RS
void registerTableFunctionDeltaLake(TableFunctionFactory & factory)
{
// Register the new local Delta Lake table function
factory.registerFunction<TableFunctionDeltaLakeLocal>(
{.description = R"(The table function can be used to read the DeltaLake table stored locally.)",
.examples{{DeltaLakeLocalDefinition::name, "SELECT * FROM deltaLakeLocal(path)", ""}},
.category = FunctionDocumentation::Category::TableFunction},
{.allow_readonly = false});
}
#endif
void registerDataLakeTableFunctions(TableFunctionFactory & factory)
{
UNUSED(factory);
#if USE_PARQUET && USE_DELTA_KERNEL_RS
registerTableFunctionDeltaLake(factory);
#endif
#if USE_AVRO
registerTableFunctionPaimon(factory);
#endif
}
}