From f81b82789fd97ee63457bcabf94fcee1bee664dc Mon Sep 17 00:00:00 2001 From: rahulsmahadev Date: Fri, 13 Nov 2026 17:20:49 +0000 Subject: [PATCH 1/3] Parse scientific notation and mixed int/decimal IN lists in filters The row filter parser only accepted decimals with a fractional part and literal sets of a single numeric kind, so `x > 1e-3` and `x in (1, 2.5)` raised a ParseException. Co-authored-by: Isaac --- pyiceberg/expressions/parser.py | 8 ++++---- tests/expressions/test_parser.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/pyiceberg/expressions/parser.py b/pyiceberg/expressions/parser.py index d813bcaa07..ac327abe87 100644 --- a/pyiceberg/expressions/parser.py +++ b/pyiceberg/expressions/parser.py @@ -105,12 +105,12 @@ def _(result: ParseResults) -> Reference: boolean = one_of(["true", "false"], caseless=True) string = sgl_quoted_string.set_results_name("raw_quoted_string") -decimal = common.real().set_results_name("decimal") +decimal = common.sci_real().set_results_name("decimal") integer = common.signed_integer().set_results_name("integer") literal = Group(string | decimal | integer | boolean).set_results_name("literal") -literal_set = Group( - DelimitedList(string) | DelimitedList(decimal) | DelimitedList(integer) | DelimitedList(boolean) -).set_results_name("literal_set") +literal_set = Group(DelimitedList(string) | DelimitedList(decimal | integer) | DelimitedList(boolean)).set_results_name( + "literal_set" +) @boolean.set_parse_action diff --git a/tests/expressions/test_parser.py b/tests/expressions/test_parser.py index 581ee90b72..f44248873d 100644 --- a/tests/expressions/test_parser.py +++ b/tests/expressions/test_parser.py @@ -146,6 +146,26 @@ def test_not_in_different_types() -> None: parser.parse("foo not in (5, 'a')") +def test_in_mixed_numbers() -> None: + for values in ("5, 6.5", "6.5, 5"): + result = parser.parse(f"foo in ({values})") + assert result == In("foo", {LongLiteral(5), DecimalLiteral(Decimal("6.5"))}), values + assert isinstance(result, In) + assert {type(value) for value in result.literals} == {LongLiteral, DecimalLiteral}, values + assert NotIn("foo", {LongLiteral(5), DecimalLiteral(Decimal("1e3"))}) == parser.parse("foo not in (5, 1e3)") + + +def test_scientific_notation() -> None: + for expression, expected in [ + ("foo > 1e-3", GreaterThan("foo", Decimal("0.001"))), + ("1e-3 < foo", GreaterThan("foo", Decimal("0.001"))), + ("foo < -1.5E3", LessThan("foo", Decimal("-1500"))), + ("foo = 2e2", EqualTo("foo", Decimal("200"))), + ("foo between 1e1 and 2.5e1", And(GreaterThanOrEqual("foo", Decimal("10")), LessThanOrEqual("foo", Decimal("25")))), + ]: + assert parser.parse(expression) == expected, expression + + def test_simple_and() -> None: assert And(GreaterThanOrEqual("foo", 5), LessThan("foo", 10)) == parser.parse("5 <= foo and foo < 10") From bccbed0242a26ae3209a49f12e73e6efda426da9 Mon Sep 17 00:00:00 2001 From: rahulsmahadev Date: Mon, 16 Nov 2026 13:21:59 +0000 Subject: [PATCH 2/3] Parse exponent literals as doubles and convert whole doubles to integer types A number with an exponent is an approximate numeric literal in ANSI SQL, and Spark, Trino and DuckDB parse 1e-3 as a double. As a DecimalLiteral, 1e3 has scale -3 and could not bind to decimal columns. Parse the exponent forms as DoubleLiteral instead, and convert a whole DoubleLiteral to int and long columns (a fractional value raises, as for decimals) so that long_col = 1e3 binds. Co-authored-by: Isaac --- pyiceberg/expressions/literals.py | 10 ++++++ pyiceberg/expressions/parser.py | 15 ++++++-- tests/expressions/test_literals.py | 17 +++++++-- tests/expressions/test_parser.py | 55 +++++++++++++++++++++++++----- 4 files changed, 84 insertions(+), 13 deletions(-) diff --git a/pyiceberg/expressions/literals.py b/pyiceberg/expressions/literals.py index b502b43539..96bb63775b 100644 --- a/pyiceberg/expressions/literals.py +++ b/pyiceberg/expressions/literals.py @@ -445,6 +445,16 @@ def _(self, type_var: DecimalType) -> Literal[Decimal]: raise ValueError(f"Could not convert {self.value} into a {type_var}, value is not finite") return DecimalLiteral(Decimal(self.value).quantize(Decimal((0, (1,), -type_var.scale)), rounding=ROUND_HALF_UP)) + @to.register(IntegerType) + @to.register(LongType) + def _(self, type_var: IntegerType | LongType) -> Literal[int]: + if not isfinite(self.value): + raise ValueError(f"Could not convert {self.value} into a {type_var}, value is not finite") + if not self.value.is_integer(): + raise ValueError(f"Could not convert {self.value} into a {type_var}, value has a fractional part") + # The decimal conversion checks the range of the column type + return DecimalLiteral(Decimal(self.value)).to(type_var) + class DateLiteral(Literal[int]): def __init__(self, value: int) -> None: diff --git a/pyiceberg/expressions/parser.py b/pyiceberg/expressions/parser.py index ac327abe87..c5c0d02d3b 100644 --- a/pyiceberg/expressions/parser.py +++ b/pyiceberg/expressions/parser.py @@ -26,6 +26,7 @@ ParserElement, ParseResults, QuotedString, + Regex, Suppress, Word, alphanums, @@ -62,6 +63,7 @@ from pyiceberg.expressions.literals import ( BooleanLiteral, DecimalLiteral, + DoubleLiteral, Literal, LongLiteral, StringLiteral, @@ -105,10 +107,12 @@ def _(result: ParseResults) -> Reference: boolean = one_of(["true", "false"], caseless=True) string = sgl_quoted_string.set_results_name("raw_quoted_string") -decimal = common.sci_real().set_results_name("decimal") +# A number with an exponent is an approximate numeric literal (a double), as in ANSI SQL +double = Regex(r"[+-]?(?:\d+\.?\d*|\.\d+)[eE][+-]?\d+").set_results_name("double") +decimal = common.real().set_results_name("decimal") integer = common.signed_integer().set_results_name("integer") -literal = Group(string | decimal | integer | boolean).set_results_name("literal") -literal_set = Group(DelimitedList(string) | DelimitedList(decimal | integer) | DelimitedList(boolean)).set_results_name( +literal = Group(string | double | decimal | integer | boolean).set_results_name("literal") +literal_set = Group(DelimitedList(string) | DelimitedList(double | decimal | integer) | DelimitedList(boolean)).set_results_name( "literal_set" ) @@ -126,6 +130,11 @@ def _(result: ParseResults) -> Literal[str]: return StringLiteral(result.raw_quoted_string[1:-1].replace("''", "'")) +@double.set_parse_action +def _(result: ParseResults) -> Literal[float]: + return DoubleLiteral(float(result.double)) + + @decimal.set_parse_action def _(result: ParseResults) -> Literal[Decimal]: return DecimalLiteral(Decimal(result.decimal)) diff --git a/tests/expressions/test_literals.py b/tests/expressions/test_literals.py index 9c4252e57d..551ae46e0d 100644 --- a/tests/expressions/test_literals.py +++ b/tests/expressions/test_literals.py @@ -268,6 +268,21 @@ def test_double_to_float_outside_bound() -> None: assert below_min_lit == FloatBelowMin() +def test_double_to_integer_and_long_conversion() -> None: + for type_var in (IntegerType(), LongType()): + assert DoubleLiteral(1e3).to(type_var) == LongLiteral(1000) + assert DoubleLiteral(-2.0).to(type_var) == LongLiteral(-2) + with pytest.raises(ValueError, match="value has a fractional part"): + DoubleLiteral(1.5).to(type_var) + with pytest.raises(ValueError, match="value is not finite"): + DoubleLiteral(float("inf")).to(type_var) + # A float literal still does not convert to an integer type + with pytest.raises(TypeError): + FloatLiteral(1e3).to(type_var) + assert DoubleLiteral(1e10).to(IntegerType()) == IntAboveMax() + assert DoubleLiteral(-1e19).to(LongType()) == LongBelowMin() + + @pytest.mark.parametrize( "decimal_type, decimal_value", [(DecimalType(9, 1), "34.6"), (DecimalType(9, 2), "34.56"), (DecimalType(9, 4), "34.5600")] ) @@ -703,8 +718,6 @@ def test_invalid_long_conversions() -> None: "test_type", [ BooleanType(), - IntegerType(), - LongType(), DateType(), TimeType(), TimestampType(), diff --git a/tests/expressions/test_parser.py b/tests/expressions/test_parser.py index f44248873d..9b574b097a 100644 --- a/tests/expressions/test_parser.py +++ b/tests/expressions/test_parser.py @@ -43,7 +43,10 @@ Reference, StartsWith, ) -from pyiceberg.expressions.literals import DecimalLiteral, LongLiteral, literal +from pyiceberg.expressions.literals import DecimalLiteral, DoubleLiteral, LongLiteral, literal +from pyiceberg.expressions.visitors import bind +from pyiceberg.schema import Schema +from pyiceberg.types import DecimalType, DoubleType, LongType, NestedField def test_always_true() -> None: @@ -152,18 +155,54 @@ def test_in_mixed_numbers() -> None: assert result == In("foo", {LongLiteral(5), DecimalLiteral(Decimal("6.5"))}), values assert isinstance(result, In) assert {type(value) for value in result.literals} == {LongLiteral, DecimalLiteral}, values - assert NotIn("foo", {LongLiteral(5), DecimalLiteral(Decimal("1e3"))}) == parser.parse("foo not in (5, 1e3)") + + result = parser.parse("foo not in (5, 6.5, 1e3)") + assert result == NotIn("foo", {LongLiteral(5), DecimalLiteral(Decimal("6.5")), DoubleLiteral(1000.0)}) + assert isinstance(result, NotIn) + assert {type(value) for value in result.literals} == {LongLiteral, DecimalLiteral, DoubleLiteral} def test_scientific_notation() -> None: + # A number with an exponent is a double, as in ANSI SQL + for expression, expected in [ + ("foo > 1e-3", GreaterThan("foo", 0.001)), + ("1e-3 < foo", GreaterThan("foo", 0.001)), + ("foo < -1.5E3", LessThan("foo", -1500.0)), + ("foo = 2e2", EqualTo("foo", 200.0)), + ("foo = .5e1", EqualTo("foo", 5.0)), + ]: + result = parser.parse(expression) + assert result == expected, expression + assert isinstance(result.literal, DoubleLiteral), expression + + assert And(GreaterThanOrEqual("foo", 10.0), LessThanOrEqual("foo", 25.0)) == parser.parse("foo between 1e1 and 2.5e1") + + +def test_bind_numeric_literals() -> None: + schema = Schema( + NestedField(1, "dec", DecimalType(9, 2), required=False), + NestedField(2, "lng", LongType(), required=False), + NestedField(3, "dbl", DoubleType(), required=False), + ) for expression, expected in [ - ("foo > 1e-3", GreaterThan("foo", Decimal("0.001"))), - ("1e-3 < foo", GreaterThan("foo", Decimal("0.001"))), - ("foo < -1.5E3", LessThan("foo", Decimal("-1500"))), - ("foo = 2e2", EqualTo("foo", Decimal("200"))), - ("foo between 1e1 and 2.5e1", And(GreaterThanOrEqual("foo", Decimal("10")), LessThanOrEqual("foo", Decimal("25")))), + ("dec = 1e3", DecimalLiteral(Decimal("1000.00"))), + ("dec > 2.5e-1", DecimalLiteral(Decimal("0.25"))), + ("dec = 12.34", DecimalLiteral(Decimal("12.34"))), + ("lng = 1e3", LongLiteral(1000)), + ("lng = 7", LongLiteral(7)), + ("dbl > 1e-3", DoubleLiteral(0.001)), + ("dbl > 2.5", DoubleLiteral(2.5)), ]: - assert parser.parse(expression) == expected, expression + bound = bind(schema, parser.parse(expression), case_sensitive=True) + assert bound.literal == expected, expression + assert type(bound.literal) is type(expected), expression + if isinstance(expected, DecimalLiteral): + assert bound.literal.value.as_tuple() == expected.value.as_tuple(), expression + + bound = bind(schema, parser.parse("lng in (1, 2e1, 3)"), case_sensitive=True) + assert bound.literals == {LongLiteral(1), LongLiteral(20), LongLiteral(3)} + with pytest.raises(ValueError, match="value has a fractional part"): + bind(schema, parser.parse("lng = 1.5e0"), case_sensitive=True) def test_simple_and() -> None: From fd3da9c9a322be9678b0de4a472f18fc94834f0d Mon Sep 17 00:00:00 2001 From: rahulsmahadev Date: Mon, 30 Nov 2026 00:15:29 +0000 Subject: [PATCH 3/3] Bind out-of-range float literals on decimal columns to the sentinel DoubleLiteral and FloatLiteral quantized the value before checking the column range, so 1e30 raised InvalidOperation and 1e10 bound to 10000000000.00 on decimal(9, 2). The conversion now returns DecimalAboveMax or DecimalBelowMin, as the int and long conversions do, and checks the range again after rounding, since 9999999.996 carries up to 10000000.00. Set predicates leave out literals that bind to a sentinel, because they match no row. Without this, IN with an out-of-range value bound the sentinel value itself, so lng in (1e19, 7) matched the maximum long. Co-authored-by: Isaac --- pyiceberg/expressions/__init__.py | 8 +++-- pyiceberg/expressions/literals.py | 59 +++++++++++++++++++++++++++---- tests/expressions/test_parser.py | 26 ++++++++++++++ 3 files changed, 84 insertions(+), 9 deletions(-) diff --git a/pyiceberg/expressions/__init__.py b/pyiceberg/expressions/__init__.py index ad916171c5..acdd7c3e42 100644 --- a/pyiceberg/expressions/__init__.py +++ b/pyiceberg/expressions/__init__.py @@ -710,8 +710,12 @@ def __init__( def bind(self, schema: Schema, case_sensitive: bool = True) -> BoundSetPredicate: bound_term = self.term.bind(schema, case_sensitive) - literal_set = self.literals - return self.as_bound(bound_term, {lit.to(bound_term.ref().field.field_type) for lit in literal_set}) # type: ignore + bound_literals = {lit.to(bound_term.ref().field.field_type) for lit in self.literals} + # A literal outside the column type matches no row, so it is left out of the set + in_range = {lit for lit in bound_literals if not isinstance(lit, (AboveMax, BelowMin))} + if self.literals and not in_range: + return AlwaysFalse() if isinstance(self, In) else AlwaysTrue() + return self.as_bound(bound_term, in_range) # type: ignore def __str__(self) -> str: """Return the string representation of the SetPredicate class.""" diff --git a/pyiceberg/expressions/literals.py b/pyiceberg/expressions/literals.py index 96bb63775b..29bacbec6e 100644 --- a/pyiceberg/expressions/literals.py +++ b/pyiceberg/expressions/literals.py @@ -24,7 +24,7 @@ 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, localcontext from functools import singledispatchmethod from math import isfinite, isnan from typing import Any, Generic @@ -371,6 +371,55 @@ def _(self, type_var: DecimalType) -> Literal[Decimal]: return DecimalLiteral(Decimal((sign, digits + zeros, -type_var.scale))) +class DecimalAboveMax(AboveMax[Decimal], Singleton): + def __init__(self, max_value: Decimal) -> None: + super().__init__(max_value, Decimal) + + @singledispatchmethod + def to(self, type_var: IcebergType) -> Literal: # type: ignore + raise TypeError("Cannot change the type of DecimalAboveMax") + + @to.register(DecimalType) + def _(self, _: DecimalType) -> Literal[Decimal]: + return self + + +class DecimalBelowMin(BelowMin[Decimal], Singleton): + def __init__(self, min_value: Decimal) -> None: + super().__init__(min_value, Decimal) + + @singledispatchmethod + def to(self, type_var: IcebergType) -> Literal: # type: ignore + raise TypeError("Cannot change the type of DecimalBelowMin") + + @to.register(DecimalType) + def _(self, _: DecimalType) -> Literal[Decimal]: + return self + + +def _float_to_decimal(value: float, type_var: DecimalType) -> Literal[Decimal]: + """Convert a float to a decimal column, or return the sentinel when the value is outside the column range.""" + if not isfinite(value): + raise ValueError(f"Could not convert {value} into a {type_var}, value is not finite") + # A context wide enough for the column precision, so a long value neither raises nor rounds in the range checks + with localcontext() as ctx: + ctx.prec = type_var.precision + 2 + step = Decimal((0, (1,), -type_var.scale)) + limit = Decimal(10) ** (type_var.precision - type_var.scale) + exact = Decimal(value) + if exact >= limit: + return DecimalAboveMax(limit - step) + if exact <= -limit: + return DecimalBelowMin(step - limit) + rounded = exact.quantize(step, rounding=ROUND_HALF_UP) + # Rounding can carry a value just below the limit up to it, as 9999999.996 to 10000000.00 + if rounded >= limit: + return DecimalAboveMax(limit - step) + if rounded <= -limit: + return DecimalBelowMin(step - limit) + return DecimalLiteral(rounded) + + class FloatLiteral(Literal[float]): def __init__(self, value: float) -> None: super().__init__(value, float) @@ -414,9 +463,7 @@ def _(self, _: DoubleType) -> 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") - return DecimalLiteral(Decimal(self.value).quantize(Decimal((0, (1,), -type_var.scale)), rounding=ROUND_HALF_UP)) + return _float_to_decimal(self.value, type_var) class DoubleLiteral(Literal[float]): @@ -441,9 +488,7 @@ def _(self, _: FloatType) -> 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") - return DecimalLiteral(Decimal(self.value).quantize(Decimal((0, (1,), -type_var.scale)), rounding=ROUND_HALF_UP)) + return _float_to_decimal(self.value, type_var) @to.register(IntegerType) @to.register(LongType) diff --git a/tests/expressions/test_parser.py b/tests/expressions/test_parser.py index 9b574b097a..7bab08eca0 100644 --- a/tests/expressions/test_parser.py +++ b/tests/expressions/test_parser.py @@ -205,6 +205,32 @@ def test_bind_numeric_literals() -> None: bind(schema, parser.parse("lng = 1.5e0"), case_sensitive=True) +def test_bind_out_of_range_numeric_literals() -> None: + schema = Schema( + NestedField(1, "dec", DecimalType(9, 2), required=False), + NestedField(2, "lng", LongType(), required=False), + ) + for expression, expected in [ + ("dec = 1e10", AlwaysFalse()), + ("dec = -1e30", AlwaysFalse()), + ("dec < 1e30", AlwaysTrue()), + ("dec > -1e30", AlwaysTrue()), + # 9999999.996 rounds up to 10000000.00, which is above the column maximum 9999999.99 + ("dec <= 9.999999996e6", AlwaysTrue()), + ("dec > 9.999999996e6", AlwaysFalse()), + ("dec in (1e30)", AlwaysFalse()), + ("dec not in (1e30)", AlwaysTrue()), + ("lng = 1e19", AlwaysFalse()), + ("lng < 1e19", AlwaysTrue()), + ]: + assert bind(schema, parser.parse(expression), case_sensitive=True) == expected, expression + + # A value outside the column range is left out of an IN list, so the other values still match + assert bind(schema, parser.parse("lng in (1e19, 7)"), case_sensitive=True) == bind( + schema, parser.parse("lng = 7"), case_sensitive=True + ) + + def test_simple_and() -> None: assert And(GreaterThanOrEqual("foo", 5), LessThan("foo", 10)) == parser.parse("5 <= foo and foo < 10")