forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathTableFunctionObjectStorage.cpp
More file actions
425 lines (375 loc) · 15.7 KB
/
TableFunctionObjectStorage.cpp
File metadata and controls
425 lines (375 loc) · 15.7 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
#include "config.h"
#include <Core/Settings.h>
#include <Core/SettingsEnums.h>
#include <Access/Common/AccessFlags.h>
#include <Analyzer/FunctionNode.h>
#include <Analyzer/TableFunctionNode.h>
#include <Parsers/ASTSetQuery.h>
#include <Interpreters/Context.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <TableFunctions/registerTableFunctions.h>
#include <TableFunctions/TableFunctionObjectStorage.h>
#include <TableFunctions/TableFunctionObjectStorageCluster.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>
namespace DB
{
namespace Setting
{
extern const SettingsBool allow_local_data_lakes;
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 NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
extern const int SUPPORT_IS_DISABLED;
}
template <typename Definition, typename Configuration>
ObjectStoragePtr TableFunctionObjectStorage<Definition, Configuration>::getObjectStorage(const ContextPtr & context, bool create_readonly) const
{
if (!object_storage)
object_storage = configuration->createObjectStorage(context, create_readonly);
return object_storage;
}
template <typename Definition, typename Configuration>
StorageObjectStorage::ConfigurationPtr TableFunctionObjectStorage<Definition, Configuration>::getConfiguration() const
{
if (!configuration)
configuration = std::make_shared<Configuration>();
return configuration;
}
template <typename Definition, typename Configuration>
std::vector<size_t> TableFunctionObjectStorage<Definition, Configuration>::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>
void TableFunctionObjectStorage<Definition, Configuration>::parseArguments(const ASTPtr & ast_function, ContextPtr context)
{
if constexpr (std::is_same_v<Definition, IcebergLocalDefinition>)
{
if (!context->getSettingsRef()[Setting::allow_local_data_lakes])
throw Exception(
ErrorCodes::SUPPORT_IS_DISABLED,
"Table function '{}' is disabled. Set `allow_local_data_lakes` to enable it",
getName());
}
/// 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 = std::make_shared<StorageObjectStorageSettings>();
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>
ColumnsDescription TableFunctionObjectStorage<
Definition, Configuration>::getActualTableStructure(ContextPtr context, bool is_insert_query) const
{
if constexpr (std::is_same_v<Definition, IcebergLocalDefinition>)
{
if (!context->getSettingsRef()[Setting::allow_local_data_lakes])
throw Exception(
ErrorCodes::SUPPORT_IS_DISABLED,
"Table function '{}' is disabled. Set `allow_local_data_lakes` to enable it",
getName());
}
if (configuration->structure == "auto")
{
context->checkAccess(getSourceAccessType());
ColumnsDescription columns;
auto storage = getObjectStorage(context, !is_insert_query);
std::string sample_path;
resolveSchemaAndFormat(columns, configuration->format, storage, configuration, std::nullopt, sample_path, context);
return columns;
}
return parseColumnsListFromString(configuration->structure, context);
}
template <typename Definition, typename Configuration>
StoragePtr TableFunctionObjectStorage<Definition, Configuration>::executeImpl(
const ASTPtr & /* ast_function */,
ContextPtr context,
const std::string & table_name,
ColumnsDescription cached_columns,
bool is_insert_query) const
{
if constexpr (std::is_same_v<Definition, IcebergLocalDefinition>)
{
if (!context->getSettingsRef()[Setting::allow_local_data_lakes])
throw Exception(
ErrorCodes::SUPPORT_IS_DISABLED,
"Table function '{}' is disabled. Set `allow_local_data_lakes` to enable it",
getName());
}
chassert(configuration);
ColumnsDescription columns;
if (configuration->structure != "auto")
columns = parseColumnsListFromString(configuration->structure, 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{},
context);
storage->startup();
return storage;
}
bool can_use_distributed_iterator =
client_info.collaborate_with_initiator &&
context->hasReadTaskCallback();
storage = std::make_shared<StorageObjectStorage>(
configuration,
getObjectStorage(context, !is_insert_query),
context,
StorageID(getDatabaseName(), table_name),
columns,
ConstraintsDescription{},
/* comment */ String{},
/* format_settings */ std::nullopt,
/* mode */ LoadingStrictnessLevel::CREATE,
/* distributed_processing */ can_use_distributed_iterator,
/* partition_by */ nullptr);
storage->startup();
return storage;
}
void registerTableFunctionObjectStorage(TableFunctionFactory & factory)
{
UNUSED(factory);
#if USE_AWS_S3
factory.registerFunction<TableFunctionObjectStorage<S3Definition, StorageS3Configuration>>(
{
.documentation =
{
.description=R"(The table function can be used to read the data stored on AWS S3.)",
.examples{{"s3", "SELECT * FROM s3(url, access_key_id, secret_access_key)", ""}
},
.category{""}},
.allow_readonly = false
});
factory.registerFunction<TableFunctionObjectStorage<GCSDefinition, StorageS3Configuration>>(
{
.documentation =
{
.description=R"(The table function can be used to read the data stored on GCS.)",
.examples{{"gcs", "SELECT * FROM gcs(url, access_key_id, secret_access_key)", ""}
},
.category{""}},
.allow_readonly = false
});
factory.registerFunction<TableFunctionObjectStorage<COSNDefinition, StorageS3Configuration>>(
{
.documentation =
{
.description=R"(The table function can be used to read the data stored on COSN.)",
.examples{{"cosn", "SELECT * FROM cosn(url, access_key_id, secret_access_key)", ""}
},
.category{""}},
.allow_readonly = false
});
factory.registerFunction<TableFunctionObjectStorage<OSSDefinition, StorageS3Configuration>>(
{
.documentation =
{
.description=R"(The table function can be used to read the data stored on OSS.)",
.examples{{"oss", "SELECT * FROM oss(url, access_key_id, secret_access_key)", ""}
},
.category{""}},
.allow_readonly = false
});
#endif
#if USE_AZURE_BLOB_STORAGE
factory.registerFunction<TableFunctionObjectStorage<AzureDefinition, StorageAzureConfiguration>>(
{
.documentation =
{
.description=R"(The table function can be used to read the data stored on Azure Blob Storage.)",
.examples{
{
"azureBlobStorage",
"SELECT * FROM azureBlobStorage(connection_string|storage_account_url, container_name, blobpath, "
"[account_name, account_key, format, compression, structure])", ""
}}
},
.allow_readonly = false
});
#endif
#if USE_HDFS
factory.registerFunction<TableFunctionObjectStorage<HDFSDefinition, StorageHDFSConfiguration>>(
{
.documentation =
{
.description=R"(The table function can be used to read the data stored on HDFS virtual filesystem.)",
.examples{
{
"hdfs",
"SELECT * FROM hdfs(url, format, compression, structure])", ""
}}
},
.allow_readonly = false
});
#endif
}
#if USE_AZURE_BLOB_STORAGE
template class TableFunctionObjectStorage<AzureDefinition, StorageAzureConfiguration>;
template class TableFunctionObjectStorage<AzureClusterDefinition, StorageAzureConfiguration>;
#endif
#if USE_AWS_S3
template class TableFunctionObjectStorage<S3Definition, StorageS3Configuration>;
template class TableFunctionObjectStorage<S3ClusterDefinition, StorageS3Configuration>;
template class TableFunctionObjectStorage<GCSDefinition, StorageS3Configuration>;
template class TableFunctionObjectStorage<COSNDefinition, StorageS3Configuration>;
template class TableFunctionObjectStorage<OSSDefinition, StorageS3Configuration>;
#endif
#if USE_HDFS
template class TableFunctionObjectStorage<HDFSDefinition, StorageHDFSConfiguration>;
template class TableFunctionObjectStorage<HDFSClusterDefinition, StorageHDFSConfiguration>;
#endif
template class TableFunctionObjectStorage<LocalDefinition, StorageLocalConfiguration>;
#if USE_AVRO
template class TableFunctionObjectStorage<IcebergLocalDefinition, StorageLocalIcebergConfiguration>;
#endif
#if USE_AVRO && USE_AWS_S3
template class TableFunctionObjectStorage<IcebergS3ClusterDefinition, StorageS3IcebergConfiguration>;
#endif
#if USE_AVRO && USE_AZURE_BLOB_STORAGE
template class TableFunctionObjectStorage<IcebergAzureClusterDefinition, StorageAzureIcebergConfiguration>;
#endif
#if USE_AVRO && USE_HDFS
template class TableFunctionObjectStorage<IcebergHDFSClusterDefinition, StorageHDFSIcebergConfiguration>;
#endif
#if USE_PARQUET && USE_AWS_S3 && USE_DELTA_KERNEL_RS
template class TableFunctionObjectStorage<DeltaLakeClusterDefinition, StorageS3DeltaLakeConfiguration>;
#endif
#if USE_AWS_S3
template class TableFunctionObjectStorage<HudiClusterDefinition, StorageS3HudiConfiguration>;
#endif
#if USE_AVRO
void registerTableFunctionIceberg(TableFunctionFactory & factory)
{
#if USE_AWS_S3
factory.registerFunction<TableFunctionIceberg>(
{.documentation
= {.description = R"(The table function can be used to read the Iceberg table stored on S3 object store. Alias to icebergS3)",
.examples{{"iceberg", "SELECT * FROM iceberg(url, access_key_id, secret_access_key)", ""}},
.category{""}},
.allow_readonly = false});
factory.registerFunction<TableFunctionIcebergS3>(
{.documentation
= {.description = R"(The table function can be used to read the Iceberg table stored on S3 object store.)",
.examples{{"icebergS3", "SELECT * FROM icebergS3(url, access_key_id, secret_access_key)", ""}},
.category{""}},
.allow_readonly = false});
#endif
#if USE_AZURE_BLOB_STORAGE
factory.registerFunction<TableFunctionIcebergAzure>(
{.documentation
= {.description = R"(The table function can be used to read the Iceberg table stored on Azure object store.)",
.examples{{"icebergAzure", "SELECT * FROM icebergAzure(url, access_key_id, secret_access_key)", ""}},
.category{""}},
.allow_readonly = false});
#endif
#if USE_HDFS
factory.registerFunction<TableFunctionIcebergHDFS>(
{.documentation
= {.description = R"(The table function can be used to read the Iceberg table stored on HDFS virtual filesystem.)",
.examples{{"icebergHDFS", "SELECT * FROM icebergHDFS(url)", ""}},
.category{""}},
.allow_readonly = false});
#endif
factory.registerFunction<TableFunctionIcebergLocal>(
{.documentation
= {.description = R"(The table function can be used to read the Iceberg table stored locally.)",
.examples{{"icebergLocal", "SELECT * FROM icebergLocal(filename)", ""}},
.category{""}},
.allow_readonly = false});
}
#endif
#if USE_AWS_S3
#if USE_PARQUET && USE_DELTA_KERNEL_RS
void registerTableFunctionDeltaLake(TableFunctionFactory & factory)
{
factory.registerFunction<TableFunctionDeltaLake>(
{.documentation
= {.description = R"(The table function can be used to read the DeltaLake table stored on object store.)",
.examples{{"deltaLake", "SELECT * FROM deltaLake(url, access_key_id, secret_access_key)", ""}},
.category{""}},
.allow_readonly = false});
}
#endif
void registerTableFunctionHudi(TableFunctionFactory & factory)
{
factory.registerFunction<TableFunctionHudi>(
{.documentation
= {.description = R"(The table function can be used to read the Hudi table stored on object store.)",
.examples{{"hudi", "SELECT * FROM hudi(url, access_key_id, secret_access_key)", ""}},
.category{""}},
.allow_readonly = false});
}
#endif
void registerDataLakeTableFunctions(TableFunctionFactory & factory)
{
UNUSED(factory);
#if USE_AVRO
registerTableFunctionIceberg(factory);
#endif
#if USE_AWS_S3
#if USE_PARQUET && USE_DELTA_KERNEL_RS
registerTableFunctionDeltaLake(factory);
#endif
registerTableFunctionHudi(factory);
#endif
}
}