-
Notifications
You must be signed in to change notification settings - Fork 100
feat: add FastAppend #516
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
feat: add FastAppend #516
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
141eadc
feat: add FastAppend
zhjwpku 7cfc0e9
fix: review comments
zhjwpku 610cf80
Make DataFileSet preserve insertion order for v3 row ID assignment
zhjwpku 004a162
fix: windows build error
zhjwpku 310fae7
fix: move Set to SnapshotUpdate
zhjwpku 7118dcc
fix: more review comments
zhjwpku 04fb4a4
fix: windows build failure
zhjwpku e2e216d
attempt to fix manifest reader
wgtmac 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
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,197 @@ | ||
| /* | ||
| * 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/update/fast_append.h" | ||
|
|
||
| #include <format> | ||
|
|
||
| #include <gmock/gmock.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "iceberg/avro/avro_register.h" | ||
| #include "iceberg/partition_spec.h" | ||
| #include "iceberg/schema.h" | ||
| #include "iceberg/table_metadata.h" | ||
| #include "iceberg/test/matchers.h" | ||
| #include "iceberg/test/test_resource.h" | ||
| #include "iceberg/test/update_test_base.h" | ||
| #include "iceberg/util/uuid.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| class FastAppendTest : public UpdateTestBase { | ||
| protected: | ||
| static void SetUpTestSuite() { avro::RegisterAll(); } | ||
|
|
||
| void SetUp() override { | ||
| UpdateTestBase::SetUp(); | ||
|
|
||
| ASSERT_THAT(catalog_->DropTable(table_ident_, /*purge=*/false), IsOk()); | ||
|
|
||
| auto metadata_location = std::format("{}/metadata/00001-{}.metadata.json", | ||
| table_location_, Uuid::GenerateV7().ToString()); | ||
| ICEBERG_UNWRAP_OR_FAIL( | ||
| auto metadata, ReadTableMetadataFromResource("TableMetadataV2ValidMinimal.json")); | ||
| metadata->location = table_location_; | ||
| ASSERT_THAT(TableMetadataUtil::Write(*file_io_, metadata_location, *metadata), | ||
| IsOk()); | ||
| ICEBERG_UNWRAP_OR_FAIL(table_, | ||
| catalog_->RegisterTable(table_ident_, metadata_location)); | ||
|
|
||
| // Get partition spec and schema from the base table | ||
| ICEBERG_UNWRAP_OR_FAIL(spec_, table_->spec()); | ||
| ICEBERG_UNWRAP_OR_FAIL(schema_, table_->schema()); | ||
|
|
||
| // Create test data files | ||
| file_a_ = CreateDataFile("/data/file_a.parquet", 100, 1024); | ||
| file_b_ = CreateDataFile("/data/file_b.parquet", 200, 2048); | ||
|
zhjwpku marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| std::shared_ptr<DataFile> CreateDataFile(const std::string& path, int64_t record_count, | ||
| int64_t size, int64_t partition_value = 0) { | ||
| auto data_file = std::make_shared<DataFile>(); | ||
| data_file->content = DataFile::Content::kData; | ||
| data_file->file_path = table_location_ + path; | ||
| data_file->file_format = FileFormatType::kParquet; | ||
| // The base table has partition spec with identity(x), so we need 1 partition value | ||
| data_file->partition = | ||
| PartitionValues(std::vector<Literal>{Literal::Long(partition_value)}); | ||
| data_file->file_size_in_bytes = size; | ||
| data_file->record_count = record_count; | ||
| data_file->partition_spec_id = spec_->spec_id(); | ||
| return data_file; | ||
| } | ||
|
|
||
| std::shared_ptr<PartitionSpec> spec_; | ||
| std::shared_ptr<Schema> schema_; | ||
| std::shared_ptr<DataFile> file_a_; | ||
| std::shared_ptr<DataFile> file_b_; | ||
| }; | ||
|
|
||
| TEST_F(FastAppendTest, AppendDataFile) { | ||
| std::shared_ptr<FastAppend> fast_append; | ||
| ICEBERG_UNWRAP_OR_FAIL(fast_append, table_->NewFastAppend()); | ||
| fast_append->AppendFile(file_a_); | ||
|
|
||
| EXPECT_THAT(fast_append->Commit(), IsOk()); | ||
|
|
||
| EXPECT_THAT(table_->Refresh(), IsOk()); | ||
| ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot()); | ||
| EXPECT_EQ(snapshot->summary.at("added-data-files"), "1"); | ||
| EXPECT_EQ(snapshot->summary.at("added-records"), "100"); | ||
| EXPECT_EQ(snapshot->summary.at("added-files-size"), "1024"); | ||
| } | ||
|
|
||
| TEST_F(FastAppendTest, AppendMultipleDataFiles) { | ||
| std::shared_ptr<FastAppend> fast_append; | ||
| ICEBERG_UNWRAP_OR_FAIL(fast_append, table_->NewFastAppend()); | ||
| fast_append->AppendFile(file_a_); | ||
| fast_append->AppendFile(file_b_); | ||
|
|
||
| EXPECT_THAT(fast_append->Commit(), IsOk()); | ||
|
|
||
| EXPECT_THAT(table_->Refresh(), IsOk()); | ||
| ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot()); | ||
| EXPECT_EQ(snapshot->summary.at("added-data-files"), "2"); | ||
| EXPECT_EQ(snapshot->summary.at("added-records"), "300"); | ||
| EXPECT_EQ(snapshot->summary.at("added-files-size"), "3072"); | ||
| } | ||
|
|
||
| TEST_F(FastAppendTest, AppendManyFiles) { | ||
| std::shared_ptr<FastAppend> fast_append; | ||
| ICEBERG_UNWRAP_OR_FAIL(fast_append, table_->NewFastAppend()); | ||
|
|
||
| int64_t total_records = 0; | ||
| int64_t total_size = 0; | ||
| constexpr int kFileCount = 10; | ||
| for (int index = 0; index < kFileCount; ++index) { | ||
| auto data_file = CreateDataFile(std::format("/data/file_{}.parquet", index), | ||
| /*record_count=*/10 + index, | ||
| /*size=*/100 + index * 10, | ||
| /*partition_value=*/index % 2); | ||
| total_records += data_file->record_count; | ||
| total_size += data_file->file_size_in_bytes; | ||
| fast_append->AppendFile(std::move(data_file)); | ||
| } | ||
|
|
||
| EXPECT_THAT(fast_append->Commit(), IsOk()); | ||
|
|
||
| EXPECT_THAT(table_->Refresh(), IsOk()); | ||
| ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot()); | ||
| EXPECT_EQ(snapshot->summary.at("added-data-files"), std::to_string(kFileCount)); | ||
| EXPECT_EQ(snapshot->summary.at("added-records"), std::to_string(total_records)); | ||
| EXPECT_EQ(snapshot->summary.at("added-files-size"), std::to_string(total_size)); | ||
| } | ||
|
|
||
| TEST_F(FastAppendTest, EmptyTableAppendUpdatesSequenceNumbers) { | ||
| EXPECT_THAT(table_->current_snapshot(), HasErrorMessage("No current snapshot")); | ||
| const int64_t base_sequence_number = table_->metadata()->last_sequence_number; | ||
|
|
||
| std::shared_ptr<FastAppend> fast_append; | ||
| ICEBERG_UNWRAP_OR_FAIL(fast_append, table_->NewFastAppend()); | ||
| fast_append->AppendFile(file_a_); | ||
|
|
||
| EXPECT_THAT(fast_append->Commit(), IsOk()); | ||
|
|
||
| EXPECT_THAT(table_->Refresh(), IsOk()); | ||
| ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot()); | ||
| EXPECT_EQ(snapshot->sequence_number, base_sequence_number + 1); | ||
| EXPECT_EQ(table_->metadata()->last_sequence_number, base_sequence_number + 1); | ||
| } | ||
|
|
||
| TEST_F(FastAppendTest, AppendNullFile) { | ||
| std::shared_ptr<FastAppend> fast_append; | ||
| ICEBERG_UNWRAP_OR_FAIL(fast_append, table_->NewFastAppend()); | ||
| fast_append->AppendFile(nullptr); | ||
|
|
||
| auto result = fast_append->Commit(); | ||
| EXPECT_FALSE(result.has_value()); | ||
| EXPECT_THAT(result, HasErrorMessage("Invalid data file: null")); | ||
| EXPECT_THAT(table_->current_snapshot(), HasErrorMessage("No current snapshot")); | ||
| } | ||
|
|
||
| TEST_F(FastAppendTest, AppendDuplicateFile) { | ||
| std::shared_ptr<FastAppend> fast_append; | ||
| ICEBERG_UNWRAP_OR_FAIL(fast_append, table_->NewFastAppend()); | ||
| fast_append->AppendFile(file_a_); | ||
| fast_append->AppendFile(file_a_); // Add same file twice | ||
|
|
||
| EXPECT_THAT(fast_append->Commit(), IsOk()); | ||
|
|
||
| EXPECT_THAT(table_->Refresh(), IsOk()); | ||
| ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot()); | ||
| // Should only count the file once | ||
| EXPECT_EQ(snapshot->summary.at("added-data-files"), "1"); | ||
| EXPECT_EQ(snapshot->summary.at("added-records"), "100"); | ||
| } | ||
|
|
||
| TEST_F(FastAppendTest, SetSnapshotProperty) { | ||
| std::shared_ptr<FastAppend> fast_append; | ||
| ICEBERG_UNWRAP_OR_FAIL(fast_append, table_->NewFastAppend()); | ||
| fast_append->Set("custom-property", "custom-value"); | ||
| fast_append->AppendFile(file_a_); | ||
|
|
||
| EXPECT_THAT(fast_append->Commit(), IsOk()); | ||
|
|
||
| EXPECT_THAT(table_->Refresh(), IsOk()); | ||
| ICEBERG_UNWRAP_OR_FAIL(auto snapshot, table_->current_snapshot()); | ||
| EXPECT_EQ(snapshot->summary.at("custom-property"), "custom-value"); | ||
| } | ||
|
|
||
| } // 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
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,70 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "iceberg/iceberg_export.h" | ||
| #include "iceberg/type_fwd.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| /// \brief API for appending new files in a table. | ||
| /// | ||
| /// This API accumulates file additions, produces a new Snapshot of the table, and commits | ||
| /// that snapshot as the current. | ||
| /// | ||
| /// When committing, these changes will be applied to the latest table snapshot. Commit | ||
| /// conflicts will be resolved by applying the changes to the new latest snapshot and | ||
| /// reattempting the commit. | ||
| class ICEBERG_EXPORT AppendFiles { | ||
|
zhjwpku marked this conversation as resolved.
Outdated
|
||
| public: | ||
| virtual ~AppendFiles() = default; | ||
|
|
||
| /// \brief Append a DataFile to the table. | ||
| /// | ||
| /// \param file A data file | ||
| /// \return Reference to this for method chaining | ||
| virtual AppendFiles& AppendFile(std::shared_ptr<DataFile> file) = 0; | ||
|
|
||
| /// \brief Append a ManifestFile to the table. | ||
| /// | ||
| /// The manifest must contain only appended files. All files in the manifest will be | ||
| /// appended to the table in the snapshot created by this update. | ||
| /// | ||
| /// The manifest will be used directly if snapshot ID inheritance is enabled (all tables | ||
| /// with the format version > 1 or if the inheritance is enabled explicitly via table | ||
| /// properties). Otherwise, the manifest will be rewritten to assign all entries this | ||
| /// update's snapshot ID. | ||
| /// | ||
| /// If the manifest is rewritten, it is always the responsibility of the caller to | ||
| /// manage the lifecycle of the original manifest. If the manifest is used directly, it | ||
| /// should never be deleted manually if the commit succeeds as it will become part of | ||
| /// the table metadata and will be cleaned upon expiry. If the manifest gets merged with | ||
| /// others while preparing a new snapshot, it will be deleted automatically if this | ||
| /// operation is successful. If the commit fails, the manifest will never be deleted, | ||
| /// and it is up to the caller whether to delete or reuse it. | ||
| /// | ||
| /// \param file A manifest file | ||
| /// \return Reference to this for method chaining | ||
| virtual AppendFiles& AppendManifest(const ManifestFile& file) = 0; | ||
| }; | ||
|
|
||
| } // 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.