-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathvalidity.rs
More file actions
72 lines (62 loc) · 2.45 KB
/
Copy pathvalidity.rs
File metadata and controls
72 lines (62 loc) · 2.45 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
69
70
71
72
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use vortex_error::VortexResult;
use crate::ArrayRef;
use crate::array::ArrayView;
use crate::array::VTable;
use crate::validity::Validity;
/// Validity access for nullable instances of an encoding.
///
/// Non-nullable arrays bypass this hook and report [`Validity::NonNullable`]. Nullable arrays call
/// into the encoding so it can expose either a constant validity state or a row-aligned boolean
/// child array.
pub trait ValidityVTable<V: VTable> {
/// Returns the [`Validity`] of the array.
///
/// ## Pre-conditions
///
/// - The array DType is nullable.
///
/// ## Post-conditions
///
/// If this returns [`Validity::Array`], the child array must have the same length as `array`
/// and non-nullable boolean dtype.
fn validity(array: ArrayView<'_, V>) -> VortexResult<Validity>;
}
/// An implementation of the [`ValidityVTable`] for arrays that delegate validity entirely
/// to a child array.
pub struct ValidityVTableFromChild;
/// Helper trait for encodings whose validity is exactly one child slot.
pub trait ValidityChild<V: VTable> {
/// Returns the child array that carries validity for `array`.
fn validity_child(array: ArrayView<'_, V>) -> ArrayRef;
}
impl<V: VTable> ValidityVTable<V> for ValidityVTableFromChild
where
V: ValidityChild<V>,
{
fn validity(array: ArrayView<'_, V>) -> VortexResult<Validity> {
V::validity_child(array).validity()
}
}
/// An implementation of the [`ValidityVTable`] for arrays that hold an unsliced validity
/// and a slice into it.
pub struct ValidityVTableFromChildSliceHelper;
/// Helper for encodings that keep an unsliced validity child plus a local slice range.
pub trait ValidityChildSliceHelper {
/// Returns `(unsliced_validity, start, stop)` for this array's logical slice.
fn unsliced_child_and_slice(&self) -> (&ArrayRef, usize, usize);
/// Returns a sliced validity child array for the logical range.
fn sliced_child_array(&self) -> VortexResult<ArrayRef> {
let (unsliced_validity, start, stop) = self.unsliced_child_and_slice();
unsliced_validity.slice(start..stop)
}
}
impl<V: VTable> ValidityVTable<V> for ValidityVTableFromChildSliceHelper
where
V::TypedArrayData: ValidityChildSliceHelper,
{
fn validity(array: ArrayView<'_, V>) -> VortexResult<Validity> {
array.data().sliced_child_array()?.validity()
}
}