Skip to content

Commit 4035747

Browse files
authored
Bug Fix: Use historical partition field name (#1161)
* use historical partition field name * thanks ndrluis!
1 parent 67e8773 commit 4035747

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

pyiceberg/table/update/spec.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,12 @@ def _partition_field(self, transform_key: Tuple[int, Transform[Any, Any]], name:
275275
historical_fields = []
276276
for spec in self._transaction.table_metadata.specs().values():
277277
for field in spec.fields:
278-
historical_fields.append((field.source_id, field.field_id, repr(field.transform), field.name))
278+
historical_fields.append(field)
279279

280-
for field_key in historical_fields:
281-
if field_key[0] == source_id and field_key[2] == repr(transform):
282-
if name is None or field_key[3] == name:
283-
return PartitionField(source_id, field_key[1], transform, name)
280+
for field in historical_fields:
281+
if field.source_id == source_id and repr(field.transform) == repr(transform):
282+
if name is None or field.name == name:
283+
return PartitionField(source_id, field.field_id, transform, field.name)
284284

285285
new_field_id = self._new_field_id()
286286
if name is None:

tests/integration/test_partition_evolution.py

+32
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,38 @@ def test_remove_identity_v2(catalog: Catalog) -> None:
207207
assert table_v2.spec() == PartitionSpec(spec_id=0)
208208

209209

210+
@pytest.mark.integration
211+
@pytest.mark.parametrize("catalog", [pytest.lazy_fixture("session_catalog_hive"), pytest.lazy_fixture("session_catalog")])
212+
def test_remove_and_add_identity(catalog: Catalog) -> None:
213+
table = _table(catalog)
214+
table.update_spec().add_identity("id").commit()
215+
table.update_spec().remove_field("id").commit()
216+
table.update_spec().add_identity("id").commit()
217+
218+
assert len(table.specs()) == 4
219+
assert table.spec().spec_id == 3
220+
assert table.spec() == PartitionSpec(
221+
PartitionField(source_id=1, field_id=1000, transform=VoidTransform(), name="id_1000"),
222+
PartitionField(source_id=1, field_id=1001, transform=IdentityTransform(), name="id"),
223+
spec_id=3,
224+
)
225+
226+
227+
@pytest.mark.integration
228+
@pytest.mark.parametrize("catalog", [pytest.lazy_fixture("session_catalog_hive"), pytest.lazy_fixture("session_catalog")])
229+
def test_remove_and_add_identity_v2(catalog: Catalog) -> None:
230+
table_v2 = _table_v2(catalog)
231+
table_v2.update_spec().add_identity("id").commit()
232+
table_v2.update_spec().remove_field("id").commit()
233+
table_v2.update_spec().add_identity("id").commit()
234+
235+
assert len(table_v2.specs()) == 2
236+
assert table_v2.spec().spec_id == 1
237+
assert table_v2.spec() == PartitionSpec(
238+
PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="id"), spec_id=1
239+
)
240+
241+
210242
@pytest.mark.integration
211243
@pytest.mark.parametrize("catalog", [pytest.lazy_fixture("session_catalog_hive"), pytest.lazy_fixture("session_catalog")])
212244
def test_remove_bucket(catalog: Catalog) -> None:

0 commit comments

Comments
 (0)