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: Return the truth values of ne_missing and eq_missing operations for struct instead of null #18930

Merged
merged 9 commits into from
Sep 29, 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
9 changes: 8 additions & 1 deletion crates/polars-core/src/chunked_array/comparison/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ fn struct_helper<F, R>(
op: F,
reduce: R,
value: bool,
is_missing: bool,
) -> BooleanChunked
where
F: Fn(&Series, &Series) -> BooleanChunked,
Expand All @@ -778,7 +779,8 @@ where
.map(|(l, r)| op(l, r))
.reduce(reduce)
.unwrap();
if a.null_count() > 0 || b.null_count() > 0 {

if !is_missing & (a.null_count() > 0 || b.null_count() > 0) {
let mut a = a.into_owned();
a.zip_outer_validity(&b);
unsafe {
Expand All @@ -787,6 +789,7 @@ where
}
}
}

out
}
}
Expand All @@ -801,6 +804,7 @@ impl ChunkCompareEq<&StructChunked> for StructChunked {
|l, r| l.equal(r).unwrap(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this is not what this PR is trying to solve, but we made a policy that nested equality is always eq_missing. Maybe, this should changed here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense, thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not all types support eq_missing, so the code reaches panic.
Maybe it is better to get fixed in separate PR?

|a, b| a.bitand(b),
false,
false,
)
}

Expand All @@ -811,6 +815,7 @@ impl ChunkCompareEq<&StructChunked> for StructChunked {
|l, r| l.equal_missing(r).unwrap(),
|a, b| a.bitand(b),
false,
true,
)
}

Expand All @@ -821,6 +826,7 @@ impl ChunkCompareEq<&StructChunked> for StructChunked {
|l, r| l.not_equal(r).unwrap(),
|a, b| a | b,
true,
false,
)
}

Expand All @@ -831,6 +837,7 @@ impl ChunkCompareEq<&StructChunked> for StructChunked {
|l, r| l.not_equal_missing(r).unwrap(),
|a, b| a | b,
true,
true,
)
}
}
Expand Down
24 changes: 24 additions & 0 deletions py-polars/tests/unit/operations/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,30 @@ def test_missing_equality_on_bools() -> None:
]


def test_struct_equality_18870() -> None:
s = pl.Series([{"a": 1}, None])

# eq
result = s.eq(s).to_list()
expected = [True, None]
assert result == expected

# ne
result = s.ne(s).to_list()
expected = [False, None]
assert result == expected

# eq_missing
result = s.eq_missing(s).to_list()
expected = [True, True]
assert result == expected

# ne_missing
result = s.ne_missing(s).to_list()
expected = [False, False]
assert result == expected


def isnan(x: Any) -> bool:
return isinstance(x, float) and math.isnan(x)

Expand Down
Loading