Skip to content

Commit fda710e

Browse files
Round float and double literals to decimal on the shortest repr (#4442)
1 parent 07216ed commit fda710e

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎pyiceberg/expressions/literals.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ def _(self, _: DoubleType) -> Literal[float]:
416416
def _(self, type_var: DecimalType) -> Literal[Decimal]:
417417
if not isfinite(self.value):
418418
raise ValueError(f"Could not convert {self.value} into a {type_var}, value is not finite")
419-
return DecimalLiteral(Decimal(self.value).quantize(Decimal((0, (1,), -type_var.scale)), rounding=ROUND_HALF_UP))
419+
# Use the shortest repr of the float, like BigDecimal.valueOf in Java, so 2.675 rounds to 2.68
420+
return DecimalLiteral(Decimal(str(self.value)).quantize(Decimal((0, (1,), -type_var.scale)), rounding=ROUND_HALF_UP))
420421

421422

422423
class DoubleLiteral(Literal[float]):
@@ -443,7 +444,8 @@ def _(self, _: FloatType) -> Literal[float]:
443444
def _(self, type_var: DecimalType) -> Literal[Decimal]:
444445
if not isfinite(self.value):
445446
raise ValueError(f"Could not convert {self.value} into a {type_var}, value is not finite")
446-
return DecimalLiteral(Decimal(self.value).quantize(Decimal((0, (1,), -type_var.scale)), rounding=ROUND_HALF_UP))
447+
# Use the shortest repr of the float, like BigDecimal.valueOf in Java, so 2.675 rounds to 2.68
448+
return DecimalLiteral(Decimal(str(self.value)).quantize(Decimal((0, (1,), -type_var.scale)), rounding=ROUND_HALF_UP))
447449

448450

449451
class DateLiteral(Literal[int]):

‎tests/expressions/test_literals.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,19 @@ 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+
@pytest.mark.parametrize("literal_class", [FloatLiteral, DoubleLiteral])
281+
@pytest.mark.parametrize(
282+
"value, decimal_value",
283+
[(2.675, "2.68"), (1.005, "1.01"), (-2.675, "-2.68"), (0.125, "0.13"), (2.674, "2.67")],
284+
)
285+
def test_float_and_double_to_decimal_round_half_up_on_shortest_repr(
286+
literal_class: type[FloatLiteral] | type[DoubleLiteral], value: float, decimal_value: str
287+
) -> None:
288+
lit = literal_class(value)
289+
290+
assert lit.to(DecimalType(10, 2)).value.as_tuple() == Decimal(decimal_value).as_tuple()
291+
292+
280293
def test_float_to_decimal_conversion_with_infinity() -> None:
281294
lit = FloatLiteral(float("inf"))
282295

0 commit comments

Comments
 (0)