-
Notifications
You must be signed in to change notification settings - Fork 70
feat: add manifest&manifest list writer #176
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7bf5fca
feat: add manifest&manifest list writer
3f589ac
add v1v2v3 metadata wrapper
fd3650f
fix cpplint
73e115e
fix cpplint
cf01c08
fix comment
9e70336
refactor interface
4cb91b5
fix todo
515c0ad
remove v4
854176a
fix comment
f8072cb
fix cpplint
bdc2300
fix comments
6be5965
fix cpplint
d2978d8
revert cpplinter
baec4f2
fix comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| /// \file iceberg/metadata_adapter.h | ||
| /// Base class of adapter for v1v2v3v4 metadata. | ||
|
|
||
| #include "iceberg/arrow_c_data.h" | ||
| #include "iceberg/result.h" | ||
| #include "iceberg/type_fwd.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| // \brief Base class to append manifest metadata to Arrow array. | ||
| class ICEBERG_EXPORT ManifestAdapter { | ||
| public: | ||
| ManifestAdapter() = default; | ||
| virtual ~ManifestAdapter() = default; | ||
|
|
||
| virtual Status StartAppending() = 0; | ||
| virtual Result<ArrowArray> FinishAppending() = 0; | ||
| int64_t size() const { return size_; } | ||
|
|
||
| protected: | ||
| ArrowArray array_; | ||
| int64_t size_ = 0; | ||
| }; | ||
|
|
||
| // \brief Implemented by different versions with different schemas to | ||
| // append a list of `ManifestEntry`s to an `ArrowArray`. | ||
| class ICEBERG_EXPORT ManifestEntryAdapter : public ManifestAdapter { | ||
| public: | ||
| ManifestEntryAdapter() = default; | ||
| ~ManifestEntryAdapter() override = default; | ||
|
|
||
| virtual Status Append(const ManifestEntry& entry) = 0; | ||
| }; | ||
|
|
||
| // \brief Implemented by different versions with different schemas to | ||
| // append a list of `ManifestFile`s to an `ArrowArray`. | ||
| class ICEBERG_EXPORT ManifestFileAdapter : public ManifestAdapter { | ||
| public: | ||
| ManifestFileAdapter() = default; | ||
| ~ManifestFileAdapter() override = default; | ||
|
|
||
| virtual Status Append(const ManifestFile& file) = 0; | ||
| }; | ||
|
|
||
| } // namespace iceberg | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| /* | ||
| * 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 "iceberg/manifest_writer.h" | ||
|
|
||
| #include "iceberg/manifest_entry.h" | ||
| #include "iceberg/manifest_list.h" | ||
| #include "iceberg/schema.h" | ||
| #include "iceberg/util/macros.h" | ||
| #include "iceberg/v1_metadata.h" | ||
| #include "iceberg/v2_metadata.h" | ||
| #include "iceberg/v3_metadata.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| Status ManifestWriter::Add(const ManifestEntry& entry) { | ||
| if (adapter_->size() >= kBatchSize) { | ||
| ICEBERG_ASSIGN_OR_RAISE(auto array, adapter_->FinishAppending()); | ||
| ICEBERG_RETURN_UNEXPECTED(writer_->Write(array)); | ||
| ICEBERG_RETURN_UNEXPECTED(adapter_->StartAppending()); | ||
| } | ||
| return adapter_->Append(entry); | ||
| } | ||
|
|
||
| Status ManifestWriter::AddAll(const std::vector<ManifestEntry>& entries) { | ||
| for (const auto& entry : entries) { | ||
| ICEBERG_RETURN_UNEXPECTED(Add(entry)); | ||
| } | ||
| return {}; | ||
| } | ||
|
|
||
| Status ManifestWriter::Close() { | ||
| if (adapter_->size() > 0) { | ||
| ICEBERG_ASSIGN_OR_RAISE(auto array, adapter_->FinishAppending()); | ||
| ICEBERG_RETURN_UNEXPECTED(writer_->Write(array)); | ||
| } | ||
| return {}; | ||
| } | ||
|
|
||
| Result<std::unique_ptr<Writer>> OpenFileWriter(std::string_view location, | ||
| std::shared_ptr<Schema> schema, | ||
| std::shared_ptr<FileIO> file_io) { | ||
| ICEBERG_ASSIGN_OR_RAISE( | ||
| auto writer, | ||
| WriterFactoryRegistry::Open(FileFormatType::kAvro, {.path = std::string(location), | ||
| .schema = std::move(schema), | ||
| .io = std::move(file_io)})); | ||
| return writer; | ||
| } | ||
|
|
||
| Result<std::unique_ptr<ManifestWriter>> ManifestWriter::MakeV1Writer( | ||
| std::optional<int64_t> snapshot_id, std::string_view manifest_location, | ||
| std::shared_ptr<FileIO> file_io, std::shared_ptr<Schema> partition_schema) { | ||
| // TODO(xiao.dong) parse v1 schema | ||
| auto manifest_entry_schema = | ||
| ManifestEntry::TypeFromPartitionType(std::move(partition_schema)); | ||
| auto fields_span = manifest_entry_schema->fields(); | ||
| std::vector<SchemaField> fields(fields_span.begin(), fields_span.end()); | ||
| auto schema = std::make_shared<Schema>(fields); | ||
| ICEBERG_ASSIGN_OR_RAISE(auto writer, | ||
| OpenFileWriter(manifest_location, schema, std::move(file_io))); | ||
| auto adapter = std::make_unique<ManifestEntryAdapterV1>(snapshot_id, std::move(schema)); | ||
| return std::make_unique<ManifestWriter>(std::move(writer), std::move(adapter)); | ||
| } | ||
|
|
||
| Result<std::unique_ptr<ManifestWriter>> ManifestWriter::MakeV2Writer( | ||
| std::optional<int64_t> snapshot_id, std::string_view manifest_location, | ||
| std::shared_ptr<FileIO> file_io, std::shared_ptr<Schema> partition_schema) { | ||
| // TODO(xiao.dong) parse v2 schema | ||
| auto manifest_entry_schema = | ||
| ManifestEntry::TypeFromPartitionType(std::move(partition_schema)); | ||
| auto fields_span = manifest_entry_schema->fields(); | ||
| std::vector<SchemaField> fields(fields_span.begin(), fields_span.end()); | ||
| auto schema = std::make_shared<Schema>(fields); | ||
| ICEBERG_ASSIGN_OR_RAISE(auto writer, | ||
| OpenFileWriter(manifest_location, schema, std::move(file_io))); | ||
| auto adapter = std::make_unique<ManifestEntryAdapterV2>(snapshot_id, std::move(schema)); | ||
| return std::make_unique<ManifestWriter>(std::move(writer), std::move(adapter)); | ||
| } | ||
|
|
||
| Result<std::unique_ptr<ManifestWriter>> ManifestWriter::MakeV3Writer( | ||
| std::optional<int64_t> snapshot_id, std::optional<int64_t> first_row_id, | ||
| std::string_view manifest_location, std::shared_ptr<FileIO> file_io, | ||
| std::shared_ptr<Schema> partition_schema) { | ||
| // TODO(xiao.dong) parse v3 schema | ||
| auto manifest_entry_schema = | ||
| ManifestEntry::TypeFromPartitionType(std::move(partition_schema)); | ||
| auto fields_span = manifest_entry_schema->fields(); | ||
| std::vector<SchemaField> fields(fields_span.begin(), fields_span.end()); | ||
| auto schema = std::make_shared<Schema>(fields); | ||
|
Member
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. nit: you may use |
||
| ICEBERG_ASSIGN_OR_RAISE(auto writer, | ||
| OpenFileWriter(manifest_location, schema, std::move(file_io))); | ||
| auto adapter = std::make_unique<ManifestEntryAdapterV3>(snapshot_id, first_row_id, | ||
| std::move(schema)); | ||
| return std::make_unique<ManifestWriter>(std::move(writer), std::move(adapter)); | ||
| } | ||
|
|
||
| Status ManifestListWriter::Add(const ManifestFile& file) { | ||
| if (adapter_->size() >= kBatchSize) { | ||
| ICEBERG_ASSIGN_OR_RAISE(auto array, adapter_->FinishAppending()); | ||
| ICEBERG_RETURN_UNEXPECTED(writer_->Write(array)); | ||
| ICEBERG_RETURN_UNEXPECTED(adapter_->StartAppending()); | ||
| } | ||
| return adapter_->Append(file); | ||
| } | ||
|
|
||
| Status ManifestListWriter::AddAll(const std::vector<ManifestFile>& files) { | ||
| for (const auto& file : files) { | ||
| ICEBERG_RETURN_UNEXPECTED(Add(file)); | ||
| } | ||
| return {}; | ||
| } | ||
|
|
||
| Status ManifestListWriter::Close() { | ||
| if (adapter_->size() > 0) { | ||
| ICEBERG_ASSIGN_OR_RAISE(auto array, adapter_->FinishAppending()); | ||
| ICEBERG_RETURN_UNEXPECTED(writer_->Write(array)); | ||
| } | ||
| return {}; | ||
| } | ||
|
|
||
| Result<std::unique_ptr<ManifestListWriter>> ManifestListWriter::MakeV1Writer( | ||
| int64_t snapshot_id, std::optional<int64_t> parent_snapshot_id, | ||
| std::string_view manifest_list_location, std::shared_ptr<FileIO> file_io) { | ||
| // TODO(xiao.dong) parse v1 schema | ||
| std::vector<SchemaField> fields(ManifestFile::Type().fields().begin(), | ||
| ManifestFile::Type().fields().end()); | ||
| auto schema = std::make_shared<Schema>(fields); | ||
| ICEBERG_ASSIGN_OR_RAISE( | ||
| auto writer, OpenFileWriter(manifest_list_location, schema, std::move(file_io))); | ||
| auto adapter = std::make_unique<ManifestFileAdapterV1>(snapshot_id, parent_snapshot_id, | ||
| std::move(schema)); | ||
| return std::make_unique<ManifestListWriter>(std::move(writer), std::move(adapter)); | ||
| } | ||
|
|
||
| Result<std::unique_ptr<ManifestListWriter>> ManifestListWriter::MakeV2Writer( | ||
| int64_t snapshot_id, std::optional<int64_t> parent_snapshot_id, | ||
| int64_t sequence_number, std::string_view manifest_list_location, | ||
| std::shared_ptr<FileIO> file_io) { | ||
| // TODO(xiao.dong) parse v2 schema | ||
| std::vector<SchemaField> fields(ManifestFile::Type().fields().begin(), | ||
| ManifestFile::Type().fields().end()); | ||
| auto schema = std::make_shared<Schema>(fields); | ||
| ICEBERG_ASSIGN_OR_RAISE( | ||
| auto writer, OpenFileWriter(manifest_list_location, schema, std::move(file_io))); | ||
| auto adapter = std::make_unique<ManifestFileAdapterV2>( | ||
| snapshot_id, parent_snapshot_id, sequence_number, std::move(schema)); | ||
| return std::make_unique<ManifestListWriter>(std::move(writer), std::move(adapter)); | ||
| } | ||
|
|
||
| Result<std::unique_ptr<ManifestListWriter>> ManifestListWriter::MakeV3Writer( | ||
| int64_t snapshot_id, std::optional<int64_t> parent_snapshot_id, | ||
| int64_t sequence_number, std::optional<int64_t> first_row_id, | ||
| std::string_view manifest_list_location, std::shared_ptr<FileIO> file_io) { | ||
| // TODO(xiao.dong) parse v3 schema | ||
| std::vector<SchemaField> fields(ManifestFile::Type().fields().begin(), | ||
| ManifestFile::Type().fields().end()); | ||
| auto schema = std::make_shared<Schema>(fields); | ||
|
Member
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. ditto, you may use FromStructType. |
||
| ICEBERG_ASSIGN_OR_RAISE( | ||
| auto writer, OpenFileWriter(manifest_list_location, schema, std::move(file_io))); | ||
| auto adapter = std::make_unique<ManifestFileAdapterV3>( | ||
| snapshot_id, parent_snapshot_id, sequence_number, first_row_id, std::move(schema)); | ||
| return std::make_unique<ManifestListWriter>(std::move(writer), std::move(adapter)); | ||
| } | ||
|
|
||
| } // namespace iceberg | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.