Skip to content

Commit

Permalink
add 'has_validity' method
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Nov 4, 2021
1 parent 7eb17c0 commit 2d7703b
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 2 deletions.
10 changes: 9 additions & 1 deletion polars/polars-arrow/src/array/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod default_arrays;

use crate::utils::CustomIterTools;
use arrow::array::{ArrayRef, BooleanArray, ListArray, PrimitiveArray, Utf8Array};
use arrow::array::{Array, ArrayRef, BooleanArray, ListArray, PrimitiveArray, Utf8Array};
use arrow::bitmap::MutableBitmap;
use arrow::buffer::MutableBuffer;
use arrow::datatypes::DataType;
Expand Down Expand Up @@ -171,3 +171,11 @@ pub trait ListFromIter {
}
}
impl ListFromIter for ListArray<i64> {}

pub trait PolarsArray: Array {
fn has_validity(&self) -> bool {
self.validity().is_some()
}
}

impl<A: Array> PolarsArray for A {}
8 changes: 8 additions & 0 deletions polars/polars-core/src/chunked_array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,18 @@ impl<T> ChunkedArray<T> {
}

/// Get the buffer of bits representing null values
#[inline]
pub fn iter_validities(&self) -> impl Iterator<Item = Option<&Bitmap>> + '_ {
self.chunks.iter().map(|arr| arr.validity())
}

#[inline]
/// Return if any the chunks in this `[ChunkedArray]` have a validity bitmap.
/// no bitmap means no null values.
pub fn has_validity(&self) -> bool {
self.iter_validities().any(|valid| valid.is_some())
}

/// Shrink the capacity of this array to fit it's length.
pub fn shrink_to_fit(&mut self) {
self.chunks = vec![arrow::compute::concat::concatenate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ where
if self.chunks.len() == 1 {
let arr = chunks.next().unwrap();

if self.null_count() == 0 {
if !self.has_validity() {
let t = NumTakeRandomCont {
slice: arr.values(),
};
Expand Down
4 changes: 4 additions & 0 deletions polars/polars-core/src/series/implementations/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ impl SeriesTrait for SeriesWrap<BooleanChunked> {
self.0.null_count()
}

fn has_validity(&self) -> bool {
self.0.has_validity()
}

fn unique(&self) -> Result<Series> {
ChunkUnique::unique(&self.0).map(|ca| ca.into_series())
}
Expand Down
4 changes: 4 additions & 0 deletions polars/polars-core/src/series/implementations/categorical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ impl SeriesTrait for SeriesWrap<CategoricalChunked> {
self.0.null_count()
}

fn has_validity(&self) -> bool {
self.0.has_validity()
}

fn unique(&self) -> Result<Series> {
ChunkUnique::unique(&self.0).map(|ca| ca.into_series())
}
Expand Down
4 changes: 4 additions & 0 deletions polars/polars-core/src/series/implementations/dates_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,10 @@ macro_rules! impl_dyn_series {
self.0.null_count()
}

fn has_validity(&self) -> bool {
self.0.has_validity()
}

fn unique(&self) -> Result<Series> {
self.0.unique().map(|ca| ca.$into_logical().into_series())
}
Expand Down
4 changes: 4 additions & 0 deletions polars/polars-core/src/series/implementations/floats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ macro_rules! impl_dyn_series {
self.0.null_count()
}

fn has_validity(&self) -> bool {
self.0.has_validity()
}

fn unique(&self) -> Result<Series> {
ChunkUnique::unique(&self.0).map(|ca| ca.into_series())
}
Expand Down
4 changes: 4 additions & 0 deletions polars/polars-core/src/series/implementations/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ impl SeriesTrait for SeriesWrap<ListChunked> {
self.0.null_count()
}

fn has_validity(&self) -> bool {
self.0.has_validity()
}

fn is_null(&self) -> BooleanChunked {
self.0.is_null()
}
Expand Down
4 changes: 4 additions & 0 deletions polars/polars-core/src/series/implementations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,10 @@ macro_rules! impl_dyn_series {
self.0.null_count()
}

fn has_validity(&self) -> bool {
self.0.has_validity()
}

fn unique(&self) -> Result<Series> {
ChunkUnique::unique(&self.0).map(|ca| ca.into_series())
}
Expand Down
4 changes: 4 additions & 0 deletions polars/polars-core/src/series/implementations/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ where
ObjectChunked::null_count(&self.0)
}

fn has_validity(&self) -> bool {
ObjectChunked::has_validity(&self.0)
}

fn unique(&self) -> Result<Series> {
ChunkUnique::unique(&self.0).map(|ca| ca.into_series())
}
Expand Down
4 changes: 4 additions & 0 deletions polars/polars-core/src/series/implementations/utf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ impl SeriesTrait for SeriesWrap<Utf8Chunked> {
self.0.null_count()
}

fn has_validity(&self) -> bool {
self.0.has_validity()
}

fn unique(&self) -> Result<Series> {
ChunkUnique::unique(&self.0).map(|ca| ca.into_series())
}
Expand Down
4 changes: 4 additions & 0 deletions polars/polars-core/src/series/series_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,10 @@ pub trait SeriesTrait:
invalid_operation_panic!(self)
}

/// Return if any the chunks in this `[ChunkedArray]` have a validity bitmap.
/// no bitmap means no null values.
fn has_validity(&self) -> bool;

/// Get unique values in the Series.
fn unique(&self) -> Result<Series> {
invalid_operation!(self)
Expand Down

0 comments on commit 2d7703b

Please sign in to comment.