2121
2222#include " iceberg/snapshot.h"
2323#include " iceberg/table_metadata.h"
24+ #include " iceberg/util/macros.h"
2425#include " iceberg/util/string_util.h"
2526
2627namespace iceberg ::table {
2728
2829Status AssertDoesNotExist::Validate (const TableMetadata* base) const {
29- // Validate that the table does not exist
30-
3130 if (base != nullptr ) {
3231 return CommitFailed (" Requirement failed: table already exists" );
3332 }
@@ -36,39 +35,39 @@ Status AssertDoesNotExist::Validate(const TableMetadata* base) const {
3635}
3736
3837Status AssertUUID::Validate (const TableMetadata* base) const {
39- // Validate that the table UUID matches the expected value
40-
4138 if (base == nullptr ) {
4239 return CommitFailed (" Requirement failed: current table metadata is missing" );
4340 }
4441
4542 if (!StringUtils::EqualsIgnoreCase (base->table_uuid , uuid_)) {
4643 return CommitFailed (
47- " Requirement failed: table UUID does not match ( expected='{}', actual='{}') " ,
48- uuid_, base->table_uuid );
44+ " Requirement failed: table UUID does not match, expected {} != {} " , uuid_ ,
45+ base->table_uuid );
4946 }
5047
5148 return {};
5249}
5350
5451Status AssertRefSnapshotID::Validate (const TableMetadata* base) const {
55- // Validate that a reference (branch or tag) points to the expected snapshot ID
56- // Matches Java implementation logic
52+ if (base == nullptr ) {
53+ return CommitFailed (" Requirement failed: current table metadata is missing" );
54+ }
5755
58- auto it = base->refs .find (ref_name_);
59- if (it != base->refs .end ()) {
60- // Reference exists
56+ if (auto ref = base->refs .find (ref_name_); ref != base->refs .cend ()) {
57+ const auto & snapshot_ref = ref->second ;
58+ ICEBERG_DCHECK (snapshot_ref != nullptr , " Snapshot reference is null" );
59+ std::string_view type =
60+ snapshot_ref->type () == SnapshotRefType::kBranch ? " branch" : " tag" ;
6161 if (!snapshot_id_.has_value ()) {
6262 // A null snapshot ID means the ref should not exist already
63- return CommitFailed (" Requirement failed: reference '{}' was created concurrently" ,
63+ return CommitFailed (" Requirement failed: {} '{}' was created concurrently" , type ,
6464 ref_name_);
65- } else if (snapshot_id_.value () != it-> second ->snapshot_id ) {
66- return CommitFailed (
67- " Requirement failed: reference '{}' has changed: expected id {} != {} " ,
68- ref_name_, snapshot_id_. value (), it-> second ->snapshot_id );
65+ } else if (snapshot_id_.value () != snapshot_ref ->snapshot_id ) {
66+ return CommitFailed (" Requirement failed: {} '{}' has changed: expected id {} != {} " ,
67+ type, ref_name_, snapshot_id_. value () ,
68+ snapshot_ref ->snapshot_id );
6969 }
7070 } else if (snapshot_id_.has_value ()) {
71- // Reference does not exist but snapshot_id is specified
7271 return CommitFailed (" Requirement failed: branch or tag '{}' is missing, expected {}" ,
7372 ref_name_, snapshot_id_.value ());
7473 }
@@ -77,22 +76,20 @@ Status AssertRefSnapshotID::Validate(const TableMetadata* base) const {
7776}
7877
7978Status AssertLastAssignedFieldId::Validate (const TableMetadata* base) const {
80- // Validate that the last assigned field ID matches the expected value
81- // Allows base to be null for new tables
79+ if (base == nullptr ) {
80+ return CommitFailed (" Requirement failed: current table metadata is missing" );
81+ }
8282
8383 if (base && base->last_column_id != last_assigned_field_id_) {
8484 return CommitFailed (
85- " Requirement failed: last assigned field ID does not match (expected={}, "
86- " actual={})" ,
85+ " Requirement failed: last assigned field ID does not match, expected {} != {}" ,
8786 last_assigned_field_id_, base->last_column_id );
8887 }
8988
9089 return {};
9190}
9291
9392Status AssertCurrentSchemaID::Validate (const TableMetadata* base) const {
94- // Validate that the current schema ID matches the one used when the metadata was read
95-
9693 if (base == nullptr ) {
9794 return CommitFailed (" Requirement failed: current table metadata is missing" );
9895 }
@@ -104,43 +101,46 @@ Status AssertCurrentSchemaID::Validate(const TableMetadata* base) const {
104101
105102 if (base->current_schema_id .value () != schema_id_) {
106103 return CommitFailed (
107- " Requirement failed: current schema ID does not match ( expected={}, actual={}) " ,
104+ " Requirement failed: current schema ID does not match, expected {} != {} " ,
108105 schema_id_, base->current_schema_id .value ());
109106 }
110107
111108 return {};
112109}
113110
114111Status AssertLastAssignedPartitionId::Validate (const TableMetadata* base) const {
115- // Validate that the last assigned partition ID matches the expected value
116- // Allows base to be null for new tables
112+ if (base == nullptr ) {
113+ return CommitFailed (" Requirement failed: current table metadata is missing" );
114+ }
117115
118116 if (base && base->last_partition_id != last_assigned_partition_id_) {
119117 return CommitFailed (
120- " Requirement failed: last assigned partition ID does not match ( expected={}, "
121- " actual={}) " ,
118+ " Requirement failed: last assigned partition ID does not match, expected {} != "
119+ " {} " ,
122120 last_assigned_partition_id_, base->last_partition_id );
123121 }
124122
125123 return {};
126124}
127125
128126Status AssertDefaultSpecID::Validate (const TableMetadata* base) const {
129- // Validate that the default partition spec ID matches the expected value
130- // Matches Java implementation - assumes base is never null
127+ if (base == nullptr ) {
128+ return CommitFailed (" Requirement failed: current table metadata is missing" );
129+ }
131130
132131 if (base->default_spec_id != spec_id_) {
133132 return CommitFailed (
134- " Requirement failed: default partition spec changed: expected id {} != {}" ,
133+ " Requirement failed: default partition spec changed, expected id {} != {}" ,
135134 spec_id_, base->default_spec_id );
136135 }
137136
138137 return {};
139138}
140139
141140Status AssertDefaultSortOrderID::Validate (const TableMetadata* base) const {
142- // Validate that the default sort order ID matches the expected value
143- // Matches Java implementation - assumes base is never null
141+ if (base == nullptr ) {
142+ return CommitFailed (" Requirement failed: current table metadata is missing" );
143+ }
144144
145145 if (base->default_sort_order_id != sort_order_id_) {
146146 return CommitFailed (
0 commit comments