forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathICatalog.cpp
More file actions
271 lines (221 loc) · 8.36 KB
/
ICatalog.cpp
File metadata and controls
271 lines (221 loc) · 8.36 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
#include <Databases/DataLake/ICatalog.h>
#include <Common/Exception.h>
#include <Common/logger_useful.h>
#include <Poco/String.h>
#include <filesystem>
#include <Common/FailPoint.h>
namespace DB::ErrorCodes
{
extern const int NOT_IMPLEMENTED;
extern const int LOGICAL_ERROR;
extern const int BAD_ARGUMENTS;
}
namespace DB::FailPoints
{
extern const char database_iceberg_gcs[];
}
namespace DataLake
{
StorageType parseStorageTypeFromLocation(const std::string & location)
{
/// Table location in catalog metadata always starts with one of s3://, file://, etc.
/// So just extract this part of the path and deduce storage type from it.
auto pos = location.find("://");
if (pos == std::string::npos)
{
throw DB::Exception(
DB::ErrorCodes::NOT_IMPLEMENTED,
"Unexpected path format: {}", location);
}
return parseStorageTypeFromString(location.substr(0, pos));
}
StorageType parseStorageTypeFromString(const std::string & type)
{
auto capitalize_first_letter = [] (const std::string & s)
{
auto result = Poco::toLower(s);
if (!result.empty())
result[0] = std::toupper(result[0]);
return result;
};
std::string storage_type_str = type;
auto pos = storage_type_str.find("://");
if (pos != std::string::npos)
{
// convert s3://, file://, etc. to s3, file etc.
storage_type_str = storage_type_str.substr(0,pos);
}
if (capitalize_first_letter(storage_type_str) == "File")
storage_type_str = "Local";
else if (capitalize_first_letter(storage_type_str) == "S3a" || storage_type_str == "oss" || storage_type_str == "gs")
{
fiu_do_on(DB::FailPoints::database_iceberg_gcs,
{
throw DB::Exception(DB::ErrorCodes::BAD_ARGUMENTS, "Google cloud storage converts to S3");
});
storage_type_str = "S3";
}
auto storage_type = magic_enum::enum_cast<StorageType>(capitalize_first_letter(storage_type_str));
if (!storage_type)
{
throw DB::Exception(
DB::ErrorCodes::NOT_IMPLEMENTED,
"Unsupported storage type: {}", storage_type_str);
}
return *storage_type;
}
void TableMetadata::setLocation(const std::string & location_)
{
if (!with_location)
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Data location was not requested");
/// Location has format:
/// s3://<bucket>/path/to/table/data.
/// We want to split s3://<bucket> and path/to/table/data.
auto pos = location_.find("://");
if (pos == std::string::npos)
throw DB::Exception(DB::ErrorCodes::NOT_IMPLEMENTED, "Unexpected location format: {}", location_);
// pos+3 to account for ://
storage_type_str = location_.substr(0, pos+3);
auto pos_to_bucket = pos + std::strlen("://");
auto pos_to_path = location_.substr(pos_to_bucket).find('/');
if (pos_to_path == std::string::npos)
{ // empty path
location_without_path = location_;
path.clear();
bucket = location_.substr(pos_to_bucket);
}
else
{
pos_to_path = pos_to_bucket + pos_to_path;
location_without_path = location_.substr(0, pos_to_path);
path = location_.substr(pos_to_path + 1);
bucket = location_.substr(pos_to_bucket, pos_to_path - pos_to_bucket);
}
LOG_TEST(getLogger("TableMetadata"),
"Parsed location without path: {}, path: {}",
location_without_path, path);
}
std::string TableMetadata::getLocation() const
{
if (!with_location)
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Data location was not requested");
if (!endpoint.empty())
return constructLocation(endpoint);
return std::filesystem::path(location_without_path) / path;
}
std::string TableMetadata::getLocationWithEndpoint(const std::string & endpoint_) const
{
if (!with_location)
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Data location was not requested");
if (endpoint_.empty())
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Passed endpoint is empty");
return constructLocation(endpoint_);
}
std::string TableMetadata::constructLocation(const std::string & endpoint_) const
{
std::string location = endpoint_;
if (location.ends_with('/'))
location.pop_back();
if (location.ends_with(bucket))
return std::filesystem::path(location) / path / "";
return std::filesystem::path(location) / bucket / path / "";
}
void TableMetadata::setEndpoint(const std::string & endpoint_)
{
endpoint = endpoint_;
}
void TableMetadata::setSchema(const DB::NamesAndTypesList & schema_)
{
if (!with_schema)
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Data schema was not requested");
schema = schema_;
}
const DB::NamesAndTypesList & TableMetadata::getSchema() const
{
if (!with_schema)
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Data schema was not requested");
return schema;
}
void TableMetadata::setStorageCredentials(std::shared_ptr<IStorageCredentials> credentials_)
{
if (!with_storage_credentials)
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Storage credentials were not requested");
storage_credentials = std::move(credentials_);
}
std::shared_ptr<IStorageCredentials> TableMetadata::getStorageCredentials() const
{
if (!with_storage_credentials)
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Data schema was not requested");
return storage_credentials;
}
void TableMetadata::setDataLakeSpecificProperties(std::optional<DataLakeSpecificProperties> && metadata)
{
data_lake_specific_metadata = metadata;
}
std::optional<DataLakeSpecificProperties> TableMetadata::getDataLakeSpecificProperties() const
{
return data_lake_specific_metadata;
}
StorageType TableMetadata::getStorageType() const
{
if (!storage_type_str.empty())
{
return parseStorageTypeFromString(storage_type_str);
}
return parseStorageTypeFromLocation(location_without_path);
}
bool TableMetadata::hasLocation() const
{
return !location_without_path.empty();
}
bool TableMetadata::hasSchema() const
{
return !schema.empty();
}
bool TableMetadata::hasStorageCredentials() const
{
return storage_credentials != nullptr;
}
std::string TableMetadata::getMetadataLocation(const std::string & iceberg_metadata_file_location) const
{
std::string metadata_location = iceberg_metadata_file_location;
if (!metadata_location.empty())
{
std::string data_location = getLocation();
// Use the actual storage type prefix (e.g., s3://, file://, etc.)
if (metadata_location.starts_with(storage_type_str))
metadata_location = metadata_location.substr(storage_type_str.size());
if (data_location.starts_with(storage_type_str))
data_location = data_location.substr(storage_type_str.size());
else if (!endpoint.empty() && data_location.starts_with(endpoint))
data_location = data_location.substr(endpoint.size());
if (metadata_location.starts_with(data_location))
{
size_t remove_slash = metadata_location[data_location.size()] == '/' ? 1 : 0;
metadata_location = metadata_location.substr(data_location.size() + remove_slash);
}
}
return metadata_location;
}
DB::SettingsChanges CatalogSettings::allChanged() const
{
DB::SettingsChanges changes;
changes.emplace_back("storage_endpoint", storage_endpoint);
changes.emplace_back("aws_access_key_id", aws_access_key_id);
changes.emplace_back("aws_secret_access_key", aws_secret_access_key);
changes.emplace_back("region", region);
return changes;
}
void ICatalog::createTable(const String & /*namespace_name*/, const String & /*table_name*/, const String & /*new_metadata_path*/, Poco::JSON::Object::Ptr /*metadata_content*/) const
{
throw DB::Exception(DB::ErrorCodes::NOT_IMPLEMENTED, "createTable is not implemented");
}
bool ICatalog::updateMetadata(const String & /*namespace_name*/, const String & /*table_name*/, const String & /*new_metadata_path*/, Poco::JSON::Object::Ptr /*new_snapshot*/) const
{
throw DB::Exception(DB::ErrorCodes::NOT_IMPLEMENTED, "updateMetadata is not implemented");
}
void ICatalog::dropTable(const String & /*namespace_name*/, const String & /*table_name*/) const
{
throw DB::Exception(DB::ErrorCodes::NOT_IMPLEMENTED, "dropTable is not implemented");
}
}