Skip to content

Commit 0b487f2

Browse files
TiansuYuTiansu Yusungwy
authored
fix (issue-1079): allow update_column to set doc as '' (#1083)
* fix (issue-1079): allow update_column to set doc as '' * fix (issue-1079): add test for required and fix version * Apply suggestions from code review Co-authored-by: Sung Yun <[email protected]> * Apply suggestions from code review --------- Co-authored-by: Tiansu Yu <tiansu.yu@icloud> Co-authored-by: Sung Yun <[email protected]>
1 parent 87d52d3 commit 0b487f2

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

pyiceberg/table/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,20 +2492,21 @@ def update_column(
24922492
except ResolveError as e:
24932493
raise ValidationError(f"Cannot change column type: {full_name}: {field.field_type} -> {field_type}") from e
24942494

2495+
# if other updates for the same field exist in one transaction:
24952496
if updated := self._updates.get(field.field_id):
24962497
self._updates[field.field_id] = NestedField(
24972498
field_id=updated.field_id,
24982499
name=updated.name,
24992500
field_type=field_type or updated.field_type,
2500-
doc=doc or updated.doc,
2501+
doc=doc if doc is not None else updated.doc,
25012502
required=updated.required,
25022503
)
25032504
else:
25042505
self._updates[field.field_id] = NestedField(
25052506
field_id=field.field_id,
25062507
name=field.name,
25072508
field_type=field_type or field.field_type,
2508-
doc=doc or field.doc,
2509+
doc=doc if doc is not None else field.doc,
25092510
required=field.required,
25102511
)
25112512

@@ -2878,7 +2879,7 @@ def _update_column(self, field: NestedField, existing_field: NestedField) -> Non
28782879
if field.field_type.is_primitive and field.field_type != existing_field.field_type:
28792880
self.update_schema.update_column(full_name, field_type=field.field_type)
28802881

2881-
if field.doc is not None and not field.doc != existing_field.doc:
2882+
if field.doc is not None and field.doc != existing_field.doc:
28822883
self.update_schema.update_column(full_name, doc=field.doc)
28832884

28842885
def _find_field_type(self, field_id: int) -> IcebergType:

tests/table/test_init.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,41 @@ def test_add_column(table_v2: Table) -> None:
512512
assert apply_schema.highest_field_id == 4
513513

514514

515+
def test_update_column(table_v1: Table, table_v2: Table) -> None:
516+
"""
517+
Table should be able to update existing property `doc`
518+
Table should also be able to update property `required`, if the field is not an identifier field.
519+
"""
520+
COMMENT2 = "comment2"
521+
for table in [table_v1, table_v2]:
522+
original_schema = table.schema()
523+
# update existing doc to a new doc
524+
assert original_schema.find_field("y").doc == "comment"
525+
new_schema = table.transaction().update_schema().update_column("y", doc=COMMENT2)._apply()
526+
assert new_schema.find_field("y").doc == COMMENT2, "failed to update existing field doc"
527+
528+
# update existing doc to an emtpy string
529+
assert new_schema.find_field("y").doc == COMMENT2
530+
new_schema2 = table.transaction().update_schema().update_column("y", doc="")._apply()
531+
assert new_schema2.find_field("y").doc == "", "failed to remove existing field doc"
532+
533+
# update required to False
534+
assert original_schema.find_field("z").required is True
535+
new_schema3 = table.transaction().update_schema().update_column("z", required=False)._apply()
536+
assert new_schema3.find_field("z").required is False, "failed to update existing field required"
537+
538+
# assert the above two updates also works with union_by_name
539+
assert (
540+
table.update_schema().union_by_name(new_schema)._apply() == new_schema
541+
), "failed to update existing field doc with union_by_name"
542+
assert (
543+
table.update_schema().union_by_name(new_schema2)._apply() == new_schema2
544+
), "failed to remove existing field doc with union_by_name"
545+
assert (
546+
table.update_schema().union_by_name(new_schema3)._apply() == new_schema3
547+
), "failed to update existing field required with union_by_name"
548+
549+
515550
def test_add_primitive_type_column(table_v2: Table) -> None:
516551
primitive_type: Dict[str, PrimitiveType] = {
517552
"boolean": BooleanType(),

0 commit comments

Comments
 (0)