Skip to content

Commit

Permalink
fix: Fix struct broadcasting comparisons (#19003)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Sep 29, 2024
1 parent 570e3a2 commit 57df7b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions crates/polars-core/src/chunked_array/comparison/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,8 +764,10 @@ where
F: Fn(&Series, &Series) -> BooleanChunked,
R: Fn(BooleanChunked, BooleanChunked) -> BooleanChunked,
{
if a.len() != b.len() || a.struct_fields().len() != b.struct_fields().len() {
// polars_ensure!(a.len() == 1 || b.len() == 1, ShapeMismatch: "length lhs: {}, length rhs: {}", a.len(), b.len());
let len_a = a.len();
let len_b = b.len();
let broadcasts = len_a == 1 || len_b == 1;
if (a.len() != b.len() && !broadcasts) || a.struct_fields().len() != b.struct_fields().len() {
BooleanChunked::full(PlSmallStr::EMPTY, value, a.len())
} else {
let (a, b) = align_chunks_binary(a, b);
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/unit/operations/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,10 @@ def test_nested_binary_literal_super_type_12227() -> None:

result = pl.select((pl.lit(0) + (pl.lit(0) == pl.lit(0)) * pl.lit(0.1)) + pl.lit(0))
assert result.item() == 0.1


def test_struct_broadcasting_comparison() -> None:
df = pl.DataFrame({"foo": [{"a": 1}, {"a": 2}, {"a": 1}]})
assert df.select(eq=pl.col.foo == pl.col.foo.last()).to_dict(as_series=False) == {
"eq": [True, False, True]
}

0 comments on commit 57df7b0

Please sign in to comment.