Repository navigation
Parse scientific notation and mixed integer/decimal IN lists in row filters #4371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f81b827
bccbed0
fd3da9c
05fe096
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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: | ||
|
|
@@ -158,6 +161,88 @@ 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 | ||
|
|
||
| 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 [ | ||
| ("dec = 1e3", DecimalLiteral(Decimal("1000.00"))), | ||
| ("dec > 2.5e-1", DecimalLiteral(Decimal("0.25"))), | ||
| ("dec = 12.34", DecimalLiteral(Decimal("12.34"))), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add cases for values outside the column range, such as
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
| ("lng = 1e3", LongLiteral(1000)), | ||
| ("lng = 7", LongLiteral(7)), | ||
| ("dbl > 1e-3", DoubleLiteral(0.001)), | ||
| ("dbl > 2.5", DoubleLiteral(2.5)), | ||
| ]: | ||
| 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_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") | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dec <= 5e-3binds todec <= 0.01(anddec = 1e-3todec = 0.00) becauseDoubleLiteral.to(DecimalType)rounds half-up to the column scale, so a 0.01 row matches a 0.005 bound. Should exponent forms on a decimal column fail on a scale mismatch likeDecimalLiteraldoes, and can this case get a test?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jackylee-ch, good catch.
DoubleLiteral.to(DecimalType)rounds half-up to the column scale, like Java'sDoubleLiteral.to(DECIMAL), so the expression API already bindsLessThanOrEqual("dec", 0.005)asdec <= 0.01; this PR makes that reachable from string filters. I'd suggest raisingCould not convert 0.005 into a decimal(10, 2)when the value does not fit the column scale exactly, sodec <= 5e-3fails likedec <= 0.005whiledec <= 5e-1still binds as0.50. That also changes the expression API for Python floats on decimal columns and differs from Java's rounding, so I'd like to confirm that's acceptable before changing it. I'll add a test either way.