Skip to content

Commit c46b38e

Browse files
authored
fix: SetSnapshotRef::ApplyTo should call SetRef (#540)
1 parent e2d6496 commit c46b38e

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed

src/iceberg/table_update.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919

2020
#include "iceberg/table_update.h"
2121

22-
#include "iceberg/exception.h"
2322
#include "iceberg/schema.h"
23+
#include "iceberg/snapshot.h"
2424
#include "iceberg/sort_order.h"
2525
#include "iceberg/statistics_file.h"
2626
#include "iceberg/table_metadata.h"
2727
#include "iceberg/table_requirements.h"
2828
#include "iceberg/util/checked_cast.h"
29+
#include "iceberg/util/macros.h"
2930

3031
namespace iceberg {
3132
TableUpdate::~TableUpdate() = default;
@@ -348,7 +349,15 @@ std::unique_ptr<TableUpdate> RemoveSnapshotRef::Clone() const {
348349
// SetSnapshotRef
349350

350351
void SetSnapshotRef::ApplyTo(TableMetadataBuilder& builder) const {
351-
builder.SetBranchSnapshot(snapshot_id_, ref_name_);
352+
std::shared_ptr<SnapshotRef> ref;
353+
if (type_ == SnapshotRefType::kBranch) {
354+
ICEBERG_ASSIGN_OR_THROW(
355+
ref, SnapshotRef::MakeBranch(snapshot_id_, min_snapshots_to_keep_,
356+
max_snapshot_age_ms_, max_ref_age_ms_));
357+
} else {
358+
ICEBERG_ASSIGN_OR_THROW(ref, SnapshotRef::MakeTag(snapshot_id_, max_ref_age_ms_));
359+
}
360+
builder.SetRef(ref_name_, std::move(ref));
352361
}
353362

354363
void SetSnapshotRef::GenerateRequirements(TableUpdateContext& context) const {

src/iceberg/test/table_update_test.cc

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "iceberg/test/matchers.h"
3838
#include "iceberg/transform.h"
3939
#include "iceberg/type.h"
40+
#include "iceberg/util/timepoint.h"
4041

4142
namespace iceberg {
4243

@@ -368,4 +369,75 @@ TEST(TableUpdateTest, SetDefaultSortOrderApplyUpdate) {
368369
EXPECT_EQ(metadata->default_sort_order_id, 1);
369370
}
370371

372+
// Test SetSnapshotRef ApplyTo for both branch and tag
373+
TEST(TableUpdateTest, SetSnapshotRefApplyUpdate) {
374+
// Test branch ref
375+
{
376+
auto base = CreateBaseMetadata();
377+
auto builder = TableMetadataBuilder::BuildFrom(base.get());
378+
379+
// Add a snapshot that the ref will point to
380+
auto snapshot = std::make_shared<Snapshot>(
381+
Snapshot{.snapshot_id = 123456789,
382+
.sequence_number = 1,
383+
.timestamp_ms = TimePointMsFromUnixMs(1000000),
384+
.manifest_list = "s3://bucket/manifest-list.avro"});
385+
builder->AddSnapshot(snapshot);
386+
387+
// Apply SetSnapshotRef update for a branch
388+
table::SetSnapshotRef branch_update("my-branch", 123456789, SnapshotRefType::kBranch,
389+
5, 86400000, 604800000);
390+
branch_update.ApplyTo(*builder);
391+
392+
ICEBERG_UNWRAP_OR_FAIL(auto metadata, builder->Build());
393+
394+
// Verify the branch ref was added
395+
ASSERT_EQ(metadata->refs.size(), 1);
396+
auto it = metadata->refs.find("my-branch");
397+
ASSERT_NE(it, metadata->refs.end());
398+
399+
const auto& ref = it->second;
400+
EXPECT_EQ(ref->snapshot_id, 123456789);
401+
EXPECT_EQ(ref->type(), SnapshotRefType::kBranch);
402+
403+
const auto& branch = std::get<SnapshotRef::Branch>(ref->retention);
404+
EXPECT_EQ(branch.min_snapshots_to_keep.value(), 5);
405+
EXPECT_EQ(branch.max_snapshot_age_ms.value(), 86400000);
406+
EXPECT_EQ(branch.max_ref_age_ms.value(), 604800000);
407+
}
408+
409+
// Test tag ref
410+
{
411+
auto base = CreateBaseMetadata();
412+
auto builder = TableMetadataBuilder::BuildFrom(base.get());
413+
414+
// Add a snapshot that the ref will point to
415+
auto snapshot = std::make_shared<Snapshot>(
416+
Snapshot{.snapshot_id = 987654321,
417+
.sequence_number = 1,
418+
.timestamp_ms = TimePointMsFromUnixMs(2000000),
419+
.manifest_list = "s3://bucket/manifest-list.avro"});
420+
builder->AddSnapshot(snapshot);
421+
422+
// Apply SetSnapshotRef update for a tag
423+
table::SetSnapshotRef tag_update("release-1.0", 987654321, SnapshotRefType::kTag,
424+
std::nullopt, std::nullopt, 259200000);
425+
tag_update.ApplyTo(*builder);
426+
427+
ICEBERG_UNWRAP_OR_FAIL(auto metadata, builder->Build());
428+
429+
// Verify the tag ref was added
430+
ASSERT_EQ(metadata->refs.size(), 1);
431+
auto it = metadata->refs.find("release-1.0");
432+
ASSERT_NE(it, metadata->refs.end());
433+
434+
const auto& ref = it->second;
435+
EXPECT_EQ(ref->snapshot_id, 987654321);
436+
EXPECT_EQ(ref->type(), SnapshotRefType::kTag);
437+
438+
const auto& tag = std::get<SnapshotRef::Tag>(ref->retention);
439+
EXPECT_EQ(tag.max_ref_age_ms.value(), 259200000);
440+
}
441+
}
442+
371443
} // namespace iceberg

0 commit comments

Comments
 (0)