Skip to content

Commit f778f03

Browse files
authored
fix(polars): make sure sum is NULL to match other backends (#11321)
Closes #11318.
1 parent 7671bda commit f778f03

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

ibis/backends/polars/compiler.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,14 @@ def execute_reduction(op, **kw):
750750
translate.register(cls, execute_reduction)
751751

752752

753+
@translate.register(ops.Sum)
754+
def execute_sum(op, **kw):
755+
arg = translate(op.arg, **kw)
756+
if (where := op.where) is not None:
757+
arg = arg.filter(translate(where, **kw))
758+
return pl.when(arg.count() > 0).then(arg.sum()).otherwise(None)
759+
760+
753761
@translate.register(ops.First)
754762
@translate.register(ops.Last)
755763
@translate.register(ops.Arbitrary)

ibis/backends/tests/test_aggregation.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,3 +1731,12 @@ def test_group_by_scalar(alltypes, df, value):
17311731
result = expr.execute()
17321732
n = result["n"].values[0].item()
17331733
assert n == len(df)
1734+
1735+
1736+
def test_empty_sum(con):
1737+
t = ibis.memtable({"x": [1]}, schema={"x": "int"})
1738+
result = con.execute(t.count())
1739+
assert result == 1
1740+
1741+
result = con.to_pyarrow(t.x.sum(where=t.x > 1)).as_py()
1742+
assert result is None

0 commit comments

Comments
 (0)