-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathflatten.rs
40 lines (35 loc) · 1.42 KB
/
flatten.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use vortex_dtype::{match_each_native_ptype, Nullability, PType};
use vortex_error::{vortex_bail, VortexResult};
use vortex_scalar::BoolScalar;
use crate::array::bool::BoolArray;
use crate::array::constant::ConstantArray;
use crate::array::primitive::PrimitiveArray;
use crate::validity::Validity;
use crate::{ArrayDType, ArrayTrait};
use crate::{ArrayFlatten, Flattened};
impl ArrayFlatten for ConstantArray {
fn flatten(self) -> VortexResult<Flattened> {
let validity = match self.dtype().nullability() {
Nullability::NonNullable => Validity::NonNullable,
Nullability::Nullable => match self.scalar().is_null() {
true => Validity::AllInvalid,
false => Validity::AllValid,
},
};
if let Ok(b) = BoolScalar::try_from(self.scalar()) {
return Ok(Flattened::Bool(BoolArray::from_vec(
vec![b.value().unwrap_or_default(); self.len()],
validity,
)));
}
if let Ok(ptype) = PType::try_from(self.scalar().dtype()) {
return match_each_native_ptype!(ptype, |$P| {
Ok(Flattened::Primitive(PrimitiveArray::from_vec::<$P>(
vec![$P::try_from(self.scalar()).unwrap_or_else(|_| $P::default()); self.len()],
validity,
)))
});
}
vortex_bail!("Unsupported scalar type {}", self.dtype())
}
}