This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch 
gh-readonly-queue/main/pr-4442-07216edd0eca59c1d398e8f77f61ad2d9ae2ef2f
in repository https://gitbox.apache.org/repos/asf/iceberg-python.git

commit fda710e49eb092bf88e4037a0c59cdb10ad278b7
Author: Ankit Raj <[email protected]>
AuthorDate: Sat Nov 21 06:53:51 2026 +0000

    Round float and double literals to decimal on the shortest repr (#4442)
---
 pyiceberg/expressions/literals.py  |  6 ++++--
 tests/expressions/test_literals.py | 13 +++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/pyiceberg/expressions/literals.py 
b/pyiceberg/expressions/literals.py
index 9952fd82d..39ec22824 100644
--- a/pyiceberg/expressions/literals.py
+++ b/pyiceberg/expressions/literals.py
@@ -416,7 +416,8 @@ class FloatLiteral(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 @@ class DoubleLiteral(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 7e29f8d25..e3a31cf04 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
 
 
[email protected]("literal_class", [FloatLiteral, DoubleLiteral])
[email protected](
+    "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"))
 

Reply via email to