Skip to content

Commit 4000b95

Browse files
committed
Support None comparisons for null expressions
1 parent 3585c11 commit 4000b95

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

python/datafusion/expr.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ def __eq__(self, rhs: object) -> Expr:
483483
484484
Accepts either an expression or any valid PyArrow scalar literal value.
485485
"""
486+
if rhs is None:
487+
return self.is_null()
486488
if not isinstance(rhs, Expr):
487489
rhs = Expr.literal(rhs)
488490
return Expr(self.expr.__eq__(rhs.expr))
@@ -492,6 +494,8 @@ def __ne__(self, rhs: object) -> Expr:
492494
493495
Accepts either an expression or any valid PyArrow scalar literal value.
494496
"""
497+
if rhs is None:
498+
return self.is_not_null()
495499
if not isinstance(rhs, Expr):
496500
rhs = Expr.literal(rhs)
497501
return Expr(self.expr.__ne__(rhs.expr))

python/tests/test_expr.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,24 @@ def test_relational_expr(test_ctx):
173173
assert df.filter(col("a") == "beta").count() == 0
174174

175175

176+
def test_relational_expr_none_uses_null_predicates():
177+
ctx = SessionContext()
178+
179+
batch = pa.RecordBatch.from_arrays(
180+
[
181+
pa.array([1, 2, None]),
182+
pa.array(["alpha", None, "gamma"], type=pa.string_view()),
183+
],
184+
names=["a", "b"],
185+
)
186+
df = ctx.create_dataframe([[batch]], name="batch_with_nulls")
187+
188+
assert df.filter(col("a") == None).count() == 1 # noqa: E711
189+
assert df.filter(col("a") != None).count() == 2 # noqa: E711
190+
assert df.filter(col("b") == None).count() == 1 # noqa: E711
191+
assert df.filter(col("b") != None).count() == 2 # noqa: E711
192+
193+
176194
def test_expr_to_variant():
177195
# Taken from https://github.com/apache/datafusion-python/issues/781
178196
from datafusion import SessionContext

0 commit comments

Comments
 (0)