Skip to content

Commit 51dfef2

Browse files
authored
fix: Correctly handle null values when comparing structs (#23560)
1 parent 77df94d commit 51dfef2

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

crates/polars-core/src/chunked_array/comparison/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,9 +843,12 @@ where
843843
let mut a = a.into_owned();
844844
a.zip_outer_validity(&b);
845845
unsafe {
846+
let mut new_null_count = 0;
846847
for (arr, a) in out.downcast_iter_mut().zip(a.downcast_iter()) {
847-
arr.set_validity(a.validity().cloned())
848+
arr.set_validity(a.validity().cloned());
849+
new_null_count += arr.null_count();
848850
}
851+
out.set_null_count(new_null_count);
849852
}
850853
}
851854

py-polars/tests/unit/datatypes/test_struct.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,3 +1282,13 @@ def test_struct_cast_string_multiple_chunks_21650() -> None:
12821282
result = df.select(pl.col("a").cast(pl.String))
12831283
expected = pl.DataFrame({"a": ["{1,2}", "{1,2}"]})
12841284
assert_frame_equal(result, expected)
1285+
1286+
1287+
def test_struct_nulls_in_equality_23527() -> None:
1288+
df = pl.DataFrame({"a": [False, False, False, None, True, True]})
1289+
df_struct = df.with_columns(pl.struct(["a"]).alias("a"))
1290+
out = df_struct.with_columns(
1291+
(pl.col("a") == pl.col("a").shift(1)).fill_null(False).alias("a")
1292+
)
1293+
expected = pl.DataFrame({"a": [False, True, True, False, False, True]})
1294+
assert_frame_equal(out, expected)

0 commit comments

Comments
 (0)