rahulsmahadev commented on code in PR #4371:
URL: https://github.com/apache/iceberg-python/pull/4371#discussion_r4733872849
##########
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))
Review Comment:
Done in fd3da9c9. Out-of-range values now return `DecimalAboveMax` or
`DecimalBelowMin`, checked before and after the quantize, so `dec = 1e30` binds
to AlwaysFalse and `dec < 1e30` to AlwaysTrue. The quantize runs in a context
sized for the column precision. Set predicates also leave out sentinels, since
they match no row. Before, `lng in (1e19, 7)` also returned the maximum long.
##########
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"))),
+ ("dec = 12.34", DecimalLiteral(Decimal("12.34"))),
Review Comment:
Added `test_bind_out_of_range_numeric_literals` in
tests/expressions/test_parser.py, with `dec = 1e10`, `dec < 1e30`, `dec >
-1e30`, `lng = 1e19`, the 9999999.996 rounding case and the IN cases. `dec =
1e10` binds to 10000000000.00 on base, so the test fails there.
--
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]