Skip to content

Commit 13c5ab9

Browse files
kevinjqliuCopilot
andcommitted
Check for a fractional part before the integer range check
The range check rounded the decimal first, so a fractional value near the int/long bounds became an above/below-max sentinel instead of raising, depending on half-even rounding. Reject any fractional value up front. Also replace parametrize with loops in the new literal tests. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent c4489d7 commit 13c5ab9

2 files changed

Lines changed: 25 additions & 13 deletions

File tree

‎pyiceberg/expressions/literals.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,23 +535,23 @@ def _(self, type_var: DecimalType) -> Literal[Decimal]:
535535

536536
@to.register(IntegerType)
537537
def _(self, type_var: IntegerType) -> Literal[int]:
538-
value_int = int(self.value.to_integral_value())
538+
value_int = _to_integral(self.value, type_var)
539539
if value_int > IntegerType.max:
540540
return IntAboveMax()
541541
elif value_int < IntegerType.min:
542542
return IntBelowMin()
543543
else:
544-
return LongLiteral(_to_integral(self.value, type_var))
544+
return LongLiteral(value_int)
545545

546546
@to.register(LongType)
547547
def _(self, type_var: LongType) -> Literal[int]:
548-
value_int = int(self.value.to_integral_value())
548+
value_int = _to_integral(self.value, type_var)
549549
if value_int > LongType.max:
550550
return LongAboveMax()
551551
elif value_int < LongType.min:
552552
return LongBelowMin()
553553
else:
554-
return LongLiteral(_to_integral(self.value, type_var))
554+
return LongLiteral(value_int)
555555

556556
@to.register(FloatType)
557557
def _(self, _: FloatType) -> Literal[float]:

‎tests/expressions/test_literals.py‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -919,19 +919,31 @@ def test_decimal_to_long_below_min() -> None:
919919
assert isinstance(DecimalLiteral(Decimal(LongType.min - 1)).to(LongType()), LongBelowMin)
920920

921921

922-
@pytest.mark.parametrize("value", ["2.5", "2.6", "-2.5", "0.1"])
923-
@pytest.mark.parametrize("target_type", [IntegerType(), LongType()])
924-
def test_fractional_decimal_to_integral_type_raises(value: str, target_type: PrimitiveType) -> None:
922+
def test_fractional_decimal_to_integral_type_raises() -> None:
925923
# Rounding would change the predicate, e.g. x > 2.6 would become x > 3 and drop x = 3
926-
with pytest.raises(ValueError, match=f"Could not convert {value} into a {target_type}"):
927-
_ = DecimalLiteral(Decimal(value)).to(target_type)
924+
for target_type in [IntegerType(), LongType()]:
925+
for value in ["2.5", "2.6", "-2.5", "0.1"]:
926+
with pytest.raises(ValueError, match=f"Could not convert {value} into a {target_type}"):
927+
_ = DecimalLiteral(Decimal(value)).to(target_type)
928928

929929

930-
@pytest.mark.parametrize("value", ["2", "2.0", "2.00", "-2.0"])
931-
@pytest.mark.parametrize("target_type", [IntegerType(), LongType()])
932-
def test_integral_decimal_to_integral_type(value: str, target_type: PrimitiveType) -> None:
930+
def test_integral_decimal_to_integral_type() -> None:
933931
# A decimal literal without a fractional part, such as 2.0, still converts exactly
934-
assert DecimalLiteral(Decimal(value)).to(target_type) == LongLiteral(int(Decimal(value)))
932+
for target_type in [IntegerType(), LongType()]:
933+
for value in ["2", "2.0", "2.00", "-2.0"]:
934+
assert DecimalLiteral(Decimal(value)).to(target_type) == LongLiteral(int(Decimal(value)))
935+
936+
937+
def test_fractional_decimal_near_integral_type_bounds_raises() -> None:
938+
# Rejected even when rounding would cross the bound
939+
for target_type, bound in [
940+
(IntegerType(), IntegerType.max),
941+
(IntegerType(), IntegerType.min),
942+
(LongType(), LongType.max),
943+
(LongType(), LongType.min),
944+
]:
945+
with pytest.raises(ValueError, match="value has a fractional part"):
946+
_ = DecimalLiteral(Decimal(f"{bound}.9")).to(target_type)
935947

936948

937949
def test_string_to_integer_type_invalid_value() -> None:

0 commit comments

Comments
 (0)