diff --git a/src/iceberg/test/update_schema_test.cc b/src/iceberg/test/update_schema_test.cc index 4057c52c8..73d8bd946 100644 --- a/src/iceberg/test/update_schema_test.cc +++ b/src/iceberg/test/update_schema_test.cc @@ -2378,6 +2378,43 @@ TEST_F(UpdateSchemaTest, TestMoveMissingReferenceColumnFails) { EXPECT_THAT(result2, HasErrorMessage("Cannot move y after missing column: items")); } +TEST_F(UpdateSchemaTest, TestMoveWithDeletedColumnFails) { + ICEBERG_UNWRAP_OR_FAIL(auto update1, table_->NewUpdateSchema()); + update1->MoveFirst("z").DeleteColumn("z"); + auto result1 = update1->Apply(); + EXPECT_THAT(result1, IsError(ErrorKind::kValidationFailed)); + EXPECT_THAT(result1, HasErrorMessage("Cannot move missing column with id")); + + ICEBERG_UNWRAP_OR_FAIL(auto update2, table_->NewUpdateSchema()); + update2->SetIdentifierFields({}).MoveBefore("x", "z").DeleteColumn("z"); + auto result2 = update2->Apply(); + EXPECT_THAT(result2, IsError(ErrorKind::kValidationFailed)); + EXPECT_THAT(result2, HasErrorMessage("before missing reference column with id")); + + ICEBERG_UNWRAP_OR_FAIL(auto update3, table_->NewUpdateSchema()); + update3->SetIdentifierFields({}).MoveAfter("x", "z").DeleteColumn("z"); + auto result3 = update3->Apply(); + EXPECT_THAT(result3, IsError(ErrorKind::kValidationFailed)); + EXPECT_THAT(result3, HasErrorMessage("after missing reference column with id")); +} + +TEST_F(UpdateSchemaTest, TestMoveInDeletedStructIsSkipped) { + ICEBERG_UNWRAP_OR_FAIL(auto setup, table_->NewUpdateSchema()); + auto struct_type = std::make_shared(std::vector{ + SchemaField(100, "a", int32(), true), SchemaField(101, "b", int32(), true)}); + setup->AddColumn("s", struct_type); + EXPECT_THAT(setup->Commit(), IsOk()); + + ICEBERG_UNWRAP_OR_FAIL(auto reloaded, catalog_->LoadTable(table_ident_)); + ICEBERG_UNWRAP_OR_FAIL(auto update, reloaded->NewUpdateSchema()); + update->MoveBefore("s.a", "s.b").DeleteColumn("s.b").DeleteColumn("s"); + + ICEBERG_UNWRAP_OR_FAIL(auto result, update->Apply()); + ASSERT_TRUE(result.schema != nullptr); + ICEBERG_UNWRAP_OR_FAIL(auto s_opt, result.schema->FindFieldByName("s")); + EXPECT_FALSE(s_opt.has_value()); +} + TEST_F(UpdateSchemaTest, TestMovePrimitiveMapKeyFails) { ICEBERG_UNWRAP_OR_FAIL(auto setup, table_->NewUpdateSchema()); auto map_type = std::make_shared(SchemaField(98, "key", string(), false), diff --git a/src/iceberg/update/update_schema.cc b/src/iceberg/update/update_schema.cc index 1d8130fba..ab09df610 100644 --- a/src/iceberg/update/update_schema.cc +++ b/src/iceberg/update/update_schema.cc @@ -19,6 +19,7 @@ #include "iceberg/update/update_schema.h" +#include #include #include #include @@ -108,9 +109,11 @@ class ApplyChangesVisitor { } auto moves_it = moves_.find(parent_id); - if (moves_it != moves_.end() && !moves_it->second.empty()) { + if (!deletes_.contains(parent_id) && moves_it != moves_.end() && + !moves_it->second.empty()) { has_changes = true; - new_fields = MoveFields(std::move(new_fields), moves_it->second); + ICEBERG_ASSIGN_OR_RAISE(new_fields, + MoveFields(std::move(new_fields), moves_it->second)); } if (!has_changes) { @@ -225,8 +228,8 @@ class ApplyChangesVisitor { } /// \brief Helper function to apply move operations to a list of fields - static std::vector MoveFields( - std::vector&& fields, const std::vector& moves); + static Result> MoveFields( + std::vector fields, const std::vector& moves); const std::unordered_set& deletes_; const std::unordered_map>& updates_; @@ -234,52 +237,44 @@ class ApplyChangesVisitor { const std::unordered_map>& moves_; }; -std::vector ApplyChangesVisitor::MoveFields( - std::vector&& fields, const std::vector& moves) { - std::vector reordered = std::move(fields); - +Result> ApplyChangesVisitor::MoveFields( + std::vector fields, const std::vector& moves) { for (const auto& move : moves) { - auto it = std::ranges::find_if(reordered, [&move](const SchemaField& field) { - return field.field_id() == move.field_id; - }); - - if (it == reordered.end()) { - continue; - } + auto it = std::ranges::find(fields, move.field_id, &SchemaField::field_id); + ICEBERG_CHECK(it != fields.end(), "Cannot move missing column with id: {}", + move.field_id); - SchemaField to_move = *it; - reordered.erase(it); + SchemaField to_move = std::move(*it); + fields.erase(it); + auto insert_it = fields.begin(); switch (move.type) { case UpdateSchema::Move::MoveType::kFirst: - reordered.insert(reordered.begin(), std::move(to_move)); break; - - case UpdateSchema::Move::MoveType::kBefore: { - auto before_it = - std::ranges::find_if(reordered, [&move](const SchemaField& field) { - return field.field_id() == move.reference_field_id; - }); - if (before_it != reordered.end()) { - reordered.insert(before_it, std::move(to_move)); - } + case UpdateSchema::Move::MoveType::kBefore: + insert_it = + std::ranges::find(fields, move.reference_field_id, &SchemaField::field_id); + ICEBERG_CHECK( + insert_it != fields.end(), + "Cannot move column with id {} before missing reference column with id {}", + move.field_id, move.reference_field_id); break; - } - - case UpdateSchema::Move::MoveType::kAfter: { - auto after_it = - std::ranges::find_if(reordered, [&move](const SchemaField& field) { - return field.field_id() == move.reference_field_id; - }); - if (after_it != reordered.end()) { - reordered.insert(after_it + 1, std::move(to_move)); - } + case UpdateSchema::Move::MoveType::kAfter: + insert_it = + std::ranges::find(fields, move.reference_field_id, &SchemaField::field_id); + ICEBERG_CHECK( + insert_it != fields.end(), + "Cannot move column with id {} after missing reference column with id {}", + move.field_id, move.reference_field_id); + ++insert_it; break; - } + default: + return NotSupported("Unsupported move type: {}", static_cast(move.type)); } + fields.insert(insert_it, std::move(to_move)); } - return reordered; + return fields; } bool ValidateDefaultLiteral(const Literal& value) {