Skip to content

Commit 0b3a04c

Browse files
committed
fix and tests
1 parent 6da06ad commit 0b3a04c

2 files changed

Lines changed: 6 additions & 4 deletions

File tree

pyiceberg/io/pyarrow.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,13 +1970,14 @@ def _cast_if_needed(self, field: NestedField, values: pa.Array) -> pa.Array:
19701970
return values.cast(target_type)
19711971
raise ValueError(f"Unsupported schema projection from {values.type} to {target_type}")
19721972
elif isinstance(field.field_type, (IntegerType, LongType)):
1973-
# Cast smaller integer types to target type for cross-platform compatibility
1974-
# Only allow widening conversions (smaller bit width to larger)
1975-
# Narrowing conversions fall through to promote() handling below
1973+
# Cast integer types for cross-platform compatibility (e.g. Spark reads):
1974+
# widening (smaller bit width to larger) and unsigned-to-signed at same width
19761975
if pa.types.is_integer(values.type):
19771976
source_width = values.type.bit_width
19781977
target_width = target_type.bit_width
1979-
if source_width < target_width:
1978+
if source_width < target_width or (
1979+
pa.types.is_unsigned_integer(values.type) and source_width <= target_width
1980+
):
19801981
return values.cast(target_type)
19811982

19821983
if field.field_type != file_field.field_type:

tests/io/test_pyarrow.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3084,6 +3084,7 @@ def test__to_requested_schema_timestamps_without_downcast_raises_exception(
30843084
(pa.int8(), IntegerType(), pa.int32()),
30853085
(pa.int16(), IntegerType(), pa.int32()),
30863086
(pa.uint16(), IntegerType(), pa.int32()),
3087+
(pa.uint32(), IntegerType(), pa.int32()),
30873088
(pa.uint32(), LongType(), pa.int64()),
30883089
(pa.int32(), LongType(), pa.int64()),
30893090
],

0 commit comments

Comments
 (0)