Fokko commented on code in PR #4371:
URL: https://github.com/apache/iceberg-python/pull/4371#discussion_r4701649798


##########
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:
   Large values are a problem here, and filter strings can now reach them:
   
   - `dec = 1e30` on a `decimal(9, 2)` column raises 
`decimal.InvalidOperation`, because `quantize` fails once the result needs more 
than 28 digits (the default context precision).
   - `dec = 1e10` binds to `10000000000.00`, which does not fit in `decimal(9, 
2)`.
   
   Could this return an above max / below min literal instead, like the int and 
long conversions do? The existing bind logic then turns `dec = 1e30` into 
`AlwaysFalse` and `dec < 1e30` into `AlwaysTrue`. There is no decimal sentinel 
yet, so this needs a `DecimalAboveMax` and `DecimalBelowMin`. Something like 
this before the `quantize`:
   
   ```python
   limit = Decimal(10) ** (type_var.precision - type_var.scale)
   value = Decimal(self.value)
   if value >= limit:
       return DecimalAboveMax()
   elif value <= -limit:
       return DecimalBelowMin()
   ```
   
   And the same check once more after the `quantize`, since rounding can carry 
`9999999.996` to `10000000.00`.



##########
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:
   Could you add cases for values outside the column range, such as `dec = 
1e10`, `dec < 1e30`, `dec > -1e30` and `lng = 1e19`, asserting that they bind 
to `AlwaysFalse()` or `AlwaysTrue()`?



-- 
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]

Reply via email to