From bc166cef669f0e783acae4dd6f703e8605659251 Mon Sep 17 00:00:00 2001 From: Weijie Guo Date: Mon, 21 Aug 2023 18:01:34 +0800 Subject: [PATCH] fix(rust): List chunked builder should take care of series name (#10642) --- .../src/chunked_array/builder/list/boolean.rs | 23 ----------- .../src/chunked_array/builder/list/mod.rs | 4 +- .../src/chunked_array/builder/list/null.rs | 38 +++++++++++++++++++ crates/polars/tests/it/core/series.rs | 7 ++++ 4 files changed, 48 insertions(+), 24 deletions(-) create mode 100644 crates/polars-core/src/chunked_array/builder/list/null.rs diff --git a/crates/polars-core/src/chunked_array/builder/list/boolean.rs b/crates/polars-core/src/chunked_array/builder/list/boolean.rs index a6483dfcca31..4d7bc490cb3d 100644 --- a/crates/polars-core/src/chunked_array/builder/list/boolean.rs +++ b/crates/polars-core/src/chunked_array/builder/list/boolean.rs @@ -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)), - ) - } - } -} diff --git a/crates/polars-core/src/chunked_array/builder/list/mod.rs b/crates/polars-core/src/chunked_array/builder/list/mod.rs index 4484a4da1cd3..e4938e9fca17 100644 --- a/crates/polars-core/src/chunked_array/builder/list/mod.rs +++ b/crates/polars-core/src/chunked_array/builder/list/mod.rs @@ -4,6 +4,7 @@ mod boolean; #[cfg(feature = "dtype-categorical")] mod categorical; mod dtypes; +mod null; mod primitive; pub use anonymous::*; @@ -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::*; @@ -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, diff --git a/crates/polars-core/src/chunked_array/builder/list/null.rs b/crates/polars-core/src/chunked_array/builder/list/null.rs new file mode 100644 index 000000000000..70346ed32071 --- /dev/null +++ b/crates/polars-core/src/chunked_array/builder/list/null.rs @@ -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)), + ) + } + } +} diff --git a/crates/polars/tests/it/core/series.rs b/crates/polars/tests/it/core/series.rs index 6e87defa58b6..42b533c78f1a 100644 --- a/crates/polars/tests/it/core/series.rs +++ b/crates/polars/tests/it/core/series.rs @@ -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"); +}