Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyiceberg/io/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2596,8 +2596,8 @@ def __init__(self, iceberg_type: PrimitiveType, physical_type_string: str, trunc
physical_type_string == "FLOAT" and expected_physical_type == "DOUBLE"
):
pass
# Allow DECIMAL to be stored as FIXED_LEN_BYTE_ARRAY, INT32 or INT64
elif physical_type_string == "FIXED_LEN_BYTE_ARRAY" and expected_physical_type in ("INT32", "INT64"):
# Allow DECIMAL to be stored as FIXED_LEN_BYTE_ARRAY, INT32 or INT64, also after its precision is widened
elif isinstance(iceberg_type, DecimalType) and physical_type_string in ("FIXED_LEN_BYTE_ARRAY", "INT32", "INT64"):
pass
else:
raise ValueError(
Expand Down
37 changes: 37 additions & 0 deletions tests/io/test_pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3162,6 +3162,12 @@ def test_stats_aggregator_orders_negative_zero_below_zero(vals: list[float], pri
(DecimalType(precision=2, scale=2), "FIXED_LEN_BYTE_ARRAY"),
# Allowed FIXED_LEN_BYTE_ARRAY -> INT64
(DecimalType(precision=12, scale=2), "FIXED_LEN_BYTE_ARRAY"),
# Allowed INT64 -> INT32
(DecimalType(precision=9, scale=2), "INT64"),
# Allowed INT32 -> FIXED_LEN_BYTE_ARRAY
(DecimalType(precision=38, scale=2), "INT32"),
# Allowed INT64 -> FIXED_LEN_BYTE_ARRAY
(DecimalType(precision=38, scale=2), "INT64"),
],
)
def test_stats_aggregator_conditionally_allowed_types_pass(iceberg_type: PrimitiveType, physical_type_string: str) -> None:
Expand All @@ -3177,6 +3183,10 @@ def test_stats_aggregator_conditionally_allowed_types_pass(iceberg_type: Primiti
[
# Fail case: INT64 cannot be cast to INT32
(IntegerType(), "INT64"),
# Fail case: only DECIMAL can be stored as FIXED_LEN_BYTE_ARRAY instead of INT64
(LongType(), "FIXED_LEN_BYTE_ARRAY"),
# Fail case: the statistics of a DECIMAL stored as BYTE_ARRAY are not decoded
(DecimalType(precision=38, scale=2), "BYTE_ARRAY"),
],
)
def test_stats_aggregator_physical_type_does_not_match_expected_raise_error(
Expand Down Expand Up @@ -3618,6 +3628,33 @@ def test_add_files_rejects_columns_missing_from_table(catalog: InMemoryCatalog,
catalog.load_table(identifier).add_files([file_loc])


def test_add_files_int_stored_decimal_into_wider_decimal(catalog: InMemoryCatalog, tmp_path: str) -> None:
# Files written for decimal(9, 2) and decimal(18, 2) store the column as INT32 and INT64. Both stay valid for a
# wider decimal column, which PyIceberg itself writes as FIXED_LEN_BYTE_ARRAY.
catalog.create_namespace("default")
values = [Decimal("1.25"), Decimal("-3.50")]
for file_type, physical_type, table_type in (
(pa.decimal128(9, 2), "INT32", DecimalType(20, 2)),
(pa.decimal128(18, 2), "INT64", DecimalType(38, 2)),
):
identifier = f"default.add_files_{physical_type.lower()}_decimal"
catalog.create_table(identifier, schema=Schema(NestedField(1, "amount", table_type, required=False)))
file_loc = f"{tmp_path}/{physical_type.lower()}_decimal.parquet"
pq.write_table(pa.table({"amount": pa.array(values, file_type)}), file_loc, store_decimal_as_integer=True)
assert pq.read_metadata(file_loc).row_group(0).column(0).physical_type == physical_type

catalog.load_table(identifier).add_files([file_loc])

table = catalog.load_table(identifier)
data_file = next(iter(table.scan().plan_files())).file
assert data_file.lower_bounds[1] == to_bytes(table_type, Decimal("-3.50"))
assert data_file.upper_bounds[1] == to_bytes(table_type, Decimal("1.25"))
assert list(table.scan(row_filter="amount > 1.25").plan_files()) == []
result = table.scan(row_filter="amount < 0").to_arrow()["amount"]
assert result.type == pa.decimal128(table_type.precision, table_type.scale)
assert result.to_pylist() == [Decimal("-3.50")]


def test_partition_for_demo() -> None:
test_pa_schema = pa.schema([("year", pa.int64()), ("n_legs", pa.int64()), ("animal", pa.string())])
test_schema = Schema(
Expand Down
Loading