diff --git a/pyiceberg/table/update/schema.py b/pyiceberg/table/update/schema.py index 795193cf28..4e8f862e8a 100644 --- a/pyiceberg/table/update/schema.py +++ b/pyiceberg/table/update/schema.py @@ -241,9 +241,11 @@ def add_column( # Table format version 1 and 2 cannot add required column because there is no initial value raise ValueError(f"Incompatible change: cannot add required column: {'.'.join(path)}") - # update tracking for moves - self._added_name_to_id[full_name if self._case_sensitive else full_name.lower()] = new_id - self._id_to_parent[new_id] = parent_full_path + # update tracking for moves, by the path as passed in and by the name in the schema, which can differ + parent_name = self._schema.find_column_name(parent_id) + for added_name in (full_name, f"{parent_name}.{name}" if parent_name is not None else name): + self._added_name_to_id[added_name if self._case_sensitive else added_name.lower()] = new_id + self._id_to_parent[new_id] = parent_name or "" field = NestedField( field_id=new_id, diff --git a/tests/table/test_init.py b/tests/table/test_init.py index f6e0bab93a..f4e0abc412 100644 --- a/tests/table/test_init.py +++ b/tests/table/test_init.py @@ -877,6 +877,82 @@ def test_move_column_deleted_and_added_back_case_insensitive(table_v2: Table) -> ) +def test_move_nested_column_added_in_same_update(table_v2: Table) -> None: + schema = Schema( + NestedField(1, "person", StructType(NestedField(2, "name", StringType()), NestedField(3, "age", IntegerType()))), + NestedField(4, "points", ListType(5, StructType(NestedField(6, "x", DoubleType()), NestedField(7, "y", DoubleType())))), + NestedField( + 8, + "attrs", + MapType(9, StringType(), 10, StructType(NestedField(11, "x", DoubleType()), NestedField(12, "y", DoubleType()))), + ), + ) + + # A list or map as the parent: the added field by the path as passed in and by its name in the schema, also when a + # deleted field had that name + for column, struct, x_id in (("points", "points.element", 6), ("attrs", "attrs.value", 11)): + update = UpdateSchema(transaction=table_v2.transaction(), schema=schema) + update.add_column((column, "z"), DoubleType()) + update.move_before((column, "z"), (column, "x")) + update.delete_column((column, "y")) + update.add_column((column, "y"), StringType()) + update.move_before(f"{struct}.y", f"{struct}.z") + new_schema = update._apply() # pylint: disable=W0212 + + assert new_schema.find_type(struct) == StructType( + NestedField(14, "y", StringType()), NestedField(13, "z", DoubleType()), NestedField(x_id, "x", DoubleType()) + ) + + # The parent struct spelled in another case + update = UpdateSchema(transaction=table_v2.transaction(), schema=schema, case_sensitive=False) + update.add_column(("Person", "email"), StringType()) + update.move_before(("Person", "email"), ("Person", "name")) + new_schema = update._apply() # pylint: disable=W0212 + + assert new_schema.find_type("person") == StructType( + NestedField(13, "email", StringType()), NestedField(2, "name", StringType()), NestedField(3, "age", IntegerType()) + ) + + # A field added to a struct named "" is named ".z" in the schema, so "z" is still the top-level column + schema = Schema(NestedField(1, "", StructType(NestedField(2, "x", IntegerType()))), NestedField(3, "z", IntegerType())) + update = UpdateSchema(transaction=table_v2.transaction(), schema=schema) + update.add_column(("", "z"), StringType()) + update.move_first("z") + + assert update._apply().as_struct() == StructType( # pylint: disable=W0212 + NestedField(3, "z", IntegerType()), + NestedField(1, "", StructType(NestedField(2, "x", IntegerType()), NestedField(4, "z", StringType()))), + ) + + +def test_move_relative_to_nested_column_added_in_same_update(table_v2: Table) -> None: + schema = Schema( + NestedField(1, "points", ListType(2, StructType(NestedField(3, "x", DoubleType()), NestedField(4, "y", DoubleType())))), + NestedField(5, "struct", StructType(NestedField(6, "field1", StringType()))), + ) + + # The added field as the anchor, and two fields added under differently cased parents + update = UpdateSchema(transaction=table_v2.transaction(), schema=schema, case_sensitive=False) + update.add_column(("points", "z"), DoubleType()) + update.move_after(("points", "x"), ("points", "z")) + update.add_column(("struct", "field2"), IntegerType()) + update.add_column(("STRUCT", "field3"), DoubleType()) + update.move_before("STRUCT.FIELD3", "struct.FIELD2") + new_schema = update._apply() # pylint: disable=W0212 + + assert new_schema.find_type("points.element") == StructType( + NestedField(4, "y", DoubleType()), NestedField(7, "z", DoubleType()), NestedField(3, "x", DoubleType()) + ) + assert new_schema.find_type("struct") == StructType( + NestedField(6, "field1", StringType()), NestedField(9, "field3", DoubleType()), NestedField(8, "field2", IntegerType()) + ) + + # A field of another struct is still rejected + update = UpdateSchema(transaction=table_v2.transaction(), schema=schema).add_column(("points", "z"), DoubleType()) + with pytest.raises(ValueError, match="Cannot move field points.z to a different struct"): + update.move_before(("points", "z"), ("struct", "field1")) + + def test_update_column(table_v1: Table, table_v2: Table) -> None: """ Table should be able to update existing property `doc`