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
6 changes: 5 additions & 1 deletion pyiceberg/expressions/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from datetime import date, datetime, time
from decimal import ROUND_HALF_UP, Decimal
from functools import singledispatchmethod
from math import isnan
from math import isfinite, isnan
from typing import Any, Generic
from uuid import UUID

Expand Down Expand Up @@ -414,6 +414,8 @@ def _(self, _: DoubleType) -> Literal[float]:

@to.register(DecimalType)
def _(self, type_var: DecimalType) -> Literal[Decimal]:
if not isfinite(self.value):
raise ValueError(f"Could not convert {self.value} into a {type_var}, value is not finite")
return DecimalLiteral(Decimal(self.value).quantize(Decimal((0, (1,), -type_var.scale)), rounding=ROUND_HALF_UP))


Expand All @@ -439,6 +441,8 @@ def _(self, _: FloatType) -> Literal[float]:

@to.register(DecimalType)
def _(self, type_var: DecimalType) -> Literal[Decimal]:
if not isfinite(self.value):
raise ValueError(f"Could not convert {self.value} into a {type_var}, value is not finite")
return DecimalLiteral(Decimal(self.value).quantize(Decimal((0, (1,), -type_var.scale)), rounding=ROUND_HALF_UP))


Expand Down
16 changes: 16 additions & 0 deletions tests/expressions/test_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,22 @@ def test_double_to_decimal_conversion(decimal_type: DecimalType, decimal_value:
assert lit.to(decimal_type).value.as_tuple() == Decimal(decimal_value).as_tuple() # type: ignore


def test_float_to_decimal_conversion_with_infinity() -> None:
lit = FloatLiteral(float("inf"))

with pytest.raises(ValueError) as e:
_ = lit.to(DecimalType(9, 2))
assert "Could not convert inf into a decimal(9, 2), value is not finite" in str(e.value)


def test_double_to_decimal_conversion_with_infinity() -> None:
lit = DoubleLiteral(float("inf"))

with pytest.raises(ValueError) as e:
_ = lit.to(DecimalType(9, 2))
assert "Could not convert inf into a decimal(9, 2), value is not finite" in str(e.value)


def test_decimal_to_decimal_conversion() -> None:
lit = literal(Decimal("34.11").quantize(Decimal(".01")))

Expand Down
Loading