Skip to content

Commit 18bade1

Browse files
committed
feat: add rewrite manifests
1 parent 90a2be7 commit 18bade1

20 files changed

Lines changed: 2706 additions & 8 deletions

src/iceberg/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ set(ICEBERG_SOURCES
108108
update/merging_snapshot_update.cc
109109
update/overwrite_files.cc
110110
update/pending_update.cc
111+
update/rewrite_manifests.cc
111112
update/row_delta.cc
112113
update/set_snapshot.cc
113114
update/snapshot_manager.cc

src/iceberg/manifest/rolling_manifest_writer.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ Status RollingManifestWriter::WriteExistingEntry(
5454
return {};
5555
}
5656

57+
Status RollingManifestWriter::WriteExistingEntry(const ManifestEntry& entry) {
58+
ICEBERG_ASSIGN_OR_RAISE(auto* writer, CurrentWriter());
59+
ICEBERG_RETURN_UNEXPECTED(writer->WriteExistingEntry(entry));
60+
current_file_rows_++;
61+
return {};
62+
}
63+
5764
Status RollingManifestWriter::WriteDeletedEntry(
5865
std::shared_ptr<DataFile> file, int64_t data_sequence_number,
5966
std::optional<int64_t> file_sequence_number) {

src/iceberg/manifest/rolling_manifest_writer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class ICEBERG_EXPORT RollingManifestWriter {
7676
int64_t data_sequence_number,
7777
std::optional<int64_t> file_sequence_number = std::nullopt);
7878

79+
/// \brief Add an existing entry while preserving snapshot and sequence fields.
80+
Status WriteExistingEntry(const ManifestEntry& entry);
81+
7982
/// \brief Add a delete entry for a file.
8083
///
8184
/// \param file a deleted data file

src/iceberg/manifest/v3_metadata.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,20 @@ Status ManifestFileAdapterV3::Init() {
194194
}
195195

196196
Status ManifestFileAdapterV3::Append(const ManifestFile& file) {
197-
ICEBERG_RETURN_UNEXPECTED(AppendInternal(file));
198197
if (WrapFirstRowId(file)) {
199198
if (!next_row_id_.has_value()) {
200199
return InvalidManifestList("Missing next-row-id for file: {}", file.manifest_path);
201200
}
202-
next_row_id_ = next_row_id_.value() + file.existing_rows_count.value_or(0) +
203-
file.added_rows_count.value_or(0);
201+
if (!file.existing_rows_count.has_value() || !file.added_rows_count.has_value()) {
202+
return InvalidManifestList("Missing row counts for file: {}", file.manifest_path);
203+
}
204+
}
205+
206+
ICEBERG_RETURN_UNEXPECTED(AppendInternal(file));
207+
208+
if (WrapFirstRowId(file)) {
209+
next_row_id_ = next_row_id_.value() + file.existing_rows_count.value() +
210+
file.added_rows_count.value();
204211
}
205212
return {};
206213
}

src/iceberg/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ iceberg_sources = files(
133133
'update/merging_snapshot_update.cc',
134134
'update/overwrite_files.cc',
135135
'update/pending_update.cc',
136+
'update/rewrite_manifests.cc',
136137
'update/row_delta.cc',
137138
'update/set_snapshot.cc',
138139
'update/snapshot_manager.cc',

src/iceberg/table.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "iceberg/update/fast_append.h"
3737
#include "iceberg/update/merge_append.h"
3838
#include "iceberg/update/overwrite_files.h"
39+
#include "iceberg/update/rewrite_manifests.h"
3940
#include "iceberg/update/row_delta.h"
4041
#include "iceberg/update/set_snapshot.h"
4142
#include "iceberg/update/snapshot_manager.h"
@@ -245,6 +246,12 @@ Result<std::shared_ptr<OverwriteFiles>> Table::NewOverwrite() {
245246
return OverwriteFiles::Make(name().name, std::move(ctx));
246247
}
247248

249+
Result<std::shared_ptr<RewriteManifests>> Table::NewRewriteManifests() {
250+
ICEBERG_ASSIGN_OR_RAISE(
251+
auto ctx, TransactionContext::Make(shared_from_this(), TransactionKind::kUpdate));
252+
return RewriteManifests::Make(name().name, std::move(ctx));
253+
}
254+
248255
Result<std::shared_ptr<UpdateStatistics>> Table::NewUpdateStatistics() {
249256
ICEBERG_ASSIGN_OR_RAISE(
250257
auto ctx, TransactionContext::Make(shared_from_this(), TransactionKind::kUpdate));
@@ -360,6 +367,10 @@ Result<std::shared_ptr<OverwriteFiles>> StaticTable::NewOverwrite() {
360367
return NotSupported("Cannot create an overwrite for a static table");
361368
}
362369

370+
Result<std::shared_ptr<RewriteManifests>> StaticTable::NewRewriteManifests() {
371+
return NotSupported("Cannot create a rewrite manifests for a static table");
372+
}
373+
363374
Result<std::shared_ptr<SnapshotManager>> StaticTable::NewSnapshotManager() {
364375
return NotSupported("Cannot create a snapshot manager for a static table");
365376
}

src/iceberg/table.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ class ICEBERG_EXPORT Table : public std::enable_shared_from_this<Table> {
188188
/// \brief Create a new OverwriteFiles to overwrite data files and commit the changes.
189189
virtual Result<std::shared_ptr<OverwriteFiles>> NewOverwrite();
190190

191+
/// \brief Create a new RewriteManifests to rewrite manifest layout.
192+
virtual Result<std::shared_ptr<RewriteManifests>> NewRewriteManifests();
193+
191194
/// \brief Create a new SnapshotManager to manage snapshots and snapshot references.
192195
virtual Result<std::shared_ptr<SnapshotManager>> NewSnapshotManager();
193196

@@ -263,6 +266,8 @@ class ICEBERG_EXPORT StaticTable : public Table {
263266

264267
Result<std::shared_ptr<OverwriteFiles>> NewOverwrite() override;
265268

269+
Result<std::shared_ptr<RewriteManifests>> NewRewriteManifests() override;
270+
266271
Result<std::shared_ptr<SnapshotManager>> NewSnapshotManager() override;
267272

268273
private:

src/iceberg/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ if(ICEBERG_BUILD_BUNDLE)
234234
merge_append_test.cc
235235
merging_snapshot_update_test.cc
236236
name_mapping_update_test.cc
237+
rewrite_manifests_test.cc
237238
row_delta_test.cc
238239
snapshot_manager_test.cc
239240
transaction_test.cc

src/iceberg/test/merge_append_test.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,9 @@ class MergeAppendTestBase : public UpdateTestBase {
8888
auto metadata_location = std::format("{}/metadata/00001-{}.metadata.json",
8989
table_location_, Uuid::GenerateV7().ToString());
9090
ICEBERG_UNWRAP_OR_FAIL(
91-
auto metadata, ReadTableMetadataFromResource("TableMetadataV2ValidMinimal.json"));
91+
auto metadata, ReadTableMetadataFromResource("TableMetadataV3ValidMinimal.json"));
9292
metadata->format_version = format_version;
9393
metadata->location = table_location_;
94-
metadata->next_row_id = TableMetadata::kInitialRowId;
9594

9695
ASSERT_THAT(TableMetadataUtil::Write(*file_io_, metadata_location, *metadata),
9796
IsOk());

0 commit comments

Comments
 (0)