vortex_array/arrays/patched/vtable/
operations.rs1use vortex_error::VortexResult;
5
6use crate::ExecutionCtx;
7use crate::array::ArrayView;
8use crate::array::OperationsVTable;
9use crate::arrays::PrimitiveArray;
10use crate::arrays::patched::Patched;
11use crate::arrays::patched::PatchedArrayExt;
12use crate::arrays::patched::PatchedArraySlotsExt;
13use crate::optimizer::ArrayOptimizer;
14use crate::scalar::Scalar;
15
16impl OperationsVTable<Patched> for Patched {
17 type ProbeState = ();
18
19 fn scalar_at(
20 array: ArrayView<'_, Patched>,
21 index: usize,
22 ctx: &mut ExecutionCtx,
23 ) -> VortexResult<Scalar> {
24 let chunk = (index + array.offset()) / 1024;
25
26 #[expect(
27 clippy::cast_possible_truncation,
28 reason = "N % 1024 always fits in u16"
29 )]
30 let chunk_index = ((index + array.offset()) % 1024) as u16;
31
32 let lane = (index + array.offset()) % array.n_lanes();
33
34 let range = array.lane_range(chunk, lane, ctx)?;
35
36 let patch_indices = array
39 .patch_indices()
40 .slice(range.clone())?
41 .optimize()?
42 .execute::<PrimitiveArray>(ctx)?;
43
44 for (&patch_index, idx) in std::iter::zip(patch_indices.as_slice::<u16>(), range) {
47 if patch_index == chunk_index {
48 return array
49 .patch_values()
50 .execute_scalar(idx, ctx)?
51 .cast(array.dtype());
52 }
53 }
54
55 array.inner().execute_scalar(index, ctx)
57 }
58}
59
60#[cfg(test)]
61mod tests {
62 use std::sync::LazyLock;
63
64 use vortex_buffer::buffer;
65 use vortex_session::VortexSession;
66
67 use crate::IntoArray;
68 use crate::VortexSessionExecute;
69 use crate::arrays::Patched;
70 use crate::dtype::Nullability;
71 use crate::optimizer::ArrayOptimizer;
72 use crate::patches::Patches;
73 use crate::scalar::Scalar;
74
75 static SESSION: LazyLock<VortexSession> = LazyLock::new(VortexSession::empty);
76
77 #[test]
78 fn test_simple() {
79 let mut ctx = SESSION.create_execution_ctx();
80 let values = buffer![0u16; 1024].into_array();
81 let patches = Patches::new(
82 1024,
83 0,
84 buffer![1u32, 2, 3].into_array(),
85 buffer![1u16; 3].into_array(),
86 None,
87 &mut ctx,
88 )
89 .unwrap();
90
91 let mut ctx = SESSION.create_execution_ctx();
92
93 let array = Patched::from_array_and_patches(values, &patches, &mut ctx)
94 .unwrap()
95 .into_array();
96
97 assert_eq!(
98 array
99 .execute_scalar(0, &mut SESSION.create_execution_ctx())
100 .unwrap(),
101 Scalar::primitive(0u16, Nullability::NonNullable)
102 );
103 assert_eq!(
104 array
105 .execute_scalar(1, &mut SESSION.create_execution_ctx())
106 .unwrap(),
107 Scalar::primitive(1u16, Nullability::NonNullable)
108 );
109 assert_eq!(
110 array
111 .execute_scalar(2, &mut SESSION.create_execution_ctx())
112 .unwrap(),
113 Scalar::primitive(1u16, Nullability::NonNullable)
114 );
115 assert_eq!(
116 array
117 .execute_scalar(3, &mut SESSION.create_execution_ctx())
118 .unwrap(),
119 Scalar::primitive(1u16, Nullability::NonNullable)
120 );
121 }
122
123 #[test]
124 fn test_multi_chunk() {
125 let mut ctx = SESSION.create_execution_ctx();
126 let values = buffer![0u16; 4096].into_array();
127 let patches = Patches::new(
128 4096,
129 0,
130 buffer![1u32, 2, 3].into_array(),
131 buffer![1u16; 3].into_array(),
132 None,
133 &mut ctx,
134 )
135 .unwrap();
136
137 let mut ctx = SESSION.create_execution_ctx();
138
139 let array = Patched::from_array_and_patches(values, &patches, &mut ctx)
140 .unwrap()
141 .into_array();
142
143 for index in 0..array.len() {
144 let value = array
145 .execute_scalar(index, &mut SESSION.create_execution_ctx())
146 .unwrap();
147
148 if [1, 2, 3].contains(&index) {
149 assert_eq!(value, 1u16.into());
150 } else {
151 assert_eq!(value, 0u16.into());
152 }
153 }
154 }
155
156 #[test]
157 fn test_multi_chunk_sliced() {
158 let mut ctx = SESSION.create_execution_ctx();
159 let values = buffer![0u16; 4096].into_array();
160 let patches = Patches::new(
161 4096,
162 0,
163 buffer![1u32, 2, 3].into_array(),
164 buffer![1u16; 3].into_array(),
165 None,
166 &mut ctx,
167 )
168 .unwrap();
169
170 let mut ctx = SESSION.create_execution_ctx();
171
172 let array = Patched::from_array_and_patches(values, &patches, &mut ctx)
173 .unwrap()
174 .into_array()
175 .slice(3..4096)
176 .unwrap()
177 .optimize()
178 .unwrap();
179
180 assert!(array.is::<Patched>());
181
182 assert_eq!(
183 array
184 .execute_scalar(0, &mut SESSION.create_execution_ctx())
185 .unwrap(),
186 1u16.into()
187 );
188 for index in 1..array.len() {
189 assert_eq!(
190 array
191 .execute_scalar(index, &mut SESSION.create_execution_ctx())
192 .unwrap(),
193 0u16.into()
194 );
195 }
196 }
197}