Skip to content

Commit 155ae5b

Browse files
committed
resolve review comments
1 parent 9825bd3 commit 155ae5b

File tree

10 files changed

+64
-129
lines changed

10 files changed

+64
-129
lines changed

src/iceberg/partition_spec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class ICEBERG_EXPORT PartitionSpec : public util::Formattable {
4747
/// \brief The start ID for partition field. It is only used to generate
4848
/// partition field id for v1 metadata where it is tracked.
4949
static constexpr int32_t kLegacyPartitionDataIdStart = 1000;
50+
static constexpr int32_t kInvalidPartitionFieldId = -1;
5051

5152
/// \brief Create a new partition spec.
5253
///

src/iceberg/schema.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace iceberg {
4646
class ICEBERG_EXPORT Schema : public StructType {
4747
public:
4848
static constexpr int32_t kInitialSchemaId = 0;
49+
static constexpr int32_t kInvalidColumnId = -1;
4950

5051
explicit Schema(std::vector<SchemaField> fields,
5152
std::optional<int32_t> schema_id = std::nullopt);

src/iceberg/table_metadata.cc

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ struct TableMetadataBuilder::Impl {
214214
std::vector<std::unique_ptr<TableUpdate>> changes;
215215

216216
// Error collection (since methods return *this and cannot throw)
217-
std::vector<Status> errors;
217+
std::vector<Error> errors;
218218

219219
// Metadata location tracking
220220
std::optional<std::string> metadata_location;
@@ -224,12 +224,12 @@ struct TableMetadataBuilder::Impl {
224224
explicit Impl(int8_t format_version) : base(nullptr), metadata{} {
225225
metadata.format_version = format_version;
226226
metadata.last_sequence_number = TableMetadata::kInitialSequenceNumber;
227-
metadata.last_updated_ms = TimePointMs{std::chrono::milliseconds(0)};
228-
metadata.last_column_id = 0;
229-
metadata.default_spec_id = TableMetadata::kInitialSpecId;
230-
metadata.last_partition_id = 0;
231-
metadata.current_snapshot_id = TableMetadata::kInvalidSnapshotId;
232-
metadata.default_sort_order_id = TableMetadata::kInitialSortOrderId;
227+
metadata.last_updated_ms = TimePointMs::min();
228+
metadata.last_column_id = Schema::kInvalidColumnId;
229+
metadata.default_spec_id = PartitionSpec::kInitialSpecId;
230+
metadata.last_partition_id = PartitionSpec::kInvalidPartitionFieldId;
231+
metadata.current_snapshot_id = Snapshot::kInvalidSnapshotId;
232+
metadata.default_sort_order_id = SortOrder::kInitialSortOrderId;
233233
metadata.next_row_id = TableMetadata::kInitialRowId;
234234
}
235235

@@ -284,9 +284,9 @@ TableMetadataBuilder& TableMetadataBuilder::AssignUUID() {
284284
TableMetadataBuilder& TableMetadataBuilder::AssignUUID(std::string_view uuid) {
285285
std::string uuid_str(uuid);
286286

287-
// Validation: UUID cannot be null or empty
287+
// Validation: UUID cannot be empty
288288
if (uuid_str.empty()) {
289-
impl_->errors.emplace_back(InvalidArgument("Cannot assign null or empty UUID"));
289+
impl_->errors.emplace_back(ErrorKind::kInvalidArgument, "Cannot assign empty UUID");
290290
return *this;
291291
}
292292

@@ -299,7 +299,7 @@ TableMetadataBuilder& TableMetadataBuilder::AssignUUID(std::string_view uuid) {
299299
impl_->metadata.table_uuid = uuid_str;
300300

301301
// Record the change
302-
impl_->changes.push_back(std::make_unique<table::AssignUUID>(uuid_str));
302+
impl_->changes.push_back(std::make_unique<table::AssignUUID>(std::move(uuid_str)));
303303

304304
return *this;
305305
}
@@ -435,42 +435,20 @@ TableMetadataBuilder& TableMetadataBuilder::RemoveEncryptionKey(std::string_view
435435
throw IcebergError(std::format("{} not implemented", __FUNCTION__));
436436
}
437437

438-
TableMetadataBuilder& TableMetadataBuilder::DiscardChanges() {
439-
// Clear all changes and errors
440-
impl_->changes.clear();
441-
impl_->errors.clear();
442-
443-
// Reset metadata to base state
444-
if (impl_->base != nullptr) {
445-
impl_->metadata = *impl_->base;
446-
} else {
447-
// Reset to initial state for new table
448-
*impl_ = Impl(impl_->metadata.format_version);
449-
}
450-
451-
return *this;
452-
}
453-
454438
Result<std::unique_ptr<TableMetadata>> TableMetadataBuilder::Build() {
455439
// 1. Check for accumulated errors
456440
if (!impl_->errors.empty()) {
457441
std::string error_msg = "Failed to build TableMetadata due to validation errors:\n";
458-
for (const auto& error : impl_->errors) {
459-
error_msg += " - " + error.error().message + "\n";
442+
for (const auto& [kind, message] : impl_->errors) {
443+
error_msg += " - " + message + "\n";
460444
}
461445
return CommitFailed("{}", error_msg);
462446
}
463447

464-
// 2. Validate metadata consistency
465-
466-
// Validate UUID exists for format version > 1
467-
if (impl_->metadata.format_version > 1 && impl_->metadata.table_uuid.empty()) {
468-
return InvalidArgument("UUID is required for format version {}",
469-
impl_->metadata.format_version);
470-
}
448+
// 2. Validate metadata consistency through TableMetadata#Validate
471449

472450
// 3. Update last_updated_ms if there are changes
473-
if (!impl_->changes.empty() && impl_->base != nullptr) {
451+
if (impl_->metadata.last_updated_ms == TimePointMs::min()) {
474452
impl_->metadata.last_updated_ms =
475453
TimePointMs{std::chrono::duration_cast<std::chrono::milliseconds>(
476454
std::chrono::system_clock::now().time_since_epoch())};

src/iceberg/table_metadata.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ struct ICEBERG_EXPORT TableMetadata {
7373
static constexpr int64_t kInitialSequenceNumber = 0;
7474
static constexpr int64_t kInvalidSequenceNumber = -1;
7575
static constexpr int64_t kInitialRowId = 0;
76-
static constexpr int32_t kInitialSpecId = 0;
77-
static constexpr int32_t kInitialSortOrderId = 1;
78-
static constexpr int64_t kInvalidSnapshotId = -1;
7976

8077
/// An integer version number for the format
8178
int8_t format_version;
@@ -380,13 +377,6 @@ class ICEBERG_EXPORT TableMetadataBuilder {
380377
/// \return Reference to this builder for method chaining
381378
TableMetadataBuilder& RemoveEncryptionKey(std::string_view key_id);
382379

383-
/// \brief Discard all accumulated changes
384-
///
385-
/// This is useful when you want to reset the builder state without
386-
/// creating a new builder instance.
387-
/// \return Reference to this builder for method chaining
388-
TableMetadataBuilder& DiscardChanges();
389-
390380
/// \brief Build the TableMetadata object
391381
///
392382
/// \return A Result containing the constructed TableMetadata or an error

src/iceberg/table_requirement.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "iceberg/table_requirement.h"
2121

2222
#include "iceberg/table_metadata.h"
23-
#include "util/string_util.h"
23+
#include "iceberg/util/string_util.h"
2424

2525
namespace iceberg::table {
2626

src/iceberg/test/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ add_iceberg_test(table_test
8282
test_common.cc
8383
json_internal_test.cc
8484
table_test.cc
85-
schema_json_test.cc)
86-
87-
add_iceberg_test(table_metadata_builder_test SOURCES table_metadata_builder_test.cc)
85+
schema_json_test.cc
86+
table_metadata_builder_test.cc)
8887

8988
add_iceberg_test(expression_test
9089
SOURCES

src/iceberg/test/matchers.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <gtest/gtest.h>
2424

2525
#include "iceberg/result.h"
26+
#include "iceberg/util/macros.h"
2627

2728
/*
2829
* \brief Define custom matchers for expected<T, Error> values
@@ -210,4 +211,17 @@ auto ErrorIs(MatcherT&& matcher) {
210211
ResultMatcher<std::decay_t<MatcherT>>(false, std::forward<MatcherT>(matcher)));
211212
}
212213

214+
// Evaluate `rexpr` which should return a Result<T, Error>.
215+
// On success: assign the contained value to `lhs`.
216+
// On failure: fail the test with the error message.
217+
#define ICEBERG_UNWRAP_OR_FAIL_IMPL(result_name, lhs, rexpr) \
218+
auto&& result_name = (rexpr); \
219+
ASSERT_TRUE(result_name.has_value()) \
220+
<< "Operation failed: " << result_name.error().message; \
221+
lhs = std::move(result_name.value());
222+
223+
#define ICEBERG_UNWRAP_OR_FAIL(lhs, rexpr) \
224+
ICEBERG_UNWRAP_OR_FAIL_IMPL(ICEBERG_ASSIGN_OR_RAISE_NAME(result_, __COUNTER__), lhs, \
225+
rexpr)
226+
213227
} // namespace iceberg

src/iceberg/test/meson.build

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ iceberg_tests = {
4949
'schema_json_test.cc',
5050
'table_test.cc',
5151
'test_common.cc',
52+
'table_metadata_builder_test.cc',
5253
),
5354
},
5455
'expression_test': {
@@ -80,9 +81,6 @@ iceberg_tests = {
8081
),
8182
},
8283
'roaring_test': {'sources': files('roaring_test.cc')},
83-
'table_metadata_builder_test': {
84-
'sources': files('table_metadata_builder_test.cc'),
85-
},
8684
}
8785

8886
if get_option('rest').enabled()

0 commit comments

Comments
 (0)