Skip to content

Commit

Permalink
fix: Empty any_horizontal should be false, not true (#18545)
Browse files Browse the repository at this point in the history
  • Loading branch information
orlp committed Sep 4, 2024
1 parent 5774962 commit b1f15c3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 4 additions & 3 deletions crates/polars-plan/src/plans/conversion/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ pub(super) fn convert_functions(
) -> PolarsResult<Node> {
match function {
// This can be created by col(*).is_null() on empty dataframes.
FunctionExpr::Boolean(BooleanFunction::AllHorizontal | BooleanFunction::AnyHorizontal)
if input.is_empty() =>
{
FunctionExpr::Boolean(BooleanFunction::AllHorizontal) if input.is_empty() => {
return to_aexpr_impl(lit(true), arena, state);
},
FunctionExpr::Boolean(BooleanFunction::AnyHorizontal) if input.is_empty() => {
return to_aexpr_impl(lit(false), arena, state);
},
// Convert to binary expression as the optimizer understands those.
// Don't exceed 128 expressions as we might stackoverflow.
FunctionExpr::Boolean(BooleanFunction::AllHorizontal) => {
Expand Down
15 changes: 15 additions & 0 deletions py-polars/tests/unit/operations/aggregation/test_horizontal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

import polars as pl
import polars.selectors as cs
from polars.exceptions import ComputeError
from polars.testing import assert_frame_equal, assert_series_equal

Expand Down Expand Up @@ -51,6 +52,20 @@ def test_all_any_horizontally() -> None:
assert "horizontal" not in dfltr.explain().lower()


def test_empty_all_any_horizontally() -> None:
# any/all_horizontal don't allow empty input, but we can still trigger this
# by selecting an empty set of columns with pl.selectors.
df = pl.DataFrame({"x": [1, 2, 3]})
assert_frame_equal(
df.select(pl.any_horizontal(cs.string().is_null())),
pl.DataFrame({"literal": False}),
)
assert_frame_equal(
df.select(pl.all_horizontal(cs.string().is_null())),
pl.DataFrame({"literal": True}),
)


def test_all_any_single_input() -> None:
df = pl.DataFrame({"a": [0, 1, None]})
out = df.select(
Expand Down

0 comments on commit b1f15c3

Please sign in to comment.