rahulsmahadev commented on code in PR #4371:
URL: https://github.com/apache/iceberg-python/pull/4371#discussion_r4600857904
##########
tests/expressions/test_parser.py:
##########
@@ -146,6 +149,62 @@ 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"))),
Review Comment:
Thanks @jackylee-ch, good catch. `DoubleLiteral.to(DecimalType)` rounds
half-up to the column scale, like Java's `DoubleLiteral.to(DECIMAL)`, so the
expression API already binds `LessThanOrEqual("dec", 0.005)` as `dec <= 0.01`;
this PR makes that reachable from string filters. I'd suggest raising `Could
not convert 0.005 into a decimal(10, 2)` when the value does not fit the column
scale exactly, so `dec <= 5e-3` fails like `dec <= 0.005` while `dec <= 5e-1`
still binds as `0.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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]