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-4383-08ec3e6272ba3204809a68b59d3d5b789f6d9d2a in repository https://gitbox.apache.org/repos/asf/iceberg-python.git
commit d064b320954b31163eef3ff3a34a6e8f20ab75ae Author: Shahzeb Ali <[email protected]> AuthorDate: Wed Nov 18 09:26:13 2026 +0000 fix(expressions): reject fractional strings converting to int/long (#4383) * fix(expressions): reject fractional strings converting to int/long Signed-off-by: Shahzeb Ali <[email protected]> * test(expressions): expect ValueError for fractional strings to int/long Signed-off-by: Shahzeb Ali <[email protected]> * style: remove stray blank line in test_string_literal Signed-off-by: Shahzeb Ali <[email protected]> * fix: restore closing parentheses on pi_double_lit Signed-off-by: Shahzeb Ali <[email protected]> * fix: correct indentation of sqrt2 assert in test_string_literal Signed-off-by: Shahzeb Ali <[email protected]> * fix: dedent test_integral_decimal_to_integral_type to top level Signed-off-by: Shahzeb Ali <[email protected]> * fix(expressions): check fractional part before range check in StringLiteral.to Signed-off-by: Shahzeb Ali <[email protected]> * test: cover integral string to int/long happy flow in test_string_literal Signed-off-by: Shahzeb Ali <[email protected]> --------- Signed-off-by: Shahzeb Ali <[email protected]> --- pyiceberg/expressions/literals.py | 17 +++++++++++------ tests/expressions/test_literals.py | 25 ++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/pyiceberg/expressions/literals.py b/pyiceberg/expressions/literals.py index b502b4353..bf74761d3 100644 --- a/pyiceberg/expressions/literals.py +++ b/pyiceberg/expressions/literals.py @@ -590,12 +590,16 @@ class StringLiteral(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 @@ -603,12 +607,13 @@ class StringLiteral(Literal[str]): 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 diff --git a/tests/expressions/test_literals.py b/tests/expressions/test_literals.py index 9c4252e57..09471d170 100644 --- a/tests/expressions/test_literals.py +++ b/tests/expressions/test_literals.py @@ -325,9 +325,8 @@ 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 literal("3").to(IntegerType()) == literal(3) + assert literal("3").to(LongType()) == literal(3) assert sqrt2 != pi assert pi != pi_double_lit assert pi == pi_string_lit @@ -946,6 +945,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()]:
