-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathzip.rs
More file actions
106 lines (94 loc) · 3.33 KB
/
Copy pathzip.rs
File metadata and controls
106 lines (94 loc) · 3.33 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use vortex_error::VortexResult;
use crate::ArrayRef;
use crate::ExecutionCtx;
use crate::IntoArray;
use crate::arrays::Chunked;
use crate::arrays::ChunkedArray;
use crate::builtins::ArrayBuiltins;
use crate::scalar_fn::fns::zip::ZipKernel;
// Push down the zip call to the chunks. Without this rule
// the default implementation canonicalises the chunked array
// then zips once.
impl ZipKernel for Chunked {
fn zip(
if_true: &ChunkedArray,
if_false: &ArrayRef,
mask: &ArrayRef,
_ctx: &mut ExecutionCtx,
) -> VortexResult<Option<ArrayRef>> {
let Some(if_false) = if_false.as_opt::<Chunked>() else {
return Ok(None);
};
let dtype = if_true
.dtype()
.union_nullability(if_false.dtype().nullability());
let mut out_chunks = Vec::with_capacity(if_true.nchunks() + if_false.nchunks());
for pair in if_true.paired_chunks(if_false) {
let pair = pair?;
let mask_slice = mask.slice(pair.pos)?;
out_chunks.push(mask_slice.zip(pair.left, pair.right)?);
}
// SAFETY: chunks originate from zipping slices of inputs that share dtype/nullability.
let chunked = unsafe { ChunkedArray::new_unchecked(out_chunks, dtype) };
Ok(Some(chunked.into_array()))
}
}
#[cfg(test)]
mod tests {
use vortex_buffer::buffer;
use vortex_mask::Mask;
use crate::ArrayRef;
use crate::IntoArray;
use crate::LEGACY_SESSION;
use crate::ToCanonical;
use crate::VortexSessionExecute;
use crate::arrays::Chunked;
use crate::arrays::ChunkedArray;
use crate::builtins::ArrayBuiltins;
use crate::dtype::DType;
use crate::dtype::Nullability;
use crate::dtype::PType;
#[test]
fn test_chunked_zip_aligns_across_boundaries() {
let if_true = ChunkedArray::try_new(
vec![
buffer![1i32, 2].into_array(),
buffer![3i32].into_array(),
buffer![4i32, 5].into_array(),
],
DType::Primitive(PType::I32, Nullability::NonNullable),
)
.unwrap();
let if_false = ChunkedArray::try_new(
vec![
buffer![10i32].into_array(),
buffer![11i32, 12].into_array(),
buffer![13i32, 14].into_array(),
],
DType::Primitive(PType::I32, Nullability::NonNullable),
)
.unwrap();
let mask = Mask::from_iter([true, false, true, false, true]);
let zipped = &mask
.into_array()
.zip(if_true.into_array(), if_false.into_array())
.unwrap();
// One step of execution will push down the zip.
let zipped = zipped
.clone()
.execute::<ArrayRef>(&mut LEGACY_SESSION.create_execution_ctx())
.unwrap();
let zipped = zipped
.as_opt::<Chunked>()
.expect("zip should keep chunked encoding");
assert_eq!(zipped.nchunks(), 4);
let mut values: Vec<i32> = Vec::new();
for chunk in zipped.chunks() {
let primitive = chunk.to_primitive();
values.extend_from_slice(primitive.as_slice::<i32>());
}
assert_eq!(values, vec![1, 11, 3, 13, 5]);
}
}