forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathUtils.cpp
More file actions
507 lines (430 loc) · 19.1 KB
/
Utils.cpp
File metadata and controls
507 lines (430 loc) · 19.1 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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include <Core/Settings.h>
#include <Disks/IO/AsynchronousBoundedReadBuffer.h>
#include <Disks/IO/CachedOnDiskReadBufferFromFile.h>
#include <Disks/IO/getThreadPoolReader.h>
#include <Disks/ObjectStorages/IObjectStorage.h>
#include <Interpreters/Cache/FileCache.h>
#include <Interpreters/Cache/FileCacheFactory.h>
#include <Interpreters/Cache/FileCacheKey.h>
#include <Interpreters/Context.h>
#include <Storages/ObjectStorage/StorageObjectStorage.h>
#include <Storages/ObjectStorage/Utils.h>
#include <Disks/ObjectStorages/ObjectStorageFactory.h>
#include <Poco/Util/MapConfiguration.h>
#include <IO/S3/URI.h>
#include <filesystem>
#include <functional>
#if USE_AWS_S3
#include <Disks/ObjectStorages/S3/S3ObjectStorage.h>
#endif
#if USE_HDFS
#include <Disks/ObjectStorages/HDFS/HDFSObjectStorage.h>
#endif
namespace DB
{
namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
extern const int LOGICAL_ERROR;
}
namespace
{
std::string normalizeScheme(const std::string & scheme)
{
auto scheme_lowercase = Poco::toLower(scheme);
if (scheme_lowercase == "s3a" || scheme_lowercase == "s3n")
scheme_lowercase = "s3";
else if (scheme_lowercase == "wasb" || scheme_lowercase == "wasbs" || scheme_lowercase == "abfss")
scheme_lowercase = "abfs";
return scheme_lowercase;
}
std::string factoryTypeForScheme(const std::string & normalized_scheme)
{
if (normalized_scheme == "s3") return "s3";
if (normalized_scheme == "abfs") return "azure";
if (normalized_scheme == "hdfs") return "hdfs";
if (normalized_scheme == "file") return "local";
return "";
}
bool isAbsolutePath(const std::string & path)
{
if (!path.empty() && (path.front() == '/' || path.find("://") != std::string_view::npos))
return true;
return false;
}
/// Normalize a path string by removing redundant components and leading slashes.
std::string normalizePathString(const std::string & path)
{
std::filesystem::path fs_path(path);
std::filesystem::path normalized = fs_path.lexically_normal();
std::string normalized_result = normalized.string();
while (!normalized_result.empty() && normalized_result.front() == '/')
normalized_result = normalized_result.substr(1);
return normalized_result;
}
#if USE_AWS_S3
/// For s3:// URIs (generic), bucket needs to match.
/// For explicit http(s):// URIs, both bucket and endpoint must match.
bool s3URIMatches(const S3::URI & target_uri, const std::string & base_bucket, const std::string & base_endpoint, const std::string & target_scheme_normalized)
{
bool bucket_matches = (target_uri.bucket == base_bucket);
bool endpoint_matches = (target_uri.endpoint == base_endpoint);
bool is_generic_s3_uri = (target_scheme_normalized == "s3");
return bucket_matches && (endpoint_matches || is_generic_s3_uri);
}
#endif
std::pair<ObjectStoragePtr, std::string> getOrCreateStorageAndKey(
const std::string & cache_key,
const std::string & key_to_use,
const std::string & storage_type,
SecondaryStorages & secondary_storages,
const ContextPtr & context,
std::function<void(Poco::Util::MapConfiguration &, const std::string &)> configure_fn)
{
{
std::lock_guard lock(secondary_storages.mutex);
if (auto it = secondary_storages.storages.find(cache_key); it != secondary_storages.storages.end())
return {it->second, key_to_use};
}
Poco::AutoPtr<Poco::Util::MapConfiguration> cfg(new Poco::Util::MapConfiguration);
const std::string config_prefix = "object_storages." + cache_key;
cfg->setString(config_prefix + ".object_storage_type", storage_type);
configure_fn(*cfg, config_prefix);
ObjectStoragePtr storage = ObjectStorageFactory::instance().create(cache_key, *cfg, config_prefix, context, /*skip_access_check*/ true);
{
std::lock_guard lock(secondary_storages.mutex);
auto [it, inserted] = secondary_storages.storages.emplace(cache_key, storage);
if (!inserted)
return {it->second, key_to_use};
}
return {storage, key_to_use};
}
/// Normalize a path (relative to table location ot absolute path) to a key that will be looked up in the object storage.
std::string normalizePathToStorageRoot(const std::string & table_location, const std::string & path)
{
if (table_location.empty())
{
if (!path.empty() && path.front() == '/')
return path.substr(1);
return path;
}
if (isAbsolutePath(path))
return SchemeAuthorityKey(path).key; // Absolute path, return the key part
SchemeAuthorityKey base{table_location};
if (base.key.empty())
return path; // Table location is empty, return the path as is
std::string base_key_trimmed = base.key;
while (!base_key_trimmed.empty() && base_key_trimmed.front() == '/')
base_key_trimmed = base_key_trimmed.substr(1);
while (!base_key_trimmed.empty() && base_key_trimmed.back() == '/')
base_key_trimmed.pop_back();
std::string rel_path = path;
while (!rel_path.empty() && rel_path.front() == '/')
rel_path = rel_path.substr(1);
if (!base_key_trimmed.empty() && (rel_path == base_key_trimmed || rel_path.starts_with(base_key_trimmed + "/")))
return normalizePathString(rel_path); // Path already includes table location
std::string result = base.key;
if (!result.empty() && result.back() != '/')
result += '/';
result += rel_path;
return normalizePathString(result);
}
}
// TODO: handle https://s3.amazonaws.com/bucketname/... properly
SchemeAuthorityKey::SchemeAuthorityKey(const std::string & uri)
{
if (uri.empty())
return;
if (auto scheme_sep = uri.find("://"); scheme_sep != std::string_view::npos)
{
scheme = Poco::toLower(uri.substr(0, scheme_sep));
auto rest = uri.substr(scheme_sep + 3); // skip ://
// authority is up to next '/'
auto slash = rest.find('/');
if (slash == std::string_view::npos)
{
authority = std::string(rest);
key = "/"; // Path obviously incorrect, but it will be dealt with by caller
return;
}
authority = std::string(rest.substr(0, slash));
/// For file:// URIs, the path is absolute, so we need to keep the leading '/'
/// e.g. file:///home/user/data -> scheme="file", authority="", key="/home/user/data"
if (scheme == "file")
key = std::string(rest.substr(slash));
else
key = std::string(rest.substr(++slash));
return;
}
// if part has no scheme and starts with '/' -- it is an absolute uri for local file: file:///path
if (uri.front() == '/')
{
scheme = "file";
key = std::string(uri);
return;
}
// Relative path
key = std::string(uri);
}
std::optional<String> checkAndGetNewFileOnInsertIfNeeded(
const IObjectStorage & object_storage,
const StorageObjectStorageConfiguration & configuration,
const StorageObjectStorageQuerySettings & settings,
const String & key,
size_t sequence_number)
{
if (settings.truncate_on_insert
|| !object_storage.exists(StoredObject(key)))
return std::nullopt;
if (settings.create_new_file_on_insert)
{
auto pos = key.find_first_of('.');
String new_key;
do
{
new_key = key.substr(0, pos) + "." + std::to_string(sequence_number) + (pos == std::string::npos ? "" : key.substr(pos));
++sequence_number;
}
while (object_storage.exists(StoredObject(new_key)));
return new_key;
}
throw Exception(
ErrorCodes::BAD_ARGUMENTS,
"Object in bucket {} with key {} already exists. "
"If you want to overwrite it, enable setting {}_truncate_on_insert, if you "
"want to create a new file on each insert, enable setting {}_create_new_file_on_insert",
configuration.getNamespace(), key, configuration.getTypeName(), configuration.getTypeName());
}
void resolveSchemaAndFormat(
ColumnsDescription & columns,
ObjectStoragePtr object_storage,
StorageObjectStorageConfigurationPtr & configuration,
std::optional<FormatSettings> format_settings,
std::string & sample_path,
const ContextPtr & context)
{
if (configuration->getFormat() == "auto")
{
if (configuration->isDataLakeConfiguration())
{
throw Exception(
ErrorCodes::LOGICAL_ERROR,
"Format must be already specified for {} storage.",
configuration->getTypeName());
}
}
if (columns.empty())
{
if (configuration->isDataLakeConfiguration())
{
auto table_structure = configuration->tryGetTableStructureFromMetadata();
if (table_structure)
columns = table_structure.value();
}
if (columns.empty())
{
if (configuration->getFormat() == "auto")
{
std::string format;
std::tie(columns, format) = StorageObjectStorage::resolveSchemaAndFormatFromData(
object_storage, configuration, format_settings, sample_path, context);
configuration->setFormat(format);
}
else
{
chassert(!configuration->getFormat().empty());
columns = StorageObjectStorage::resolveSchemaFromData(object_storage, configuration, format_settings, sample_path, context);
}
}
}
else if (configuration->getFormat() == "auto")
{
configuration->setFormat(StorageObjectStorage::resolveFormatFromData(object_storage, configuration, format_settings, sample_path, context));
}
validateSupportedColumns(columns, *configuration);
}
void validateSupportedColumns(
ColumnsDescription & columns,
const StorageObjectStorageConfiguration & configuration)
{
if (!columns.hasOnlyOrdinary())
{
/// We don't allow special columns.
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Special columns like MATERIALIZED, ALIAS or EPHEMERAL are not supported for {} storage.",
configuration.getTypeName());
}
}
std::string makeAbsolutePath(const std::string & table_location, const std::string & path)
{
if (isAbsolutePath(path))
return path;
auto table_location_decomposed = SchemeAuthorityKey(table_location);
std::string normalized_key = normalizePathToStorageRoot(table_location, path);
if (!table_location_decomposed.scheme.empty())
return table_location_decomposed.scheme + "://" + table_location_decomposed.authority + "/" + normalized_key;
return normalized_key;
}
std::pair<DB::ObjectStoragePtr, std::string> resolveObjectStorageForPath(
const std::string & table_location,
const std::string & path,
const DB::ObjectStoragePtr & base_storage,
SecondaryStorages & secondary_storages,
const DB::ContextPtr & context)
{
if (!isAbsolutePath(path))
return {base_storage, normalizePathToStorageRoot(table_location, path)}; // Relative path definitely goes to base storage
SchemeAuthorityKey table_location_decomposed{table_location};
SchemeAuthorityKey target_decomposed{path};
const std::string base_scheme_normalized = normalizeScheme(table_location_decomposed.scheme);
const std::string target_scheme_normalized = normalizeScheme(target_decomposed.scheme);
// For S3 URIs, use S3::URI to properly handle all kinds of URIs, e.g. https://s3.amazonaws.com/bucket/... == s3://bucket/...
#if USE_AWS_S3
if (target_scheme_normalized == "s3" || target_scheme_normalized == "https" || target_scheme_normalized == "http")
{
std::string normalized_path = path;
if (target_decomposed.scheme == "s3a" || target_decomposed.scheme == "s3n")
{
normalized_path = "s3://" + target_decomposed.authority + "/" + target_decomposed.key;
}
// enable_url_encoding=false, path from metadata must have correct encoding already
S3::URI s3_uri(normalized_path, /*allow_archive_path_syntax*/ false, /*enable_url_encoding*/ false);
std::string key_to_use = s3_uri.key;
bool use_base_storage = false;
if (base_storage->getType() == ObjectStorageType::S3)
{
if (auto s3_storage = std::dynamic_pointer_cast<S3ObjectStorage>(base_storage))
{
const std::string base_bucket = s3_storage->getObjectsNamespace();
const std::string base_endpoint = s3_storage->getDescription();
if (s3URIMatches(s3_uri, base_bucket, base_endpoint, target_scheme_normalized))
use_base_storage = true;
}
}
if (!use_base_storage && (base_scheme_normalized == "s3" || base_scheme_normalized == "https" || base_scheme_normalized == "http"))
{
std::string normalized_table_location = table_location;
if (table_location_decomposed.scheme == "s3a" || table_location_decomposed.scheme == "s3n")
{
normalized_table_location = "s3://" + table_location_decomposed.authority + "/" + table_location_decomposed.key;
}
S3::URI base_s3_uri(normalized_table_location, /*allow_archive_path_syntax*/ false, /*enable_url_encoding*/ false);
if (s3URIMatches(s3_uri, base_s3_uri.bucket, base_s3_uri.endpoint, target_scheme_normalized))
use_base_storage = true;
}
if (use_base_storage)
return {base_storage, key_to_use};
const std::string storage_cache_key = "s3://" + s3_uri.bucket + "@" + (s3_uri.endpoint.empty() ? "amazonaws.com" : s3_uri.endpoint);
return getOrCreateStorageAndKey(
storage_cache_key,
key_to_use,
"s3",
secondary_storages,
context,
[&](Poco::Util::MapConfiguration & cfg, const std::string & config_prefix)
{
// Use the full endpoint or construct it from bucket
std::string endpoint = s3_uri.endpoint.empty()
? ("https://" + s3_uri.bucket + ".s3.amazonaws.com")
: s3_uri.endpoint;
cfg.setString(config_prefix + ".endpoint", endpoint);
// Copy credentials from base storage if it's also S3
if (base_storage->getType() == ObjectStorageType::S3)
{
if (auto s3_storage = std::dynamic_pointer_cast<S3ObjectStorage>(base_storage))
{
if (auto s3_client = s3_storage->tryGetS3StorageClient())
{
const auto credentials = s3_client->getCredentials();
const String & access_key_id = credentials.GetAWSAccessKeyId();
const String & secret_access_key = credentials.GetAWSSecretKey();
const String & session_token = credentials.GetSessionToken();
const String & region = s3_client->getRegion();
if (!access_key_id.empty())
cfg.setString(config_prefix + ".access_key_id", access_key_id);
if (!secret_access_key.empty())
cfg.setString(config_prefix + ".secret_access_key", secret_access_key);
if (!session_token.empty())
cfg.setString(config_prefix + ".session_token", session_token);
if (!region.empty())
cfg.setString(config_prefix + ".region", region);
}
}
}
});
}
#endif
#if USE_HDFS
if (target_scheme_normalized == "hdfs")
{
bool use_base_storage = false;
// Check if base_storage matches (only if it's HDFS)
if (base_storage->getType() == ObjectStorageType::HDFS)
{
if (auto hdfs_storage = std::dynamic_pointer_cast<HDFSObjectStorage>(base_storage))
{
const std::string base_url = hdfs_storage->getDescription();
// Extract endpoint from base URL (hdfs://namenode:port/path -> hdfs://namenode:port)
std::string base_endpoint;
if (auto pos = base_url.find('/', base_url.find("//") + 2); pos != std::string::npos)
base_endpoint = base_url.substr(0, pos);
else
base_endpoint = base_url;
// For HDFS, compare endpoints (namenode addresses)
std::string target_endpoint = target_scheme_normalized + "://" + target_decomposed.authority;
if (base_endpoint == target_endpoint)
use_base_storage = true;
// Also check if table_location matches
if (!use_base_storage && base_scheme_normalized == "hdfs")
{
if (table_location_decomposed.authority == target_decomposed.authority)
use_base_storage = true;
}
}
}
if (use_base_storage)
return {base_storage, target_decomposed.key};
}
#endif
/// Fallback for schemes not handled above (e.g., abfs, file)
if (base_scheme_normalized == target_scheme_normalized && table_location_decomposed.authority == target_decomposed.authority)
return {base_storage, target_decomposed.key};
const std::string cache_key = target_scheme_normalized + "://" + target_decomposed.authority;
const std::string type_for_factory = factoryTypeForScheme(target_scheme_normalized);
if (type_for_factory.empty())
throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Unsupported storage scheme '{}' in path '{}'", target_scheme_normalized, path);
/// Handle storage types that need new storage creation
return getOrCreateStorageAndKey(
cache_key,
target_decomposed.key,
type_for_factory,
secondary_storages,
context,
[&](Poco::Util::MapConfiguration & cfg, const std::string & config_prefix)
{
if (target_scheme_normalized == "file")
{
std::filesystem::path fs_path(target_decomposed.key);
std::filesystem::path parent = fs_path.parent_path();
std::string dir_path = parent.string();
if (dir_path.empty() || dir_path == "/")
dir_path = "/";
else if (dir_path.back() != '/')
dir_path += '/';
cfg.setString(config_prefix + ".path", dir_path);
}
else if (target_scheme_normalized == "abfs")
{
cfg.setString(config_prefix + ".endpoint", target_scheme_normalized + "://" + target_decomposed.authority);
}
else if (target_scheme_normalized == "hdfs")
{
// HDFS endpoint must end with '/'
auto endpoint = target_scheme_normalized + "://" + target_decomposed.authority;
if (!endpoint.empty() && endpoint.back() != '/')
endpoint.push_back('/');
cfg.setString(config_prefix + ".endpoint", endpoint);
}
});
}
}