Skip to content
17 changes: 11 additions & 6 deletions pyiceberg/expressions/literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,25 +590,30 @@ def _(self, _: StringType) -> Literal[str]:
def _(self, type_var: IntegerType) -> Literal[int]:
try:
number = _parse_numeric_string(self.value)
# Check the fractional part before the range check, mirroring
# DecimalLiteral: a fractional value is not representable at all,
# so it raises instead of returning AboveMax/BelowMin
value_int = _to_integral(number, type_var)

if IntegerType.max < number:
if IntegerType.max < value_int:
return IntAboveMax()
elif IntegerType.min > number:
elif IntegerType.min > value_int:
return IntBelowMin()
return LongLiteral(int(number))
return LongLiteral(value_int)
except (ArithmeticError, OverflowError, ValueError) as e:
raise ValueError(f"Could not convert {self.value} into a {type_var}") from e

@to.register(LongType)
def _(self, type_var: LongType) -> Literal[int]:
try:
long_value = _parse_numeric_string(self.value)
if LongType.max < long_value:
value_long = _to_integral(long_value, type_var)
if LongType.max < value_long:
return LongAboveMax()
elif LongType.min > long_value:
elif LongType.min > value_long:
return LongBelowMin()
else:
return LongLiteral(int(long_value))
return LongLiteral(value_long)
except (ArithmeticError, OverflowError, TypeError, ValueError) as e:
raise ValueError(f"Could not convert {self.value} into a {type_var}") from e

Expand Down
23 changes: 20 additions & 3 deletions tests/expressions/test_literals.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,6 @@ def test_string_literal() -> None:
pi_string_lit = StringLiteral("3.141")
pi_double_lit = literal(3.141).to(DoubleType())

assert literal("3.141").to(IntegerType()) == literal(3)
assert literal("3.141").to(LongType()) == literal(3)

assert sqrt2 != pi
assert pi != pi_double_lit
assert pi == pi_string_lit
Expand Down Expand Up @@ -946,6 +943,26 @@ def test_fractional_decimal_to_integral_type_raises() -> None:
_ = DecimalLiteral(Decimal(value)).to(target_type)


def test_fractional_string_to_integral_type_raises() -> None:
# Rounding would change the predicate, e.g. x < "2.6" would become x < 2 and drop x = 2
for target_type in [IntegerType(), LongType()]:
for value in ["2.5", "2.6", "-2.5", "0.1"]:
with pytest.raises(ValueError, match=f"Could not convert {value} into a {target_type}"):
_ = literal(value).to(target_type)


def test_fractional_string_near_integral_type_bounds_raises() -> None:
# Rejected even when rounding would cross the bound, mirroring DecimalLiteral
for target_type, bound in [
(IntegerType(), IntegerType.max),
(IntegerType(), IntegerType.min),
(LongType(), LongType.max),
(LongType(), LongType.min),
]:
with pytest.raises(ValueError, match=f"Could not convert {bound}.9 into a {target_type}"):
_ = literal(f"{bound}.9").to(target_type)


def test_integral_decimal_to_integral_type() -> None:
# A decimal literal without a fractional part, such as 2.0, still converts exactly
for target_type in [IntegerType(), LongType()]:
Expand Down
Loading