-
Notifications
You must be signed in to change notification settings - Fork 70
feat: add table metadata reader and writer #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,10 +23,17 @@ | |
| #include <ranges> | ||
| #include <string> | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
|
|
||
| #include "iceberg/file_io.h" | ||
| #include "iceberg/json_internal.h" | ||
| #include "iceberg/partition_spec.h" | ||
| #include "iceberg/result.h" | ||
| #include "iceberg/schema.h" | ||
| #include "iceberg/snapshot.h" | ||
| #include "iceberg/sort_order.h" | ||
| #include "iceberg/util/macros.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| std::string ToString(const SnapshotLogEntry& entry) { | ||
|
|
@@ -69,4 +76,100 @@ Result<std::shared_ptr<SortOrder>> TableMetadata::SortOrder() const { | |
| return *iter; | ||
| } | ||
|
|
||
| namespace { | ||
|
|
||
| template <typename T> | ||
| bool SharedPtrVectorEquals(const std::vector<std::shared_ptr<T>>& lhs, | ||
| const std::vector<std::shared_ptr<T>>& rhs) { | ||
| if (lhs.size() != rhs.size()) { | ||
| return false; | ||
| } | ||
| for (size_t i = 0; i < lhs.size(); ++i) { | ||
| if (*lhs[i] != *rhs[i]) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be null pointer?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be null if there is something wrong. Since |
||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool SnapshotRefEquals( | ||
| const std::unordered_map<std::string, std::shared_ptr<SnapshotRef>>& lhs, | ||
| const std::unordered_map<std::string, std::shared_ptr<SnapshotRef>>& rhs) { | ||
| if (lhs.size() != rhs.size()) { | ||
| return false; | ||
| } | ||
| for (const auto& [key, value] : lhs) { | ||
| auto iter = rhs.find(key); | ||
| if (iter == rhs.end()) { | ||
| return false; | ||
| } | ||
| if (*iter->second != *value) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| bool operator==(const TableMetadata& lhs, const TableMetadata& rhs) { | ||
| return lhs.format_version == rhs.format_version && lhs.table_uuid == rhs.table_uuid && | ||
| lhs.location == rhs.location && | ||
| lhs.last_sequence_number == rhs.last_sequence_number && | ||
| lhs.last_updated_ms == rhs.last_updated_ms && | ||
| lhs.last_column_id == rhs.last_column_id && | ||
| lhs.current_schema_id == rhs.current_schema_id && | ||
| SharedPtrVectorEquals(lhs.schemas, rhs.schemas) && | ||
| lhs.default_spec_id == rhs.default_spec_id && | ||
| lhs.last_partition_id == rhs.last_partition_id && | ||
| lhs.properties == rhs.properties && | ||
| lhs.current_snapshot_id == rhs.current_snapshot_id && | ||
| SharedPtrVectorEquals(lhs.snapshots, rhs.snapshots) && | ||
| lhs.snapshot_log == rhs.snapshot_log && lhs.metadata_log == rhs.metadata_log && | ||
| SharedPtrVectorEquals(lhs.sort_orders, rhs.sort_orders) && | ||
| lhs.default_sort_order_id == rhs.default_sort_order_id && | ||
| SnapshotRefEquals(lhs.refs, rhs.refs) && | ||
| SharedPtrVectorEquals(lhs.statistics, rhs.statistics) && | ||
| SharedPtrVectorEquals(lhs.partition_statistics, rhs.partition_statistics) && | ||
| lhs.next_row_id == rhs.next_row_id; | ||
| } | ||
|
|
||
| Result<MetadataFileCodecType> TableMetadataUtil::CodecFromFileName( | ||
| std::string_view file_name) { | ||
| if (file_name.find(".metadata.json") == std::string::npos) { | ||
| return InvalidArgument("{} is not a valid metadata file", file_name); | ||
| } | ||
|
|
||
| // We have to be backward-compatible with .metadata.json.gz files | ||
| if (file_name.ends_with(".metadata.json.gz")) { | ||
| return MetadataFileCodecType::kGzip; | ||
| } | ||
|
|
||
| std::string_view file_name_without_suffix = | ||
| file_name.substr(0, file_name.find_last_of(".metadata.json")); | ||
| if (file_name_without_suffix.ends_with(".gz")) { | ||
| return MetadataFileCodecType::kGzip; | ||
| } | ||
| return MetadataFileCodecType::kNone; | ||
| } | ||
|
|
||
| Result<std::unique_ptr<TableMetadata>> TableMetadataUtil::Read( | ||
| FileIO& io, const std::string& location, std::optional<size_t> length) { | ||
| ICEBERG_ASSIGN_OR_RAISE(auto codec_type, CodecFromFileName(location)); | ||
| if (codec_type == MetadataFileCodecType::kGzip) { | ||
| return NotImplemented("Reading gzip-compressed metadata files is not supported yet"); | ||
| } | ||
|
|
||
| ICEBERG_ASSIGN_OR_RAISE(auto content, io.ReadFile(location, length)); | ||
| ICEBERG_ASSIGN_OR_RAISE(auto json, FromJsonString(content)); | ||
| return TableMetadataFromJson(json); | ||
| } | ||
|
|
||
| Status TableMetadataUtil::Write(FileIO& io, const std::string& location, | ||
| const TableMetadata& metadata) { | ||
| auto json = ToJson(metadata); | ||
| ICEBERG_ASSIGN_OR_RAISE(auto json_string, ToJsonString(json)); | ||
| return io.WriteFile(location, json_string); | ||
| } | ||
|
|
||
| } // namespace iceberg | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #include <arrow/filesystem/localfs.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "iceberg/arrow/arrow_fs_file_io.h" | ||
| #include "iceberg/file_io.h" | ||
| #include "iceberg/schema.h" | ||
| #include "iceberg/snapshot.h" | ||
| #include "iceberg/table_metadata.h" | ||
| #include "matchers.h" | ||
| #include "temp_file_test_base.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| class MetadataIOTest : public TempFileTestBase { | ||
| protected: | ||
| void SetUp() override { | ||
| TempFileTestBase::SetUp(); | ||
| io_ = std::make_shared<iceberg::arrow::ArrowFileSystemFileIO>( | ||
| std::make_shared<::arrow::fs::LocalFileSystem>()); | ||
| temp_filepath_ = CreateNewTempFilePathWithSuffix(".metadata.json"); | ||
| } | ||
|
|
||
| std::shared_ptr<iceberg::FileIO> io_; | ||
| std::string temp_filepath_; | ||
| }; | ||
|
|
||
| TEST_F(MetadataIOTest, ReadWriteMetadata) { | ||
| std::vector<SchemaField> schema_fields; | ||
| schema_fields.emplace_back(/*field_id=*/1, "x", std::make_shared<LongType>(), | ||
| /*optional=*/false); | ||
| auto schema = std::make_shared<Schema>(std::move(schema_fields), /*schema_id=*/1); | ||
|
|
||
| TableMetadata metadata{.format_version = 1, | ||
| .table_uuid = "1234567890", | ||
| .location = "s3://bucket/path", | ||
| .last_sequence_number = 0, | ||
| .schemas = {schema}, | ||
| .current_schema_id = 1, | ||
| .default_spec_id = 0, | ||
| .last_partition_id = 0, | ||
| .properties = {{"key", "value"}}, | ||
| .current_snapshot_id = 3051729675574597004, | ||
| .snapshots = {std::make_shared<Snapshot>(Snapshot{ | ||
| .snapshot_id = 3051729675574597004, | ||
| .sequence_number = 0, | ||
| .timestamp_ms = TimePointMsFromUnixMs(1515100955770).value(), | ||
| .manifest_list = "s3://a/b/1.avro", | ||
| .summary = {{"operation", "append"}}, | ||
| })}, | ||
| .default_sort_order_id = 0, | ||
| .next_row_id = 0}; | ||
|
|
||
| EXPECT_THAT(TableMetadataUtil::Write(*io_, temp_filepath_, metadata), IsOk()); | ||
|
|
||
| auto result = TableMetadataUtil::Read(*io_, temp_filepath_); | ||
| EXPECT_THAT(result, IsOk()); | ||
|
|
||
| auto metadata_read = std::move(result.value()); | ||
| EXPECT_EQ(*metadata_read, metadata); | ||
| } | ||
|
|
||
| } // namespace iceberg |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:should we only catch json::parse_error exception?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that the official documentation says it only throws
parse_error. I just want to be safe enough to catch all just in case. Or we can add a special branch forparse_errorfor better error message.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also call an overload that doesn't throw an exception, and explicitly check whether the resulting JSON object is
discarded()(IIRC)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've created #87 to track it.