Skip to content
Closed
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
7 changes: 6 additions & 1 deletion pyiceberg/io/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +931 to +932

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this in conflict with the spec: https://iceberg.apache.org/spec/#primitive-types

It explicitly states:

Scale is fixed, precision must be 38 or less

return pa.decimal128(precision, iceberg_type.scale)
return visit(iceberg_type, _ConvertToArrowSchema())


Expand Down
45 changes: 45 additions & 0 deletions tests/io/test_pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down