Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/iceberg/test/update_schema_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<StructType>(std::vector<SchemaField>{
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<MapType>(SchemaField(98, "key", string(), false),
Expand Down
73 changes: 34 additions & 39 deletions src/iceberg/update/update_schema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "iceberg/update/update_schema.h"

#include <algorithm>
#include <format>
#include <memory>
#include <optional>
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -225,61 +228,53 @@ class ApplyChangesVisitor {
}

/// \brief Helper function to apply move operations to a list of fields
static std::vector<SchemaField> MoveFields(
std::vector<SchemaField>&& fields, const std::vector<UpdateSchema::Move>& moves);
static Result<std::vector<SchemaField>> MoveFields(
std::vector<SchemaField> fields, const std::vector<UpdateSchema::Move>& moves);

const std::unordered_set<int32_t>& deletes_;
const std::unordered_map<int32_t, std::shared_ptr<SchemaField>>& updates_;
const std::unordered_map<int32_t, std::vector<int32_t>>& parent_to_added_ids_;
const std::unordered_map<int32_t, std::vector<UpdateSchema::Move>>& moves_;
};

std::vector<SchemaField> ApplyChangesVisitor::MoveFields(
std::vector<SchemaField>&& fields, const std::vector<UpdateSchema::Move>& moves) {
std::vector<SchemaField> reordered = std::move(fields);

Result<std::vector<SchemaField>> ApplyChangesVisitor::MoveFields(
std::vector<SchemaField> fields, const std::vector<UpdateSchema::Move>& 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;
Comment thread
WZhuo marked this conversation as resolved.
Resolved
}
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<int>(move.type));
}
fields.insert(insert_it, std::move(to_move));
}

return reordered;
return fields;
}

bool ValidateDefaultLiteral(const Literal& value) {
Expand Down
Loading