@@ -429,7 +429,7 @@ class TableMetadataBuilder::Impl {
429429 Status SetProperties (const std::unordered_map<std::string, std::string>& updated);
430430 Status RemoveProperties (const std::unordered_set<std::string>& removed);
431431 Status SetCurrentSchema (int32_t schema_id);
432- Status RemoveSchemas (const std::vector <int32_t >& schema_ids);
432+ Status RemoveSchemas (const std::unordered_set <int32_t >& schema_ids);
433433 Result<int32_t > AddSchema (const Schema& schema, int32_t new_last_column_id);
434434
435435 Result<std::unique_ptr<TableMetadata>> Build ();
@@ -634,7 +634,7 @@ Status TableMetadataBuilder::Impl::RemoveProperties(
634634Status TableMetadataBuilder::Impl::SetCurrentSchema (int32_t schema_id) {
635635 if (schema_id == kLastAdded ) {
636636 if (!last_added_schema_id_.has_value ()) {
637- return InvalidArgument (" Cannot set last added schema: no schema has been added" );
637+ return ValidationFailed (" Cannot set last added schema: no schema has been added" );
638638 }
639639 return SetCurrentSchema (last_added_schema_id_.value ());
640640 }
@@ -686,57 +686,55 @@ Status TableMetadataBuilder::Impl::SetCurrentSchema(int32_t schema_id) {
686686 return {};
687687}
688688
689- Status TableMetadataBuilder::Impl::RemoveSchemas (const std::vector< int32_t >& schema_ids) {
690- std::unordered_set<int32_t > schema_ids_to_remove ( schema_ids. begin (), schema_ids. end ());
689+ Status TableMetadataBuilder::Impl::RemoveSchemas (
690+ const std::unordered_set<int32_t >& schema_ids) {
691691 auto current_schema_id = metadata_.current_schema_id .value_or (Schema::kInitialSchemaId );
692- if (!schema_ids_to_remove.contains (current_schema_id)) {
693- return InvalidArgument (" Cannot remove current schema: {}" , current_schema_id);
694- }
692+ ICEBERG_PRECHECK (!schema_ids.contains (current_schema_id),
693+ " Cannot remove current schema: {}" , current_schema_id);
695694
696- if (!schema_ids_to_remove .empty ()) {
695+ if (!schema_ids .empty ()) {
697696 metadata_.schemas = metadata_.schemas | std::views::filter ([&](const auto & schema) {
698- return !schema_ids_to_remove .contains (
697+ return !schema_ids .contains (
699698 schema->schema_id ().value_or (Schema::kInitialSchemaId ));
700699 }) |
701700 std::ranges::to<std::vector<std::shared_ptr<Schema>>>();
702- changes_.push_back (
703- std::make_unique<table::RemoveSchemas>(std::move (schema_ids_to_remove)));
701+ changes_.push_back (std::make_unique<table::RemoveSchemas>(schema_ids));
704702 }
705703
706704 return {};
707705}
708706
709707Result<int32_t > TableMetadataBuilder::Impl::AddSchema (const Schema& schema,
710708 int32_t new_last_column_id) {
711- if (new_last_column_id < metadata_.last_column_id ) {
712- return InvalidArgument (" Invalid last column ID: {} < {} (previous last column ID)" ,
713- new_last_column_id, metadata_.last_column_id );
714- }
709+ ICEBERG_PRECHECK (new_last_column_id >= metadata_.last_column_id ,
710+ " Invalid last column ID: {} < {} (previous last column ID)" ,
711+ new_last_column_id, metadata_.last_column_id );
715712
716713 ICEBERG_RETURN_UNEXPECTED (schema.Validate (metadata_.format_version ));
717714
718715 auto new_schema_id = ReuseOrCreateNewSchemaId (schema);
719- if (schemas_by_id_.find (new_schema_id) != schemas_by_id_.end ()) {
716+ if (schemas_by_id_.find (new_schema_id) != schemas_by_id_.end () &&
717+ new_last_column_id == metadata_.last_column_id ) {
720718 // update last_added_schema_id if the schema was added in this set of changes (since
721719 // it is now the last)
722720 bool is_new_schema =
723721 last_added_schema_id_.has_value () &&
724- std::ranges::find_if (changes_, [new_schema_id](const auto & change) {
722+ std::ranges::any_of (changes_, [new_schema_id](const auto & change) {
725723 if (change->kind () != TableUpdate::Kind::kAddSchema ) {
726724 return false ;
727725 }
728726 auto * add_schema = dynamic_cast <table::AddSchema*>(change.get ());
729727 return add_schema->schema ()->schema_id ().value_or (Schema::kInitialSchemaId ) ==
730728 new_schema_id;
731- }) != changes_. cend () ;
729+ });
732730 last_added_schema_id_ =
733731 is_new_schema ? std::make_optional (new_schema_id) : std::nullopt ;
734732 return new_schema_id;
735733 }
736734
737- auto new_schema = std::make_shared<Schema>(
738- std::vector<SchemaField >(schema.fields (). begin (), schema. fields (). end () ),
739- new_schema_id);
735+ auto new_schema =
736+ std::make_shared<Schema >(schema.fields () | std::ranges::to<std::vector>( ),
737+ new_schema_id, schema. IdentifierFieldIds () );
740738
741739 metadata_.schemas .push_back (new_schema);
742740 schemas_by_id_.emplace (new_schema_id, new_schema);
@@ -934,8 +932,7 @@ TableMetadataBuilder& TableMetadataBuilder::AddSchema(
934932 std::shared_ptr<Schema> const & schema) {
935933 ICEBERG_BUILDER_ASSIGN_OR_RETURN (auto highest_field_id, schema->HighestFieldId ());
936934 auto new_last_column_id = std::max (impl_->metadata ().last_column_id , highest_field_id);
937- ICEBERG_BUILDER_ASSIGN_OR_RETURN (auto schema_id,
938- impl_->AddSchema (*schema, new_last_column_id));
935+ ICEBERG_BUILDER_RETURN_IF_ERROR (impl_->AddSchema (*schema, new_last_column_id));
939936 return *this ;
940937}
941938
@@ -959,7 +956,7 @@ TableMetadataBuilder& TableMetadataBuilder::RemovePartitionSpecs(
959956}
960957
961958TableMetadataBuilder& TableMetadataBuilder::RemoveSchemas (
962- const std::vector <int32_t >& schema_ids) {
959+ const std::unordered_set <int32_t >& schema_ids) {
963960 ICEBERG_BUILDER_RETURN_IF_ERROR (impl_->RemoveSchemas (schema_ids));
964961 return *this ;
965962}
0 commit comments