Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Empty any_horizontal should be false, not true #18545

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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