Skip to content

Commit 56b6368

Browse files
committed
feat: implement validation for table update requirements
Implement Validate() methods for remaining table requirement classes: - AssertDoesNotExist: validates table doesn't exist - AssertRefSnapshotID: validates snapshot references (branches/tags) - AssertLastAssignedFieldId: validates last assigned field ID - AssertLastAssignedPartitionId: validates last assigned partition ID - AssertDefaultSpecID: validates default partition spec ID - AssertDefaultSortOrderID: validates default sort order ID All implementations follow the pattern established in AssertCurrentSchemaID and provide descriptive error messages for validation failures.
1 parent abd6b3f commit 56b6368

File tree

1 file changed

+93
-8
lines changed

1 file changed

+93
-8
lines changed

src/iceberg/table_requirement.cc

Lines changed: 93 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,20 @@
1919

2020
#include "iceberg/table_requirement.h"
2121

22+
#include "iceberg/snapshot.h"
2223
#include "iceberg/table_metadata.h"
2324
#include "iceberg/util/string_util.h"
2425

2526
namespace iceberg::table {
2627

2728
Status AssertDoesNotExist::Validate(const TableMetadata* base) const {
28-
return NotImplemented("AssertDoesNotExist::Validate not implemented");
29+
// Validate that the table does not exist
30+
31+
if (base != nullptr) {
32+
return CommitFailed("Requirement failed: table already exists");
33+
}
34+
35+
return {};
2936
}
3037

3138
Status AssertUUID::Validate(const TableMetadata* base) const {
@@ -45,12 +52,54 @@ Status AssertUUID::Validate(const TableMetadata* base) const {
4552
}
4653

4754
Status AssertRefSnapshotID::Validate(const TableMetadata* base) const {
48-
return NotImplemented("AssertTableRefSnapshotID::Validate not implemented");
55+
// Validate that a reference (branch or tag) points to the expected snapshot ID
56+
57+
if (base == nullptr) {
58+
return CommitFailed("Requirement failed: current table metadata is missing");
59+
}
60+
61+
auto it = base->refs.find(ref_name_);
62+
63+
// If snapshot_id is nullopt, the reference should not exist
64+
if (!snapshot_id_.has_value()) {
65+
if (it != base->refs.end()) {
66+
return CommitFailed(
67+
"Requirement failed: reference '{}' should not exist but found snapshot ID {}",
68+
ref_name_, it->second->snapshot_id);
69+
}
70+
return {};
71+
}
72+
73+
// snapshot_id has a value, so the reference should exist and match
74+
if (it == base->refs.end()) {
75+
return CommitFailed(
76+
"Requirement failed: reference '{}' is missing in table metadata", ref_name_);
77+
}
78+
79+
if (it->second->snapshot_id != snapshot_id_.value()) {
80+
return CommitFailed(
81+
"Requirement failed: reference '{}' snapshot ID does not match (expected={}, "
82+
"actual={})",
83+
ref_name_, snapshot_id_.value(), it->second->snapshot_id);
84+
}
85+
86+
return {};
4987
}
5088

5189
Status AssertLastAssignedFieldId::Validate(const TableMetadata* base) const {
52-
return NotImplemented(
53-
"AssertCurrentTableLastAssignedFieldId::Validate not implemented");
90+
// Validate that the last assigned field ID matches the expected value
91+
92+
if (base == nullptr) {
93+
return CommitFailed("Requirement failed: current table metadata is missing");
94+
}
95+
96+
if (base->last_column_id != last_assigned_field_id_) {
97+
return CommitFailed(
98+
"Requirement failed: last assigned field ID does not match (expected={}, actual={})",
99+
last_assigned_field_id_, base->last_column_id);
100+
}
101+
102+
return {};
54103
}
55104

56105
Status AssertCurrentSchemaID::Validate(const TableMetadata* base) const {
@@ -75,16 +124,52 @@ Status AssertCurrentSchemaID::Validate(const TableMetadata* base) const {
75124
}
76125

77126
Status AssertLastAssignedPartitionId::Validate(const TableMetadata* base) const {
78-
return NotImplemented(
79-
"AssertCurrentTableLastAssignedPartitionId::Validate not implemented");
127+
// Validate that the last assigned partition ID matches the expected value
128+
129+
if (base == nullptr) {
130+
return CommitFailed("Requirement failed: current table metadata is missing");
131+
}
132+
133+
if (base->last_partition_id != last_assigned_partition_id_) {
134+
return CommitFailed(
135+
"Requirement failed: last assigned partition ID does not match (expected={}, "
136+
"actual={})",
137+
last_assigned_partition_id_, base->last_partition_id);
138+
}
139+
140+
return {};
80141
}
81142

82143
Status AssertDefaultSpecID::Validate(const TableMetadata* base) const {
83-
return NotImplemented("AssertDefaultTableSpecID::Validate not implemented");
144+
// Validate that the default partition spec ID matches the expected value
145+
146+
if (base == nullptr) {
147+
return CommitFailed("Requirement failed: current table metadata is missing");
148+
}
149+
150+
if (base->default_spec_id != spec_id_) {
151+
return CommitFailed(
152+
"Requirement failed: default spec ID does not match (expected={}, actual={})",
153+
spec_id_, base->default_spec_id);
154+
}
155+
156+
return {};
84157
}
85158

86159
Status AssertDefaultSortOrderID::Validate(const TableMetadata* base) const {
87-
return NotImplemented("AssertDefaultTableSortOrderID::Validate not implemented");
160+
// Validate that the default sort order ID matches the expected value
161+
162+
if (base == nullptr) {
163+
return CommitFailed("Requirement failed: current table metadata is missing");
164+
}
165+
166+
if (base->default_sort_order_id != sort_order_id_) {
167+
return CommitFailed(
168+
"Requirement failed: default sort order ID does not match (expected={}, actual={})",
169+
sort_order_id_, base->default_sort_order_id);
170+
}
171+
172+
return {};
88173
}
89174

90175
} // namespace iceberg::table

0 commit comments

Comments
 (0)