Skip to content

Commit

Permalink
fix(rust): List<null> chunked builder should take care of series name (
Browse files Browse the repository at this point in the history
  • Loading branch information
reswqa authored Aug 21, 2023
1 parent 08154e5 commit bc166ce
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
23 changes: 0 additions & 23 deletions crates/polars-core/src/chunked_array/builder/list/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,3 @@ impl ListBuilderTrait for ListBooleanChunkedBuilder {
self.fast_explode
}
}

impl ListBuilderTrait for LargeListNullBuilder {
#[inline]
fn append_series(&mut self, _s: &Series) -> PolarsResult<()> {
self.push_null();
Ok(())
}

#[inline]
fn append_null(&mut self) {
self.push_null()
}

fn finish(&mut self) -> ListChunked {
unsafe {
ListChunked::from_chunks_and_dtype_unchecked(
"",
vec![self.as_box()],
DataType::List(Box::new(DataType::Null)),
)
}
}
}
4 changes: 3 additions & 1 deletion crates/polars-core/src/chunked_array/builder/list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod boolean;
#[cfg(feature = "dtype-categorical")]
mod categorical;
mod dtypes;
mod null;
mod primitive;

pub use anonymous::*;
Expand All @@ -12,6 +13,7 @@ pub use boolean::*;
#[cfg(feature = "dtype-categorical")]
use categorical::*;
use dtypes::*;
use null::*;
use polars_arrow::array::list::AnonymousBuilder;
use polars_arrow::array::null::MutableNullArray;
use polars_arrow::prelude::*;
Expand Down Expand Up @@ -116,7 +118,7 @@ pub fn get_list_builder(
list_capacity,
Some(inner_type_logical.clone()),
))),
DataType::Null => Ok(Box::new(LargeListNullBuilder::with_capacity(list_capacity))),
DataType::Null => Ok(Box::new(ListNullChunkedBuilder::new(name, list_capacity))),
DataType::List(_) => Ok(Box::new(AnonymousOwnedListBuilder::new(
name,
list_capacity,
Expand Down
38 changes: 38 additions & 0 deletions crates/polars-core/src/chunked_array/builder/list/null.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use super::*;

pub struct ListNullChunkedBuilder {
builder: LargeListNullBuilder,
name: String,
}

impl ListNullChunkedBuilder {
pub fn new(name: &str, capacity: usize) -> Self {
ListNullChunkedBuilder {
builder: LargeListNullBuilder::with_capacity(capacity),
name: name.into(),
}
}
}

impl ListBuilderTrait for ListNullChunkedBuilder {
#[inline]
fn append_series(&mut self, _s: &Series) -> PolarsResult<()> {
self.builder.push_null();
Ok(())
}

#[inline]
fn append_null(&mut self) {
self.builder.push_null();
}

fn finish(&mut self) -> ListChunked {
unsafe {
ListChunked::from_chunks_and_dtype_unchecked(
&self.name,
vec![self.builder.as_box()],
DataType::List(Box::new(DataType::Null)),
)
}
}
}
7 changes: 7 additions & 0 deletions crates/polars/tests/it/core/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,10 @@ fn test_min_max_sorted_desc() {
assert_eq!(a.max(), Some(4));
assert_eq!(a.min(), Some(1));
}

#[test]
fn test_construct_list_of_null_series() {
let s = Series::new("a", [Series::new_null("a1", 1), Series::new_null("a1", 1)]);
assert_eq!(s.null_count(), s.len());
assert_eq!(s.field().name(), "a");
}

0 comments on commit bc166ce

Please sign in to comment.