-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy patharray.rs
More file actions
68 lines (57 loc) · 2.17 KB
/
Copy patharray.rs
File metadata and controls
68 lines (57 loc) · 2.17 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use crate::ArrayRef;
use crate::dtype::DType;
use crate::stats::ArrayStats;
use crate::validity::Validity;
use crate::vtable::child_to_validity;
use crate::vtable::validity_to_child;
/// The underlying child array being masked.
pub(super) const CHILD_SLOT: usize = 0;
/// The validity bitmap defining which elements are non-null.
pub(super) const VALIDITY_SLOT: usize = 1;
pub(super) const NUM_SLOTS: usize = 2;
pub(super) const SLOT_NAMES: [&str; NUM_SLOTS] = ["child", "validity"];
#[derive(Clone, Debug)]
pub struct MaskedArray {
pub(super) slots: Vec<Option<ArrayRef>>,
pub(super) dtype: DType,
pub(super) stats: ArrayStats,
}
impl MaskedArray {
pub fn try_new(child: ArrayRef, validity: Validity) -> VortexResult<Self> {
if matches!(validity, Validity::NonNullable) {
vortex_bail!("MaskedArray must have nullable validity, got {validity:?}")
}
if !child.all_valid()? {
vortex_bail!("MaskedArray children must not have nulls");
}
if let Some(validity_len) = validity.maybe_len()
&& validity_len != child.len()
{
vortex_bail!("Validity must be the same length as a MaskedArray's child");
}
// MaskedArray's nullability is determined solely by its validity, not the child's dtype.
// The child can have nullable dtype but must not have any actual null values.
let dtype = child.dtype().as_nullable();
let len = child.len();
let validity_slot = validity_to_child(&validity, len);
Ok(Self {
slots: vec![Some(child), validity_slot],
dtype,
stats: ArrayStats::default(),
})
}
/// Reconstructs the validity from the slots.
pub fn validity(&self) -> Validity {
child_to_validity(&self.slots[VALIDITY_SLOT], self.dtype.nullability())
}
pub fn child(&self) -> &ArrayRef {
self.slots[CHILD_SLOT]
.as_ref()
.vortex_expect("MaskedArray child slot")
}
}