-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d784211
commit e6a7477
Showing
8 changed files
with
174 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use num_traits::Zero; | ||
|
||
use crate::array::primitive::PrimitiveArray; | ||
use crate::array::{Array, ArrayRef}; | ||
use crate::compute::cast::cast_bool; | ||
use crate::compute::fill::FillForwardFn; | ||
use crate::error::VortexResult; | ||
use crate::match_each_native_ptype; | ||
use crate::stats::Stat; | ||
|
||
impl FillForwardFn for PrimitiveArray { | ||
fn fill_forward(&self) -> VortexResult<ArrayRef> { | ||
if self.validity().is_none() { | ||
Ok(dyn_clone::clone_box(self)) | ||
} else if self | ||
.stats() | ||
.get_or_compute_as::<usize>(&Stat::NullCount) | ||
.unwrap() | ||
== 0usize | ||
{ | ||
return Ok(PrimitiveArray::new(*self.ptype(), self.buffer().clone(), None).boxed()); | ||
} else { | ||
match_each_native_ptype!(self.ptype(), |$P| { | ||
let validity = cast_bool(self.validity().unwrap())?; | ||
let typed_data = self.typed_data::<$P>(); | ||
let mut last_value = $P::zero(); | ||
let filled = typed_data | ||
.iter() | ||
.zip(validity.buffer().iter()) | ||
.map(|(v, valid)| { | ||
if valid { | ||
last_value = *v; | ||
} | ||
last_value | ||
}) | ||
.collect::<Vec<_>>(); | ||
Ok(filled.into()) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::array::bool::BoolArray; | ||
use crate::array::downcast::DowncastArrayBuiltin; | ||
use crate::array::primitive::PrimitiveArray; | ||
use crate::array::Array; | ||
use crate::compute; | ||
|
||
#[test] | ||
fn leading_none() { | ||
let arr = PrimitiveArray::from_iter(vec![None, Some(8u8), None, Some(10), None]); | ||
let filled = compute::fill::fill_forward(arr.as_ref()).unwrap(); | ||
let filled_primitive = filled.as_primitive(); | ||
assert_eq!(filled_primitive.typed_data::<u8>(), vec![0, 8, 8, 10, 10]); | ||
assert!(filled_primitive.validity().is_none()); | ||
} | ||
|
||
#[test] | ||
fn all_none() { | ||
let arr = PrimitiveArray::from_iter(vec![Option::<u8>::None, None, None, None, None]); | ||
let filled = compute::fill::fill_forward(arr.as_ref()).unwrap(); | ||
let filled_primitive = filled.as_primitive(); | ||
assert_eq!(filled_primitive.typed_data::<u8>(), vec![0, 0, 0, 0, 0]); | ||
assert!(filled_primitive.validity().is_none()); | ||
} | ||
|
||
#[test] | ||
fn nullable_non_null() { | ||
let arr = PrimitiveArray::from_nullable( | ||
vec![8u8, 10u8, 12u8, 14u8, 16u8], | ||
Some(BoolArray::from(vec![true, true, true, true, true]).boxed()), | ||
); | ||
let filled = compute::fill::fill_forward(arr.as_ref()).unwrap(); | ||
let filled_primitive = filled.as_primitive(); | ||
assert_eq!(filled_primitive.typed_data::<u8>(), vec![8, 10, 12, 14, 16]); | ||
assert!(filled_primitive.validity().is_none()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use crate::array::{Array, ArrayRef}; | ||
use crate::error::{VortexError, VortexResult}; | ||
|
||
pub trait FillForwardFn { | ||
fn fill_forward(&self) -> VortexResult<ArrayRef>; | ||
} | ||
|
||
pub fn fill_forward(array: &dyn Array) -> VortexResult<ArrayRef> { | ||
if !array.dtype().is_nullable() { | ||
return Ok(dyn_clone::clone_box(array)); | ||
} | ||
|
||
array | ||
.fill_forward() | ||
.map(|t| t.fill_forward()) | ||
.unwrap_or_else(|| { | ||
Err(VortexError::NotImplemented( | ||
"fill_forward", | ||
array.encoding().id(), | ||
)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters