rdblue commented on code in PR #6258:
URL: https://github.com/apache/iceberg/pull/6258#discussion_r1032838588


##########
python/pyiceberg/io/pyarrow.py:
##########
@@ -379,3 +400,95 @@ def _(_: StringType) -> pa.DataType:
 def _(_: BinaryType) -> pa.DataType:
     # Variable length by default
     return pa.binary()
+
+
+class _ConvertToArrowExpression(BooleanExpressionVisitor[pc.Expression]):
+    def visit_true(self) -> pc.Expression:
+        return pc.scalar(True)
+
+    def visit_false(self) -> pc.Expression:
+        return pc.scalar(False)
+
+    def visit_not(self, child_result: pc.Expression) -> pc.Expression:
+        return ~child_result
+
+    def visit_and(self, left_result: pc.Expression, right_result: 
pc.Expression) -> pc.Expression:
+        return left_result & right_result
+
+    def visit_or(self, left_result: pc.Expression, right_result: 
pc.Expression) -> pc.Expression:
+        return left_result | right_result
+
+    def visit_unbound_predicate(self, predicate: UnboundPredicate[L]) -> 
pc.Expression:
+        raise ValueError("Please bind the expression first")
+
+    def visit_bound_predicate(self, predicate: BoundPredicate[Any]) -> 
pc.Expression:
+        return _iceberg_to_pyarrow_predicate(predicate)
+
+
+@singledispatch
+def _iceberg_to_pyarrow_predicate(expr: BoundPredicate[str]) -> pc.Expression:
+    raise ValueError(f"Unknown expression: {expr}")
+
+
+@_iceberg_to_pyarrow_predicate.register(BoundIsNull)
+def _(bound: BoundIsNull[str]) -> pc.Expression:
+    return pc.field(bound.term.ref().field.name).is_null(False)
+
+
+@_iceberg_to_pyarrow_predicate.register(BoundNotNull)
+def _(bound: BoundNotNull[str]) -> pc.Expression:
+    return pc.field(bound.term.ref().field.name).is_valid()
+
+
+@_iceberg_to_pyarrow_predicate.register(BoundIsNaN)
+def _(bound: BoundIsNaN[str]) -> pc.Expression:
+    return pc.field(bound.term.ref().field.name).is_null(True)

Review Comment:
   I found [more specific docs for 
is_null](https://arrow.apache.org/docs/python/generated/pyarrow.compute.is_null.html):
   
   > True may also be emitted for NaN values by setting the nan_is_null flag
   
   I think this needs to be more complex to handle NaN only:
   
   ```python
   @_iceberg_to_pyarrow_predicate.register(BoundIsNaN)
   def _(bound: BoundIsNaN[str]) -> pc.Expression:
       ref = pc.field(bound.term.ref().field.name)
       return ref.is_null(nan_is_null=True) & ref.is_valid()
   ```
   
   That way this will match NaN values, but not invalid (null) values.



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