-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathvtable.rs
More file actions
206 lines (177 loc) · 5.55 KB
/
vtable.rs
File metadata and controls
206 lines (177 loc) · 5.55 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use std::fmt::Debug;
use std::fmt::Formatter;
use std::hash::Hash;
use std::hash::Hasher;
use std::ops::Range;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure;
use vortex_error::vortex_panic;
use vortex_session::VortexSession;
use crate::AnyCanonical;
use crate::ArrayEq;
use crate::ArrayHash;
use crate::ArrayRef;
use crate::Precision;
use crate::array::Array;
use crate::array::ArrayId;
use crate::array::ArrayView;
use crate::array::OperationsVTable;
use crate::array::VTable;
use crate::array::ValidityVTable;
use crate::arrays::slice::SliceArrayExt;
use crate::arrays::slice::array::CHILD_SLOT;
use crate::arrays::slice::array::SLOT_NAMES;
use crate::arrays::slice::array::SliceData;
use crate::arrays::slice::rules::PARENT_RULES;
use crate::buffer::BufferHandle;
use crate::dtype::DType;
use crate::executor::ExecutionCtx;
use crate::executor::ExecutionResult;
use crate::require_child;
use crate::scalar::Scalar;
use crate::serde::ArrayChildren;
use crate::validity::Validity;
/// A [`Slice`]-encoded Vortex array.
pub type SliceArray = Array<Slice>;
#[derive(Clone, Debug)]
pub struct Slice;
impl Slice {
pub const ID: ArrayId = ArrayId::new_ref("vortex.slice");
}
impl ArrayHash for SliceData {
fn array_hash<H: Hasher>(&self, state: &mut H, _precision: Precision) {
self.range.start.hash(state);
self.range.end.hash(state);
}
}
impl ArrayEq for SliceData {
fn array_eq(&self, other: &Self, _precision: Precision) -> bool {
self.range == other.range
}
}
impl VTable for Slice {
type ArrayData = SliceData;
type OperationsVTable = Self;
type ValidityVTable = Self;
fn id(&self) -> ArrayId {
Slice::ID
}
fn validate(
&self,
data: &Self::ArrayData,
dtype: &DType,
len: usize,
slots: &[Option<ArrayRef>],
) -> VortexResult<()> {
vortex_ensure!(
slots[CHILD_SLOT].is_some(),
"SliceArray child slot must be present"
);
let child = slots[CHILD_SLOT]
.as_ref()
.vortex_expect("validated child slot");
vortex_ensure!(
child.dtype() == dtype,
"SliceArray dtype {} does not match outer dtype {}",
child.dtype(),
dtype
);
vortex_ensure!(
data.len() == len,
"SliceArray length {} does not match outer length {}",
data.len(),
len
);
vortex_ensure!(
data.range.end <= child.len(),
"SliceArray range {:?} exceeds child length {}",
data.range,
child.len()
);
Ok(())
}
fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
0
}
fn buffer(_array: ArrayView<'_, Self>, _idx: usize) -> BufferHandle {
vortex_panic!("SliceArray has no buffers")
}
fn buffer_name(_array: ArrayView<'_, Self>, _idx: usize) -> Option<String> {
None
}
fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
SLOT_NAMES[idx].to_string()
}
fn serialize(_array: ArrayView<'_, Self>) -> VortexResult<Option<Vec<u8>>> {
// TODO(joe): make this configurable
vortex_bail!("Slice array is not serializable")
}
fn deserialize(
&self,
_dtype: &DType,
_len: usize,
_metadata: &[u8],
_buffers: &[BufferHandle],
_children: &dyn ArrayChildren,
_session: &VortexSession,
) -> VortexResult<crate::array::ArrayParts<Self>> {
vortex_bail!("Slice array is not serializable")
}
fn execute(array: Array<Self>, _ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
let array = require_child!(array, array.child(), CHILD_SLOT => AnyCanonical);
debug_assert!(array.child().is_canonical());
// TODO(ngates): we should inline canonical slice logic here.
array
.child()
.slice(array.range.clone())
.map(ExecutionResult::done)
}
fn reduce_parent(
array: ArrayView<'_, Self>,
parent: &ArrayRef,
child_idx: usize,
) -> VortexResult<Option<ArrayRef>> {
PARENT_RULES.evaluate(array, parent, child_idx)
}
}
impl OperationsVTable<Slice> for Slice {
fn scalar_at(
array: ArrayView<'_, Slice>,
index: usize,
_ctx: &mut ExecutionCtx,
) -> VortexResult<Scalar> {
array.child().scalar_at(array.range.start + index)
}
}
impl ValidityVTable<Slice> for Slice {
fn validity(array: ArrayView<'_, Slice>) -> VortexResult<Validity> {
array.child().validity()?.slice(array.range.clone())
}
}
pub struct SliceMetadata(pub(super) Range<usize>);
impl Debug for SliceMetadata {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}..{}", self.0.start, self.0.end)
}
}
#[cfg(test)]
mod tests {
use vortex_error::VortexResult;
use crate::IntoArray;
use crate::arrays::PrimitiveArray;
use crate::arrays::SliceArray;
use crate::assert_arrays_eq;
#[test]
fn test_slice_slice() -> VortexResult<()> {
// Slice(1..4, Slice(2..8, base)) combines to Slice(3..6, base)
let arr = PrimitiveArray::from_iter(0i32..10).into_array();
let inner_slice = SliceArray::new(arr, 2..8).into_array();
let slice = inner_slice.slice(1..4)?;
assert_arrays_eq!(slice, PrimitiveArray::from_iter([3i32, 4, 5]));
Ok(())
}
}