Skip to content

Commit

Permalink
fix(rust, python): fix anonymous list builder (#6916)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Feb 16, 2023
1 parent a507e1b commit c4267fc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
11 changes: 7 additions & 4 deletions polars/polars-arrow/src/array/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ impl<'a> AnonymousBuilder<'a> {
self.arrays.push(arr.as_ref());
}
self.offsets.push(self.size);
if let Some(validity) = &mut self.validity {
validity.push(true)
}
self.update_validity()
}

pub fn push_null(&mut self) {
Expand All @@ -75,6 +73,7 @@ impl<'a> AnonymousBuilder<'a> {

pub fn push_empty(&mut self) {
self.offsets.push(self.last_offset());
self.update_validity()
}

fn init_validity(&mut self) {
Expand All @@ -85,11 +84,15 @@ impl<'a> AnonymousBuilder<'a> {
validity.set(len - 1, false);
self.validity = Some(validity)
}
fn update_validity(&mut self) {
if let Some(validity) = &mut self.validity {
validity.push(true)
}
}

pub fn finish(self, inner_dtype: Option<&DataType>) -> Result<ListArray<i64>> {
let inner_dtype = inner_dtype.unwrap_or_else(|| self.arrays[0].data_type());
let values = concatenate::concatenate(&self.arrays)?;

let dtype = ListArray::<i64>::default_datatype(inner_dtype.clone());
// Safety:
// offsets are monotonically increasing
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -1452,7 +1452,7 @@ def test_unique_unit_rows() -> None:


def test_panic() -> None:
# may contain some tests that yielded a panic in polars or arrow
# may contain some tests that yielded a panic in polars or pl_arrow
# https://github.com/pola-rs/polars/issues/1110
a = pl.DataFrame(
{
Expand Down
19 changes: 19 additions & 0 deletions py-polars/tests/unit/test_explode.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,22 @@ def test_explode_inner_lists_3985() -> None:
.agg(pl.col("categories"))
.with_columns(pl.col("categories").arr.eval(pl.element().arr.explode()))
).collect().to_dict(False) == {"id": [1], "categories": [["a", "b", "a", "c"]]}


def test_list_struct_explode_6905() -> None:
assert pl.DataFrame(
{
"group": [
[],
[
{"params": [1]},
{"params": []},
],
]
},
schema={"group": pl.List(pl.Struct([pl.Field("params", pl.List(pl.Int32))]))},
)["group"].arr.explode().to_list() == [
{"params": None},
{"params": [1]},
{"params": []},
]

0 comments on commit c4267fc

Please sign in to comment.