From 8c81d035d7e49d43eab2aad0e40d348a92eb11b8 Mon Sep 17 00:00:00 2001 From: rahulsmahadev Date: Wed, 2 Dec 2026 17:50:28 +0000 Subject: [PATCH] Accept INT32 and INT64 decimals for wider decimal columns in add_files StatsAggregator compared a decimal column's Parquet physical type with the one PyIceberg writes for the table's precision, so add_files rejected a decimal(18, 2) file stored as INT64 for a decimal(38, 2) column. Allow INT32, INT64 and FIXED_LEN_BYTE_ARRAY for any decimal, as the comment above the check says; the statistics of all three are already decoded with the column's scale. Co-authored-by: Isaac --- pyiceberg/io/pyarrow.py | 4 ++-- tests/io/test_pyarrow.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index 897a12dcde..dec34480e7 100644 --- a/pyiceberg/io/pyarrow.py +++ b/pyiceberg/io/pyarrow.py @@ -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( diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index c61c34433f..1fe1445d2e 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -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: @@ -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( @@ -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(