Skip to content

Commit 88cd4e3

Browse files
committed
fix(expressions): reject non-finite floats on conversion to Decimal
float('inf') is a valid float/double literal, but converting it to a Decimal type crashed with a bare decimal.InvalidOperation instead of the ValueError every other impossible conversion in this module raises. NaN is already rejected at literal construction; infinity now gets a clean, consistent domain error. Signed-off-by: Ayush Kumar Saha <225407166+AP24110011695@users.noreply.github.com>
1 parent e3564a5 commit 88cd4e3

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎pyiceberg/expressions/literals.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from datetime import date, datetime, time
2727
from decimal import ROUND_HALF_UP, Decimal
2828
from functools import singledispatchmethod
29-
from math import isnan
29+
from math import isfinite, isnan
3030
from typing import Any, Generic
3131
from uuid import UUID
3232

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

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

419421

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

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

444448

‎tests/expressions/test_literals.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,22 @@ def test_double_to_decimal_conversion(decimal_type: DecimalType, decimal_value:
277277
assert lit.to(decimal_type).value.as_tuple() == Decimal(decimal_value).as_tuple() # type: ignore
278278

279279

280+
def test_float_to_decimal_conversion_with_infinity() -> None:
281+
lit = FloatLiteral(float("inf"))
282+
283+
with pytest.raises(ValueError) as e:
284+
_ = lit.to(DecimalType(9, 2))
285+
assert "Could not convert inf into a decimal(9, 2), value is not finite" in str(e.value)
286+
287+
288+
def test_double_to_decimal_conversion_with_infinity() -> None:
289+
lit = DoubleLiteral(float("inf"))
290+
291+
with pytest.raises(ValueError) as e:
292+
_ = lit.to(DecimalType(9, 2))
293+
assert "Could not convert inf into a decimal(9, 2), value is not finite" in str(e.value)
294+
295+
280296
def test_decimal_to_decimal_conversion() -> None:
281297
lit = literal(Decimal("34.11").quantize(Decimal(".01")))
282298

0 commit comments

Comments
 (0)