Skip to content

Commit 7ec949c

Browse files
committed
remove AI-generated comments and fix some missing check
1 parent 793e6df commit 7ec949c

File tree

2 files changed

+39
-48
lines changed

2 files changed

+39
-48
lines changed

src/iceberg/table_requirement.cc

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@
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

2627
namespace iceberg::table {
2728

2829
Status 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

3837
Status 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

5451
Status 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

7978
Status 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

9392
Status 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

114111
Status 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

128126
Status 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

141140
Status 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(

src/iceberg/test/table_metadata_builder_test.cc

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,6 @@ TEST_F(TableMetadataBuilderTest, TableRequirementAssertRefSnapshotIDNulloptButEx
328328
ref->retention = SnapshotRef::Branch{};
329329
base_metadata_->refs["main"] = ref;
330330

331-
// Ref should not exist, but it does
332331
table::AssertRefSnapshotID requirement("main", std::nullopt);
333332

334333
auto status = requirement.Validate(base_metadata_.get());
@@ -355,8 +354,9 @@ TEST_F(TableMetadataBuilderTest, TableRequirementAssertLastAssignedFieldIdMismat
355354
TEST_F(TableMetadataBuilderTest, TableRequirementAssertLastAssignedFieldIdNullBase) {
356355
table::AssertLastAssignedFieldId requirement(10);
357356

358-
// Null base is now allowed (for new tables) - should succeed
359-
ASSERT_THAT(requirement.Validate(nullptr), IsOk());
357+
auto status = requirement.Validate(nullptr);
358+
EXPECT_THAT(status, IsError(ErrorKind::kCommitFailed));
359+
EXPECT_THAT(status, HasErrorMessage("metadata is missing"));
360360
}
361361

362362
TEST_F(TableMetadataBuilderTest, TableRequirementAssertLastAssignedPartitionIdSuccess) {
@@ -378,8 +378,9 @@ TEST_F(TableMetadataBuilderTest, TableRequirementAssertLastAssignedPartitionIdMi
378378
TEST_F(TableMetadataBuilderTest, TableRequirementAssertLastAssignedPartitionIdNullBase) {
379379
table::AssertLastAssignedPartitionId requirement(5);
380380

381-
// Null base is now allowed (for new tables) - should succeed
382-
ASSERT_THAT(requirement.Validate(nullptr), IsOk());
381+
auto status = requirement.Validate(nullptr);
382+
EXPECT_THAT(status, IsError(ErrorKind::kCommitFailed));
383+
EXPECT_THAT(status, HasErrorMessage("metadata is missing"));
383384
}
384385

385386
TEST_F(TableMetadataBuilderTest, TableRequirementAssertDefaultSpecIDSuccess) {
@@ -398,11 +399,6 @@ TEST_F(TableMetadataBuilderTest, TableRequirementAssertDefaultSpecIDMismatch) {
398399
EXPECT_THAT(status, HasErrorMessage("spec changed"));
399400
}
400401

401-
// Removed TableRequirementAssertDefaultSpecIDNullBase test
402-
// Java implementation doesn't check for null base, so passing nullptr would cause
403-
// undefined behavior This matches Java's assumption that base is never null when Validate
404-
// is called
405-
406402
TEST_F(TableMetadataBuilderTest, TableRequirementAssertDefaultSortOrderIDSuccess) {
407403
base_metadata_->default_sort_order_id = 2;
408404
table::AssertDefaultSortOrderID requirement(2);
@@ -419,11 +415,6 @@ TEST_F(TableMetadataBuilderTest, TableRequirementAssertDefaultSortOrderIDMismatc
419415
EXPECT_THAT(status, HasErrorMessage("sort order changed"));
420416
}
421417

422-
// Removed TableRequirementAssertDefaultSortOrderIDNullBase test
423-
// Java implementation doesn't check for null base, so passing nullptr would cause
424-
// undefined behavior This matches Java's assumption that base is never null when Validate
425-
// is called
426-
427418
// ============================================================================
428419
// Integration Tests - End-to-End Workflow
429420
// ============================================================================

0 commit comments

Comments
 (0)