This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-python.git
The following commit(s) were added to refs/heads/main by this push:
new 6874e73e8 Convert float literals to wide decimal columns without
InvalidOperation (#4372)
6874e73e8 is described below
commit 6874e73e86a59572e755f92b2f7415f5f7819f6d
Author: Rahul Shivu Mahadev <[email protected]>
AuthorDate: Wed Nov 25 22:26:03 2026 +0000
Convert float literals to wide decimal columns without InvalidOperation
(#4372)
FloatLiteral.to(DecimalType) and DoubleLiteral.to(DecimalType) quantize the
value under the default 28-digit decimal context, so a value whose quantized
form has more digits raised decimal.InvalidOperation: amt > 1e26 on a
decimal(38, 2) column, or amt < 0.5 on a decimal(38, 30) column. Quantize
under
a context with enough precision for the result.
Co-authored-by: Isaac <[email protected]>
---
pyiceberg/expressions/literals.py | 22 +++++++++++++---------
tests/expressions/test_literals.py | 10 ++++++++++
2 files changed, 23 insertions(+), 9 deletions(-)
diff --git a/pyiceberg/expressions/literals.py
b/pyiceberg/expressions/literals.py
index 39ec22824..371cac8bb 100644
--- a/pyiceberg/expressions/literals.py
+++ b/pyiceberg/expressions/literals.py
@@ -24,7 +24,7 @@ from __future__ import annotations
import struct
from abc import ABC, abstractmethod
from datetime import date, datetime, time
-from decimal import ROUND_HALF_UP, Decimal
+from decimal import ROUND_HALF_UP, Decimal, getcontext, localcontext
from functools import singledispatchmethod
from math import isfinite, isnan
from typing import Any, Generic
@@ -371,6 +371,16 @@ class LongLiteral(Literal[int]):
return DecimalLiteral(Decimal((sign, digits + zeros,
-type_var.scale)))
+def _float_to_decimal(value: float, type_var: DecimalType) -> Decimal:
+ if not isfinite(value):
+ raise ValueError(f"Could not convert {value} into a {type_var}, value
is not finite")
+ # Use the shortest repr of the float, like BigDecimal.valueOf in Java, so
2.675 rounds to 2.68
+ shortest = Decimal(str(value))
+ # Quantize needs a context precision that covers every digit of the
result, which can exceed the default 28 digits
+ with localcontext(prec=max(getcontext().prec, shortest.adjusted() +
type_var.scale + 2)):
+ return shortest.quantize(Decimal((0, (1,), -type_var.scale)),
rounding=ROUND_HALF_UP)
+
+
class FloatLiteral(Literal[float]):
def __init__(self, value: float) -> None:
super().__init__(value, float)
@@ -414,10 +424,7 @@ class FloatLiteral(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")
- # 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))
+ return DecimalLiteral(_float_to_decimal(self.value, type_var))
class DoubleLiteral(Literal[float]):
@@ -442,10 +449,7 @@ class DoubleLiteral(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")
- # 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))
+ return DecimalLiteral(_float_to_decimal(self.value, type_var))
class DateLiteral(Literal[int]):
diff --git a/tests/expressions/test_literals.py
b/tests/expressions/test_literals.py
index e3a31cf04..25ea32546 100644
--- a/tests/expressions/test_literals.py
+++ b/tests/expressions/test_literals.py
@@ -306,6 +306,16 @@ def test_double_to_decimal_conversion_with_infinity() ->
None:
assert "Could not convert inf into a decimal(9, 2), value is not finite"
in str(e.value)
+def test_float_to_decimal_conversion_beyond_default_context_precision() ->
None:
+ # The default decimal context keeps 28 digits, fewer than a decimal(38, s)
value can have
+ for value, decimal_type, expected in [
+ (2.0**87, DecimalType(38, 2), "154742504910672530000000000.00"),
+ (0.5, DecimalType(38, 30), "0.500000000000000000000000000000"),
+ ]:
+ for lit in (FloatLiteral(value), DoubleLiteral(value)):
+ assert lit.to(decimal_type).value.as_tuple() ==
Decimal(expected).as_tuple(), (lit, decimal_type)
+
+
def test_decimal_to_decimal_conversion() -> None:
lit = literal(Decimal("34.11").quantize(Decimal(".01")))