diff --git a/pyiceberg/expressions/literals.py b/pyiceberg/expressions/literals.py index 9952fd82d4..39ec228246 100644 --- a/pyiceberg/expressions/literals.py +++ b/pyiceberg/expressions/literals.py @@ -416,7 +416,8 @@ def _(self, _: DoubleType) -> Literal[float]: 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)) + # Use the shortest repr of the float, like BigDecimal.valueOf in Java, so 2.675 rounds to 2.68 + return DecimalLiteral(Decimal(str(self.value)).quantize(Decimal((0, (1,), -type_var.scale)), rounding=ROUND_HALF_UP)) class DoubleLiteral(Literal[float]): @@ -443,7 +444,8 @@ def _(self, _: FloatType) -> Literal[float]: 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)) + # Use the shortest repr of the float, like BigDecimal.valueOf in Java, so 2.675 rounds to 2.68 + return DecimalLiteral(Decimal(str(self.value)).quantize(Decimal((0, (1,), -type_var.scale)), rounding=ROUND_HALF_UP)) class DateLiteral(Literal[int]): diff --git a/tests/expressions/test_literals.py b/tests/expressions/test_literals.py index 7e29f8d25d..e3a31cf043 100644 --- a/tests/expressions/test_literals.py +++ b/tests/expressions/test_literals.py @@ -277,6 +277,19 @@ 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 +@pytest.mark.parametrize("literal_class", [FloatLiteral, DoubleLiteral]) +@pytest.mark.parametrize( + "value, decimal_value", + [(2.675, "2.68"), (1.005, "1.01"), (-2.675, "-2.68"), (0.125, "0.13"), (2.674, "2.67")], +) +def test_float_and_double_to_decimal_round_half_up_on_shortest_repr( + literal_class: type[FloatLiteral] | type[DoubleLiteral], value: float, decimal_value: str +) -> None: + lit = literal_class(value) + + assert lit.to(DecimalType(10, 2)).value.as_tuple() == Decimal(decimal_value).as_tuple() + + def test_float_to_decimal_conversion_with_infinity() -> None: lit = FloatLiteral(float("inf"))