Skip to content

Commit bfdb598

Browse files
authored
Merge pull request #7 from redpanda-data/feat/glue-metadata-fallback
feat(rest): fall back to storage metadata when inline metadata is unusable
2 parents aa38942 + 1c66ebf commit bfdb598

4 files changed

Lines changed: 67 additions & 18 deletions

File tree

‎src/iceberg/catalog/rest/json_serde.cc‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,16 @@ Result<LoadTableResult> LoadTableResultFromJson(const nlohmann::json& json) {
731731
GetJsonValueOrDefault<std::string>(json, kMetadataLocation));
732732
ICEBERG_ASSIGN_OR_RAISE(auto metadata_json,
733733
GetJsonValue<nlohmann::json>(json, kMetadata));
734-
ICEBERG_ASSIGN_OR_RAISE(result.metadata, TableMetadataFromJson(metadata_json));
734+
// Some catalogs (AWS Glue) return inline metadata that omits required
735+
// fields such as "schemas". When the response carries a metadata-location,
736+
// leave metadata null and let the caller load the complete metadata file
737+
// from storage instead of failing the whole request.
738+
auto metadata = TableMetadataFromJson(metadata_json);
739+
if (metadata.has_value()) {
740+
result.metadata = std::move(metadata.value());
741+
} else if (result.metadata_location.empty()) {
742+
return std::unexpected<Error>(metadata.error());
743+
}
735744
ICEBERG_ASSIGN_OR_RAISE(result.config,
736745
GetJsonValueOrDefault<decltype(result.config)>(json, kConfig));
737746
if (auto it = json.find(kStorageCredentials); it != json.end() && !it->is_null()) {
@@ -984,7 +993,11 @@ Result<CommitTableResponse> CommitTableResponseFromJson(const nlohmann::json& js
984993
GetJsonValue<std::string>(json, kMetadataLocation));
985994
ICEBERG_ASSIGN_OR_RAISE(auto metadata_json,
986995
GetJsonValue<nlohmann::json>(json, kMetadata));
987-
ICEBERG_ASSIGN_OR_RAISE(response.metadata, TableMetadataFromJson(metadata_json));
996+
// Tolerate unusable inline metadata (AWS Glue omits "schemas"); the
997+
// metadata-location above is required, so the caller can reload from it.
998+
if (auto metadata = TableMetadataFromJson(metadata_json); metadata.has_value()) {
999+
response.metadata = std::move(metadata.value());
1000+
}
9881001
ICEBERG_RETURN_UNEXPECTED(response.Validate());
9891002
return response;
9901003
}

‎src/iceberg/catalog/rest/rest_catalog.cc‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "iceberg/schema.h"
4747
#include "iceberg/sort_order.h"
4848
#include "iceberg/table.h"
49+
#include "iceberg/table_metadata.h"
4950
#include "iceberg/table_requirement.h"
5051
#include "iceberg/table_requirements.h"
5152
#include "iceberg/table_update.h"
@@ -747,6 +748,12 @@ Result<std::shared_ptr<Transaction>> RestCatalog::StageCreateTable(
747748
auto storage_credentials = std::move(result.storage_credentials);
748749
ICEBERG_ASSIGN_OR_RAISE(auto table_io,
749750
TableFileIO(context, table_config, storage_credentials));
751+
if (!result.metadata) {
752+
// The inline metadata was unusable (e.g. AWS Glue omits "schemas");
753+
// load the complete metadata file pointed to by metadata-location.
754+
ICEBERG_ASSIGN_OR_RAISE(result.metadata,
755+
TableMetadataUtil::Read(*table_io, result.metadata_location));
756+
}
750757
ICEBERG_ASSIGN_OR_RAISE(
751758
auto table_session,
752759
TableAuthSession(identifier, table_config, std::move(contextual_session)));
@@ -863,6 +870,12 @@ Result<std::shared_ptr<Table>> RestCatalog::MakeTableFromLoadResult(
863870
auto storage_credentials = std::move(result.storage_credentials);
864871
ICEBERG_ASSIGN_OR_RAISE(auto table_io,
865872
TableFileIO(context, table_config, storage_credentials));
873+
if (!result.metadata) {
874+
// The inline metadata was unusable (e.g. AWS Glue omits "schemas");
875+
// load the complete metadata file pointed to by metadata-location.
876+
ICEBERG_ASSIGN_OR_RAISE(result.metadata,
877+
TableMetadataUtil::Read(*table_io, result.metadata_location));
878+
}
866879
ICEBERG_ASSIGN_OR_RAISE(
867880
auto table_session,
868881
TableAuthSession(identifier, table_config, std::move(contextual_session)));
@@ -878,6 +891,13 @@ Result<std::shared_ptr<Table>> RestCatalog::MakeTableFromCommitResponse(
878891
const SessionContext& context,
879892
const std::unordered_map<std::string, std::string>& table_config,
880893
std::shared_ptr<auth::AuthSession> table_session, std::shared_ptr<FileIO> table_io) {
894+
if (!response.metadata) {
895+
// The inline metadata was unusable (e.g. AWS Glue omits "schemas");
896+
// load the complete metadata file pointed to by metadata-location.
897+
ICEBERG_ASSIGN_OR_RAISE(
898+
response.metadata,
899+
TableMetadataUtil::Read(*table_io, response.metadata_location));
900+
}
881901
// Reuse the bound FileIO because commit responses carry no config or credentials.
882902
auto table_catalog = std::make_shared<TableScopedCatalog>(
883903
shared_from_this(), context, identifier, table_config, table_session, table_io);

‎src/iceberg/catalog/rest/types.h‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,18 @@ using PageToken = std::string;
184184
/// \brief Result body for table create/load/register APIs.
185185
struct ICEBERG_REST_EXPORT LoadTableResult {
186186
std::string metadata_location;
187-
std::shared_ptr<TableMetadata> metadata; // required
187+
/// \brief Required by the spec, but may be null when the inline metadata
188+
/// was unusable and metadata_location is set; the catalog then loads the
189+
/// metadata file from storage.
190+
std::shared_ptr<TableMetadata> metadata;
188191
std::unordered_map<std::string, std::string> config;
189192
/// \brief Vended storage credentials, one per URI prefix; empty if none.
190193
std::vector<StorageCredential> storage_credentials;
191194

192195
/// \brief Validates the LoadTableResult.
193196
Status Validate() const {
194-
if (!metadata) {
195-
return ValidationFailed("Invalid metadata: null");
197+
if (!metadata && metadata_location.empty()) {
198+
return ValidationFailed("Invalid metadata: null and no metadata-location");
196199
}
197200
for (const auto& credential : storage_credentials) {
198201
ICEBERG_RETURN_UNEXPECTED(credential.Validate());
@@ -279,17 +282,16 @@ struct ICEBERG_REST_EXPORT CommitTableRequest {
279282

280283
/// \brief Response from committing changes to a table.
281284
struct ICEBERG_REST_EXPORT CommitTableResponse {
282-
std::string metadata_location; // required
283-
std::shared_ptr<TableMetadata> metadata; // required
285+
std::string metadata_location; // required
286+
/// \brief Required by the spec, but may be null when the inline metadata
287+
/// was unusable; the catalog then loads the metadata file from storage.
288+
std::shared_ptr<TableMetadata> metadata;
284289

285290
/// \brief Validates the CommitTableResponse.
286291
Status Validate() const {
287292
if (metadata_location.empty()) {
288293
return ValidationFailed("Invalid metadata location: empty");
289294
}
290-
if (!metadata) {
291-
return ValidationFailed("Invalid metadata: null");
292-
}
293295
return {};
294296
}
295297

‎src/iceberg/test/rest_json_serde_test.cc‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,17 @@ INSTANTIATE_TEST_SUITE_P(
11621162
.json_str =
11631163
R"({"metadata":{"format-version":2,"table-uuid":"test-uuid-1234","location":"s3://bucket/test","last-sequence-number":0,"last-updated-ms":0,"last-column-id":1,"schemas":[{"type":"struct","schema-id":1,"fields":[{"id":1,"name":"id","type":"int","required":true}]}],"current-schema-id":1,"partition-specs":[{"spec-id":0,"fields":[]}],"default-spec-id":0,"last-partition-id":0,"sort-orders":[{"order-id":0,"fields":[]}],"default-sort-order-id":0,"properties":{}},"config":{"warehouse":"s3://bucket/warehouse"}})",
11641164
.expected_model = {.metadata = MakeSimpleTableMetadata(),
1165-
.config = {{"warehouse", "s3://bucket/warehouse"}}}}),
1165+
.config = {{"warehouse", "s3://bucket/warehouse"}}}},
1166+
// Unusable inline metadata (e.g. AWS Glue returns metadata that is not
1167+
// an object, or omits required fields). Because a metadata-location is
1168+
// present, parsing succeeds with null metadata so the catalog can load
1169+
// the complete metadata file from storage.
1170+
LoadTableResultDeserializeParam{
1171+
.test_name = "UnusableInlineMetadata",
1172+
.json_str =
1173+
R"({"metadata-location":"s3://bucket/metadata/v1.json","metadata":"invalid"})",
1174+
.expected_model = {.metadata_location = "s3://bucket/metadata/v1.json",
1175+
.metadata = nullptr}}),
11661176
[](const ::testing::TestParamInfo<LoadTableResultDeserializeParam>& info) {
11671177
return info.param.test_name;
11681178
});
@@ -1400,7 +1410,17 @@ INSTANTIATE_TEST_SUITE_P(
14001410
.json_str =
14011411
R"({"metadata-location":"s3://bucket/metadata/v2.json","metadata":{"format-version":2,"table-uuid":"test-uuid-1234","location":"s3://bucket/test","last-sequence-number":0,"last-updated-ms":0,"last-column-id":1,"schemas":[{"type":"struct","schema-id":1,"fields":[{"id":1,"name":"id","type":"int","required":true}]}],"current-schema-id":1,"partition-specs":[{"spec-id":0,"fields":[]}],"default-spec-id":0,"last-partition-id":0,"sort-orders":[{"order-id":0,"fields":[]}],"default-sort-order-id":0,"properties":{}}})",
14021412
.expected_model = {.metadata_location = "s3://bucket/metadata/v2.json",
1403-
.metadata = MakeSimpleTableMetadata()}}),
1413+
.metadata = MakeSimpleTableMetadata()}},
1414+
// Unusable inline metadata (e.g. AWS Glue returns metadata that is not
1415+
// an object, or omits required fields). Because a metadata-location is
1416+
// present, parsing succeeds with null metadata so the catalog can load
1417+
// the complete metadata file from storage.
1418+
CommitTableResponseDeserializeParam{
1419+
.test_name = "UnusableInlineMetadata",
1420+
.json_str =
1421+
R"({"metadata-location":"s3://bucket/metadata/v2.json","metadata":"invalid"})",
1422+
.expected_model = {.metadata_location = "s3://bucket/metadata/v2.json",
1423+
.metadata = nullptr}}),
14041424
[](const ::testing::TestParamInfo<CommitTableResponseDeserializeParam>& info) {
14051425
return info.param.test_name;
14061426
});
@@ -1433,12 +1453,6 @@ INSTANTIATE_TEST_SUITE_P(
14331453
.invalid_json_str =
14341454
R"({"metadata-location":123,"metadata":{"format-version":2,"table-uuid":"test","location":"s3://test","last-sequence-number":0,"last-column-id":1,"last-updated-ms":0,"schemas":[{"type":"struct","schema-id":1,"fields":[{"id":1,"name":"id","type":"int","required":true}]}],"current-schema-id":1,"partition-specs":[{"spec-id":0,"fields":[]}],"default-spec-id":0,"last-partition-id":0,"sort-orders":[{"order-id":0,"fields":[]}],"default-sort-order-id":0}})",
14351455
.expected_error_message = "type must be string, but is number"},
1436-
// Wrong type for metadata field
1437-
CommitTableResponseInvalidParam{
1438-
.test_name = "WrongMetadataType",
1439-
.invalid_json_str =
1440-
R"({"metadata-location":"s3://bucket/metadata/v2.json","metadata":"invalid"})",
1441-
.expected_error_message = "Cannot parse metadata from a non-object"},
14421456
// Empty JSON object
14431457
CommitTableResponseInvalidParam{
14441458
.test_name = "EmptyJson",

0 commit comments

Comments
 (0)