Skip to content

Commit ca9d9d3

Browse files
committed
fix: handle negative-scale Decimals in increment, decrement and truncate_decimal
Decimal.as_tuple().exponent carries the negated scale, so a Decimal with a non-negative exponent (1E+2) has a negative scale. abs() turned that exponent into a positive scale, so increment()/decrement() returned values on the wrong order of magnitude (1E+2 -> 0.02 instead of 2E+2) and truncate_decimal() landed on a wrong scale. Same root cause as #3997, remaining call sites. Signed-off-by: Ayush Kumar Saha <225407166+AP24110011695@users.noreply.github.com>
1 parent e3564a5 commit ca9d9d3

4 files changed

Lines changed: 39 additions & 4 deletions

File tree

‎pyiceberg/expressions/literals.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,14 @@ def __init__(self, value: Decimal) -> None:
514514
super().__init__(value, Decimal)
515515

516516
def increment(self) -> Literal[Decimal]:
517-
original_scale = abs(int(self.value.as_tuple().exponent))
517+
# A Decimal carries the negated scale as its exponent, so a value with a
518+
# non-negative exponent (1E+2) has a negative scale and abs() would read it wrong
519+
original_scale = -int(self.value.as_tuple().exponent)
518520
unscaled = decimal_to_unscaled(self.value)
519521
return DecimalLiteral(unscaled_to_decimal(unscaled + 1, original_scale))
520522

521523
def decrement(self) -> Literal[Decimal]:
522-
original_scale = abs(int(self.value.as_tuple().exponent))
524+
original_scale = -int(self.value.as_tuple().exponent)
523525
unscaled = decimal_to_unscaled(self.value)
524526
return DecimalLiteral(unscaled_to_decimal(unscaled - 1, original_scale))
525527

‎pyiceberg/utils/decimal.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ def truncate_decimal(value: Decimal, width: int) -> Decimal:
112112
"""
113113
unscaled_value = decimal_to_unscaled(value)
114114
applied_value = unscaled_value - (((unscaled_value % width) + width) % width)
115-
return unscaled_to_decimal(applied_value, abs(int(value.as_tuple().exponent)))
115+
# A Decimal carries the negated scale as its exponent, so a value with a
116+
# non-negative exponent (1E+2) has a negative scale and abs() would read it wrong
117+
return unscaled_to_decimal(applied_value, -int(value.as_tuple().exponent))
116118

117119

118120
MAX_PRECISION = tuple(math.floor(math.log10(math.fabs(math.pow(2, 8 * pos - 1) - 1))) for pos in range(24))

‎tests/expressions/test_literals.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,24 @@ def test_decimal_literal_decrement() -> None:
10001000
assert dec.decrement().value.as_tuple() == Decimal("10.122").as_tuple()
10011001

10021002

1003+
def test_decimal_literal_increment_negative_scale() -> None:
1004+
dec = DecimalLiteral(Decimal("1E+2"))
1005+
# Twice to check that we don't mutate the value
1006+
assert dec.increment() == DecimalLiteral(Decimal("2E+2"))
1007+
assert dec.increment() == DecimalLiteral(Decimal("2E+2"))
1008+
# To check that the scale is still the same (a non-negative exponent means a negative scale)
1009+
assert dec.increment().value.as_tuple() == Decimal("2E+2").as_tuple()
1010+
1011+
1012+
def test_decimal_literal_decrement_negative_scale() -> None:
1013+
dec = DecimalLiteral(Decimal("1E+2"))
1014+
# Twice to check that we don't mutate the value
1015+
assert dec.decrement() == DecimalLiteral(Decimal("0E+2"))
1016+
assert dec.decrement() == DecimalLiteral(Decimal("0E+2"))
1017+
# To check that the scale is still the same, also when stepping below zero
1018+
assert dec.decrement().decrement() == DecimalLiteral(Decimal("-1E+2"))
1019+
1020+
10031021
def test_uuid_literal_initialization() -> None:
10041022
test_uuid = uuid.UUID("f79c3e09-677c-4bbd-a479-3f349cb785e7")
10051023
uuid_literal = literal(test_uuid)

‎tests/utils/test_decimal.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import pytest
2020

21-
from pyiceberg.utils.decimal import bytes_required, decimal_required_bytes, decimal_to_bytes
21+
from pyiceberg.utils.decimal import bytes_required, decimal_required_bytes, decimal_to_bytes, truncate_decimal
2222

2323

2424
def test_decimal_required_bytes() -> None:
@@ -65,3 +65,16 @@ def test_decimal_to_bytes() -> None:
6565
# Unscaled values -128 and -32768 require no sign padding.
6666
assert decimal_to_bytes(Decimal("-1.28")) == b"\x80"
6767
assert decimal_to_bytes(Decimal("-327.68")) == b"\x80\x00"
68+
69+
70+
def test_truncate_decimal() -> None:
71+
assert truncate_decimal(Decimal("12.34"), 10) == Decimal("12.30")
72+
# Negative values truncate toward negative infinity
73+
assert truncate_decimal(Decimal("-12.34"), 10) == Decimal("-12.40")
74+
75+
76+
def test_truncate_decimal_negative_scale() -> None:
77+
# A Decimal like 1.5E+3 carries a negative scale; reading its exponent as a
78+
# positive scale would return a value on the wrong order of magnitude
79+
assert truncate_decimal(Decimal("1.5E+3"), 4) == Decimal("1.2E+3")
80+
assert truncate_decimal(Decimal("1.5E+3"), 4).as_tuple() == Decimal("1.2E+3").as_tuple()

0 commit comments

Comments
 (0)