diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index f5ddaee8f4..f4a36264e3 100644 --- a/pyiceberg/io/pyarrow.py +++ b/pyiceberg/io/pyarrow.py @@ -922,10 +922,15 @@ def _literal_arrow_type(iceberg_type: IcebergType, values: Iterable[Any]) -> pa. A decimal literal can have more digits than the column, e.g. 1000.00 for decimal(5, 2), so the precision is widened to hold every literal. The scale always stays the column's. + Past the 38 digits of decimal128, e.g. 1 for decimal(38, 38), the literal is a decimal256 + (up to 76 digits), which Arrow compares with the decimal128 column by value. """ if isinstance(iceberg_type, DecimalType): digits = [len(value.as_tuple().digits) for value in values if isinstance(value, Decimal)] - return pa.decimal128(max([iceberg_type.precision, *digits]), iceberg_type.scale) + precision = max([iceberg_type.precision, *digits]) + if precision > 38: + return pa.decimal256(precision, iceberg_type.scale) + return pa.decimal128(precision, iceberg_type.scale) return visit(iceberg_type, _ConvertToArrowSchema()) diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index 5c1933fc20..9851c9da91 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -1095,6 +1095,51 @@ def test_row_filter_decimal_literal_wider_than_column(catalog: InMemoryCatalog) assert sorted(table.scan(row_filter=row_filter).to_arrow()["id"].to_pylist()) == expected, row_filter +def test_decimal_literal_wider_than_decimal128_to_pyarrow() -> None: + schema = Schema(NestedField(1, "amt", DecimalType(38, 18), required=False)) + ref = BoundReference(schema.find_field(1), schema.accessor_for_field(1)) + # 10**20 has 39 digits at scale 18, one more than decimal128 holds + wide = literal(10**20).to(DecimalType(38, 18)) + narrow = literal(-3).to(DecimalType(38, 18)) + arrow_table = pa.table({"amt": pa.array([Decimal("12.5"), Decimal("-3")], pa.decimal128(38, 18))}) + for bound_expr, expected in [ + (BoundLessThan(term=ref, literal=wide), [Decimal("12.5"), Decimal("-3")]), + (BoundNotEqualTo(term=ref, literal=wide), [Decimal("12.5"), Decimal("-3")]), + (BoundIn(term=ref, literals={wide, narrow}), [Decimal("-3")]), + (BoundNotIn(term=ref, literals={wide, narrow}), [Decimal("12.5")]), + ]: + assert arrow_table.filter(expression_to_pyarrow(bound_expr))["amt"].to_pylist() == expected, bound_expr + + +def test_row_filter_decimal_literal_wider_than_decimal128(catalog: InMemoryCatalog) -> None: + catalog.create_namespace("default") + table = catalog.create_table( + "default.test_decimal_literal_wider_than_decimal128", + schema=Schema( + NestedField(1, "id", IntegerType(), required=False), + NestedField(2, "ratio", DecimalType(38, 38), required=False), + NestedField(3, "amount", DecimalType(38, 18), required=False), + ), + ) + table.append( + pa.table( + { + "id": pa.array([1, 2, 3], pa.int32()), + "ratio": pa.array([Decimal("0.5"), Decimal("-0.25"), None], pa.decimal128(38, 38)), + "amount": pa.array([Decimal("12.5"), Decimal("-3"), None], pa.decimal128(38, 18)), + } + ) + ) + + # At the column's scale, 1 and 10**20 have 39 digits, one more than decimal128 holds + for row_filter, expected in [ + (LessThan("ratio", 1), [1, 2]), + (LessThan("amount", 10**20), [1, 2]), + (In("amount", {10**20, -3}), [2]), + ]: + assert sorted(table.scan(row_filter=row_filter).to_arrow()["id"].to_pylist()) == expected, row_filter + + @pytest.fixture def schema_int() -> Schema: return Schema(NestedField(1, "id", IntegerType(), required=False))