From 5d7de4644f9749b4609117c8e2f0b55df986c6a3 Mon Sep 17 00:00:00 2001 From: Weijie Guo Date: Mon, 15 Apr 2024 17:06:16 +0800 Subject: [PATCH] fix: Correct the unsoundness slice range of `arr.min/max` --- crates/polars-ops/src/chunked_array/array/min_max.rs | 4 +++- py-polars/tests/unit/namespaces/array/test_array.py | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/polars-ops/src/chunked_array/array/min_max.rs b/crates/polars-ops/src/chunked_array/array/min_max.rs index c61d422e4277..bdeb76f250aa 100644 --- a/crates/polars-ops/src/chunked_array/array/min_max.rs +++ b/crates/polars-ops/src/chunked_array/array/min_max.rs @@ -25,7 +25,9 @@ where (0..values.len()) .step_by(width) .map(|start| { - let sliced = unsafe { values.clone().sliced_unchecked(start, start + width) }; + // SAFETY: This value array from a FixedSizeListArray, + // we can ensure that `start + width` will not out out range + let sliced = unsafe { values.clone().sliced_unchecked(start, width) }; arr_agg(&sliced) }) .collect_arr() diff --git a/py-polars/tests/unit/namespaces/array/test_array.py b/py-polars/tests/unit/namespaces/array/test_array.py index f3fdb3c902dd..80405b24ba08 100644 --- a/py-polars/tests/unit/namespaces/array/test_array.py +++ b/py-polars/tests/unit/namespaces/array/test_array.py @@ -15,6 +15,10 @@ def test_arr_min_max() -> None: assert s.arr.max().to_list() == [2, 4] assert s.arr.min().to_list() == [1, 3] + s_with_null = pl.Series("a", [[None, 2], None, [3, 4]], dtype=pl.Array(pl.Int64, 2)) + assert s_with_null.arr.max().to_list() == [2, None, 4] + assert s_with_null.arr.min().to_list() == [2, None, 3] + def test_array_min_max_dtype_12123() -> None: df = pl.LazyFrame(