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: Don't oob on nulls in list.get #17262

Merged
merged 1 commit into from
Jun 28, 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
20 changes: 17 additions & 3 deletions crates/polars-arrow/src/legacy/kernels/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,23 @@ pub fn sublist_get(arr: &ListArray<i64>, index: i64) -> ArrayRef {

/// Check if an index is out of bounds for at least one sublist.
pub fn index_is_oob(arr: &ListArray<i64>, index: i64) -> bool {
arr.offsets()
.lengths()
.any(|len| index.negative_to_usize(len).is_none())
if arr.null_count() == 0 {
arr.offsets()
.lengths()
.any(|len| index.negative_to_usize(len).is_none())
} else {
arr.offsets()
.lengths()
.zip(arr.validity().unwrap())
.any(|(len, valid)| {
if valid {
index.negative_to_usize(len).is_none()
} else {
// skip nulls
false
}
})
}
}

/// Convert a list `[1, 2, 3]` to a list type of `[[1], [2], [3]]`
Expand Down
15 changes: 15 additions & 0 deletions py-polars/tests/unit/operations/test_gather.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,18 @@ def test_list_get_null_offset_17248() -> None:
assert df.select(
result=pl.when(pl.col.material.list.len() == 1).then("material").list.get(0),
)["result"].to_list() == [None, "CI", "CI"]


def test_list_get_null_oob_17252() -> None:
df = pl.DataFrame(
{
"name": ["BOB-3", "BOB", None],
}
)

split = df.with_columns(pl.col("name").str.split("-"))
assert split.with_columns(pl.col("name").list.get(0))["name"].to_list() == [
"BOB",
"BOB",
None,
]