vortex_array/arrays/slice/
vtable.rs1use std::fmt::Debug;
5use std::fmt::Formatter;
6use std::hash::Hash;
7use std::hash::Hasher;
8use std::ops::Range;
9
10use vortex_error::VortexExpect;
11use vortex_error::VortexResult;
12use vortex_error::vortex_ensure;
13use vortex_error::vortex_ensure_eq;
14use vortex_error::vortex_err;
15use vortex_error::vortex_panic;
16use vortex_session::registry::CachedId;
17
18use crate::AnyCanonical;
19use crate::ArrayEq;
20use crate::ArrayHash;
21use crate::ArrayParts;
22use crate::ArrayRef;
23use crate::EqMode;
24use crate::array::Array;
25use crate::array::ArrayId;
26use crate::array::ArrayView;
27use crate::array::OperationsVTable;
28use crate::array::ProbeState;
29use crate::array::VTable;
30use crate::array::ValidityVTable;
31use crate::array::with_empty_buffers;
32use crate::arrays::slice::SliceArraySlotsExt;
33use crate::arrays::slice::array::SliceData;
34use crate::arrays::slice::array::SliceSlots;
35use crate::buffer::BufferHandle;
36use crate::dtype::DType;
37use crate::executor::ExecutionCtx;
38use crate::executor::ExecutionResult;
39use crate::optimizer::ArrayOptimizer;
40use crate::require_child;
41use crate::scalar::Scalar;
42use crate::validity::Validity;
43
44pub type SliceArray = Array<Slice>;
46
47#[derive(Clone, Debug)]
48pub struct Slice;
49
50impl ArrayHash for SliceData {
51 fn array_hash<H: Hasher>(&self, state: &mut H, _accuracy: EqMode) {
52 self.range.start.hash(state);
53 self.range.end.hash(state);
54 }
55}
56
57impl ArrayEq for SliceData {
58 fn array_eq(&self, other: &Self, _accuracy: EqMode) -> bool {
59 self.range == other.range
60 }
61}
62
63impl VTable for Slice {
64 type TypedArrayData = SliceData;
65 type OperationsVTable = Self;
66 type ValidityVTable = Self;
67 fn id(&self) -> ArrayId {
68 static ID: CachedId = CachedId::new("vortex.slice");
69 *ID
70 }
71
72 fn validate(
73 &self,
74 data: &Self::TypedArrayData,
75 dtype: &DType,
76 len: usize,
77 slots: &[Option<ArrayRef>],
78 ) -> VortexResult<()> {
79 vortex_ensure!(
80 slots[SliceSlots::CHILD].is_some(),
81 "SliceArray child slot must be present"
82 );
83 let child = slots[SliceSlots::CHILD]
84 .as_ref()
85 .vortex_expect("validated child slot");
86 vortex_ensure_eq!(
87 child.dtype(),
88 dtype,
89 "SliceArray dtype does not match outer dtype",
90 );
91 vortex_ensure_eq!(
92 data.len(),
93 len,
94 "SliceArray length does not match outer length",
95 );
96 vortex_ensure!(
97 data.range.end <= child.len(),
98 "SliceArray range {:?} exceeds child length {}",
99 data.range,
100 child.len()
101 );
102 Ok(())
103 }
104
105 fn nbuffers(_array: ArrayView<'_, Self>) -> usize {
106 0
107 }
108
109 fn buffer(_array: ArrayView<'_, Self>, _idx: usize) -> BufferHandle {
110 vortex_panic!("SliceArray has no buffers")
111 }
112
113 fn buffer_name(_array: ArrayView<'_, Self>, _idx: usize) -> Option<String> {
114 None
115 }
116
117 fn with_buffers(
118 &self,
119 array: ArrayView<'_, Self>,
120 buffers: &[BufferHandle],
121 ) -> VortexResult<ArrayParts<Self>> {
122 with_empty_buffers(self, array, buffers)
123 }
124
125 fn slot_name(_array: ArrayView<'_, Self>, idx: usize) -> String {
126 SliceSlots::NAMES[idx].to_string()
127 }
128
129 fn execute(array: Array<Self>, ctx: &mut ExecutionCtx) -> VortexResult<ExecutionResult> {
130 let array = require_child!(array, array.child(), SliceSlots::CHILD => AnyCanonical);
131
132 debug_assert!(array.child().is_canonical());
133 array
135 .child()
136 .slice(array.range.clone())?
137 .optimize_ctx(ctx)
138 .map(ExecutionResult::done)
139 }
140}
141impl OperationsVTable<Slice> for Slice {
142 type ProbeState = ();
143
144 fn probe_scalar(
145 state: &mut ProbeState<'_, Slice>,
146 index: usize,
147 ctx: &mut ExecutionCtx,
148 ) -> VortexResult<Scalar> {
149 let child_index = state.array().range.start + index;
150 state
151 .slot(SliceSlots::CHILD)?
152 .ok_or_else(|| vortex_err!("Slice child slot is missing"))?
153 .execute_scalar(child_index, ctx)
154 }
155
156 fn scalar_at(
157 array: ArrayView<'_, Slice>,
158 index: usize,
159 ctx: &mut ExecutionCtx,
160 ) -> VortexResult<Scalar> {
161 Self::probe_scalar(&mut ProbeState::once(array), index, ctx)
162 }
163}
164
165impl ValidityVTable<Slice> for Slice {
166 fn validity(array: ArrayView<'_, Slice>) -> VortexResult<Validity> {
167 array.child().validity()?.slice(array.range.clone())
168 }
169}
170
171pub struct SliceMetadata(pub(super) Range<usize>);
172
173impl Debug for SliceMetadata {
174 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
175 write!(f, "{}..{}", self.0.start, self.0.end)
176 }
177}
178
179#[cfg(test)]
180mod tests {
181 use std::sync::LazyLock;
182
183 use vortex_error::VortexResult;
184 use vortex_session::VortexSession;
185
186 use crate::IntoArray;
187 use crate::VortexSessionExecute;
188 use crate::arrays::PrimitiveArray;
189 use crate::arrays::SliceArray;
190 use crate::assert_arrays_eq;
191
192 static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
193
194 #[test]
195 fn test_slice_slice() -> VortexResult<()> {
196 let mut ctx = SESSION.create_execution_ctx();
197 let arr = PrimitiveArray::from_iter(0i32..10).into_array();
199 let inner_slice = SliceArray::new(arr, 2..8).into_array();
200 let slice = inner_slice.slice(1..4)?;
201
202 assert_arrays_eq!(slice, PrimitiveArray::from_iter([3i32, 4, 5]), &mut ctx);
203
204 Ok(())
205 }
206}