zhjwpku commented on code in PR #401:
URL: https://github.com/apache/iceberg-cpp/pull/401#discussion_r2647078305
##########
src/iceberg/table_metadata.cc:
##########
@@ -572,6 +582,69 @@ Result<int32_t>
TableMetadataBuilder::Impl::AddSortOrder(const SortOrder& order)
return new_order_id;
}
+Status TableMetadataBuilder::Impl::SetDefaultPartitionSpec(int32_t spec_id) {
+ if (spec_id == -1) {
+ if (!last_added_spec_id_.has_value()) {
+ return ValidationFailed(
+ "Cannot set last added partition spec: no partition spec has been
added");
+ }
+ return SetDefaultPartitionSpec(last_added_spec_id_.value());
+ }
+
+ if (spec_id == metadata_.default_spec_id) {
+ // the new spec is already current and no change is needed
+ return {};
+ }
+
+ metadata_.default_spec_id = spec_id;
+ if (last_added_spec_id_ == std::make_optional(spec_id)) {
+
changes_.push_back(std::make_unique<table::SetDefaultPartitionSpec>(kLastAdded));
+ } else {
+
changes_.push_back(std::make_unique<table::SetDefaultPartitionSpec>(spec_id));
+ }
+ return {};
+}
+
+Result<int32_t> TableMetadataBuilder::Impl::AddPartitionSpec(const
PartitionSpec& spec) {
+ int32_t new_spec_id = ReuseOrCreateNewPartitionSpecId(spec);
+
+ if (specs_by_id_.contains(new_spec_id)) {
+ // update last_added_spec_id if the spec was added in this set of changes
(since it
+ // is now the last)
+ bool is_new_spec =
+ last_added_spec_id_.has_value() &&
+ std::ranges::find_if(changes_, [new_spec_id](const auto& change) {
+ return change->kind() == TableUpdate::Kind::kAddPartitionSpec &&
+ internal::checked_cast<const
table::AddPartitionSpec&>(*change)
+ .spec()
+ ->spec_id() == new_spec_id;
+ }) != changes_.cend();
Review Comment:
Good suggestion, I do this refactor in #440, please take a look.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]