-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathoperations.rs
More file actions
294 lines (266 loc) · 9.19 KB
/
Copy pathoperations.rs
File metadata and controls
294 lines (266 loc) · 9.19 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use vortex_array::ArrayView;
use vortex_array::ExecutionCtx;
use vortex_array::scalar::Scalar;
use vortex_array::vtable::OperationsVTable;
use vortex_error::VortexResult;
use crate::BitPacked;
use crate::bitpack_decompress;
use crate::bitpacking::array::BitPackedArrayExt;
impl OperationsVTable<BitPacked> for BitPacked {
type ProbeState = ();
fn scalar_at(
array: ArrayView<'_, BitPacked>,
index: usize,
_ctx: &mut ExecutionCtx,
) -> VortexResult<Scalar> {
Ok(
if let Some(patches) = array.patches()
&& let Some(patch) = patches.get_patched(index)?
{
patch
} else {
bitpack_decompress::unpack_single(array, index)
},
)
}
}
#[cfg(test)]
mod test {
use std::ops::Range;
use vortex_array::ArrayRef;
use vortex_array::IntoArray;
use vortex_array::VortexSessionExecute;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::arrays::SliceArray;
use vortex_array::assert_arrays_eq;
use vortex_array::assert_nth_scalar;
use vortex_array::buffer::BufferHandle;
use vortex_array::dtype::DType;
use vortex_array::dtype::Nullability;
use vortex_array::dtype::PType;
use vortex_array::patches::Patches;
use vortex_array::scalar::Scalar;
use vortex_array::validity::Validity;
use vortex_buffer::Alignment;
use vortex_buffer::Buffer;
use vortex_buffer::ByteBuffer;
use vortex_buffer::buffer;
use crate::BitPacked;
use crate::BitPackedArray;
use crate::BitPackedData;
use crate::bitpacking::array::BitPackedArrayExt;
use crate::test::SESSION;
fn bp(array: &ArrayRef, bit_width: u8) -> BitPackedArray {
BitPackedData::encode(array, bit_width, &mut SESSION.create_execution_ctx()).unwrap()
}
fn slice_via_reduce(array: &BitPackedArray, range: Range<usize>) -> BitPackedArray {
let array_ref = array.clone().into_array();
let slice_array = SliceArray::new(array_ref.clone(), range);
let sliced = array_ref
.reduce_parent(&slice_array.into_array(), 0)
.expect("execute_parent failed")
.expect("expected slice kernel to execute");
sliced.as_::<BitPacked>().into_owned()
}
#[test]
pub fn slice_block() {
let arr = bp(
&PrimitiveArray::from_iter((0u32..2048).map(|v| v % 64)).into_array(),
6,
);
let sliced = slice_via_reduce(&arr, 1024..2048);
assert_nth_scalar!(sliced, 0, 1024u32 % 64, &mut SESSION.create_execution_ctx());
assert_nth_scalar!(
sliced,
1023,
2047u32 % 64,
&mut SESSION.create_execution_ctx()
);
assert_eq!(sliced.offset(), 0);
assert_eq!(sliced.len(), 1024);
}
#[test]
pub fn slice_within_block() {
let arr = bp(
&PrimitiveArray::from_iter((0u32..2048).map(|v| v % 64)).into_array(),
6,
);
let sliced = slice_via_reduce(&arr, 512..1434);
assert_nth_scalar!(sliced, 0, 512u32 % 64, &mut SESSION.create_execution_ctx());
assert_nth_scalar!(
sliced,
921,
1433u32 % 64,
&mut SESSION.create_execution_ctx()
);
assert_eq!(sliced.offset(), 512);
assert_eq!(sliced.len(), 922);
}
#[test]
fn slice_within_block_u8s() {
let packed = bp(
&PrimitiveArray::from_iter((0..10_000).map(|i| (i % 63) as u8)).into_array(),
7,
);
let compressed = packed.slice(768..9999).unwrap();
assert_nth_scalar!(
compressed,
0,
(768 % 63) as u8,
&mut SESSION.create_execution_ctx()
);
assert_nth_scalar!(
compressed,
compressed.len() - 1,
(9998 % 63) as u8,
&mut SESSION.create_execution_ctx()
);
}
#[test]
fn slice_block_boundary_u8s() {
let packed = bp(
&PrimitiveArray::from_iter((0..10_000).map(|i| (i % 63) as u8)).into_array(),
7,
);
let compressed = packed.slice(7168..9216).unwrap();
assert_nth_scalar!(
compressed,
0,
(7168 % 63) as u8,
&mut SESSION.create_execution_ctx()
);
assert_nth_scalar!(
compressed,
compressed.len() - 1,
(9215 % 63) as u8,
&mut SESSION.create_execution_ctx()
);
}
#[test]
fn double_slice_within_block() {
let arr = bp(
&PrimitiveArray::from_iter((0u32..2048).map(|v| v % 64)).into_array(),
6,
);
let sliced = slice_via_reduce(&arr, 512..1434);
assert_nth_scalar!(sliced, 0, 512u32 % 64, &mut SESSION.create_execution_ctx());
assert_nth_scalar!(
sliced,
921,
1433u32 % 64,
&mut SESSION.create_execution_ctx()
);
assert_eq!(sliced.offset(), 512);
assert_eq!(sliced.len(), 922);
let doubly_sliced = slice_via_reduce(&sliced, 127..911);
assert_nth_scalar!(
doubly_sliced,
0,
(512u32 + 127) % 64,
&mut SESSION.create_execution_ctx()
);
assert_nth_scalar!(
doubly_sliced,
783,
(512u32 + 910) % 64,
&mut SESSION.create_execution_ctx()
);
assert_eq!(doubly_sliced.offset(), 639);
assert_eq!(doubly_sliced.len(), 784);
}
#[test]
fn slice_empty_patches() {
let mut ctx = SESSION.create_execution_ctx();
// We create an array that has 1 element that does not fit in the 6-bit range.
let array = BitPackedData::encode(&buffer![0u32..=64].into_array(), 6, &mut ctx).unwrap();
assert!(array.patches().is_some());
let patch_indices = array.patches().unwrap().indices().clone();
assert_eq!(patch_indices.len(), 1);
// Slicing with patches requires the execute path (not reduce) since patches.slice()
// reads buffers. The slice range 0..64 excludes the patch at index 64, so the
// resulting array should have no patches.
let array_ref = array.into_array();
let slice_array = SliceArray::new(array_ref, 0..64);
let mut ctx = SESSION.create_execution_ctx();
let sliced_bp = slice_array
.into_array()
.execute::<ArrayRef>(&mut ctx)
.expect("slice execution failed")
.as_::<BitPacked>()
.into_owned();
assert!(sliced_bp.patches().is_none());
}
#[test]
fn take_after_slice() {
// Check that our take implementation respects the offsets applied after slicing.
let array = bp(
&PrimitiveArray::from_iter((63u32..).take(3072)).into_array(),
6,
);
// Slice the array.
// The resulting array will still have 3 1024-element chunks.
let sliced = array.slice(922..2061).unwrap();
// Take one element from each chunk.
// Chunk 1: physical indices 922-1023, logical indices 0-101
// Chunk 2: physical indices 1024-2047, logical indices 102-1125
// Chunk 3: physical indices 2048-2060, logical indices 1126-1138
let taken = sliced
.take(buffer![101i64, 1125, 1138].into_array())
.unwrap();
assert_eq!(taken.len(), 3);
}
#[test]
fn scalar_at_invalid_patches() {
let packed_array = BitPacked::try_new(
BufferHandle::new_host(ByteBuffer::copy_from_aligned(
[0u8; 128],
Alignment::of::<u32>(),
)),
PType::U32,
Validity::AllInvalid,
Some(
Patches::new(
8,
0,
buffer![1u32].into_array(),
PrimitiveArray::new(buffer![999u32], Validity::AllValid).into_array(),
None,
)
.unwrap(),
),
1,
8,
0,
)
.unwrap()
.into_array();
assert_eq!(
packed_array
.execute_scalar(1, &mut SESSION.create_execution_ctx())
.unwrap(),
Scalar::null(DType::Primitive(PType::U32, Nullability::Nullable))
);
}
#[test]
fn scalar_at() {
let mut ctx = SESSION.create_execution_ctx();
let values = (0u32..257).collect::<Buffer<_>>();
let uncompressed = values.clone().into_array();
let packed = BitPackedData::encode(&uncompressed, 8, &mut ctx).unwrap();
assert!(packed.patches().is_some());
let patches = packed.patches().unwrap().indices().clone();
assert_eq!(
usize::try_from(
&patches
.execute_scalar(0, &mut SESSION.create_execution_ctx())
.unwrap()
)
.unwrap(),
256
);
let expected = PrimitiveArray::from_iter(values.iter().copied());
assert_arrays_eq!(packed, expected, &mut ctx);
}
}