Skip to content

Commit db62f03

Browse files
committed
Add unit test and address comments
1 parent 2262b51 commit db62f03

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

pyiceberg/io/pyarrow.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,9 +779,7 @@ def visit_uuid(self, _: UUIDType) -> pa.DataType:
779779
return pa.uuid()
780780

781781
def visit_unknown(self, _: UnknownType) -> pa.DataType:
782-
"""
783-
UnknownType can be promoted to any primitive type in V3+ tables per the Iceberg spec
784-
"""
782+
"""Type `UnknownType` can be promoted to any primitive type in V3+ tables per the Iceberg spec."""
785783
return pa.null()
786784

787785
def visit_binary(self, _: BinaryType) -> pa.DataType:

pyiceberg/schema.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,13 +1769,13 @@ def _is_field_compatible(self, lhs: NestedField) -> bool:
17691769
promote(rhs.field_type, lhs.field_type)
17701770
self.rich_table.add_row("✅", str(lhs), str(rhs))
17711771
return True
1772-
except ResolveError as e:
1772+
except ResolveError:
17731773
# UnknownType can only be promoted to Primitive types
17741774
if isinstance(rhs.field_type, UnknownType):
1775-
if isinstance(lhs.field_type, (ListType, MapType, StructType)):
1776-
error_msg = f"PyArrow null type (UnknownType) cannot be promoted to non-primitive type {lhs.field_type}. UnknownType can only be promoted to primitive types (string, int, boolean, etc.) in V3+ tables."
1775+
if not isinstance(lhs.field_type, PrimitiveType):
1776+
error_msg = f"Null type (UnknownType) cannot be promoted to non-primitive type {lhs.field_type}. UnknownType can only be promoted to primitive types (string, int, boolean, etc.) in V3+ tables."
17771777
else:
1778-
error_msg = f"PyArrow null type (UnknownType) cannot be promoted to {lhs.field_type}. This may be due to table format version limitations (V1/V2 tables don't support UnknownType promotion)."
1778+
error_msg = f"Null type (UnknownType) cannot be promoted to {lhs.field_type}. This may be due to table format version limitations (V1/V2 tables don't support UnknownType promotion)."
17791779
self.rich_table.add_row("❌", str(lhs), f"{str(rhs)} - {error_msg}")
17801780
else:
17811781
self.rich_table.add_row("❌", str(lhs), str(rhs))

tests/test_schema.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
TimestampType,
5555
TimestamptzType,
5656
TimeType,
57+
UnknownType,
5758
UUIDType,
5859
)
5960

@@ -919,6 +920,36 @@ def test_promotion(file_type: IcebergType, read_type: IcebergType) -> None:
919920
promote(file_type, read_type)
920921

921922

923+
def test_unknown_type_promotion_to_primitive() -> None:
924+
"""Test that UnknownType can be promoted to primitive types (V3+ behavior)"""
925+
unknown_type = UnknownType()
926+
927+
assert promote(unknown_type, StringType()) == StringType()
928+
assert promote(unknown_type, IntegerType()) == IntegerType()
929+
assert promote(unknown_type, BooleanType()) == BooleanType()
930+
assert promote(unknown_type, FloatType()) == FloatType()
931+
932+
933+
def test_unknown_type_promotion_to_non_primitive_raises_resolve_error() -> None:
934+
"""Test that UnknownType cannot be promoted to non-primitive types and raises ResolveError"""
935+
unknown_type = UnknownType()
936+
937+
with pytest.raises(ResolveError) as exc_info:
938+
promote(unknown_type, ListType(element_id=1, element_type=StringType(), element_required=False))
939+
940+
assert "Cannot promote unknown to list<string>" in str(exc_info.value)
941+
942+
with pytest.raises(ResolveError) as exc_info:
943+
promote(unknown_type, MapType(key_id=1, key_type=StringType(), value_id=2, value_type=StringType(), value_required=False))
944+
945+
assert "Cannot promote unknown to map<string, string>" in str(exc_info.value)
946+
947+
with pytest.raises(ResolveError) as exc_info:
948+
promote(unknown_type, StructType(NestedField(field_id=1, name="field", field_type=StringType(), required=False)))
949+
950+
assert "Cannot promote unknown to struct<1: field: optional string>" in str(exc_info.value)
951+
952+
922953
@pytest.fixture()
923954
def primitive_fields() -> List[NestedField]:
924955
return [

0 commit comments

Comments
 (0)