Skip to content

Commit f841c9c

Browse files
committed
implement filter for primitive vector
Signed-off-by: Connor Tsui <[email protected]>
1 parent 956f5c2 commit f841c9c

File tree

3 files changed

+88
-11
lines changed

3 files changed

+88
-11
lines changed

vortex-compute/src/filter/mask.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4-
use vortex_mask::Mask;
4+
use vortex_mask::{Mask, MaskMut};
55

66
use crate::filter::Filter;
77

@@ -27,3 +27,19 @@ impl Filter for &Mask {
2727
}
2828
}
2929
}
30+
31+
impl Filter for &mut MaskMut {
32+
type Output = ();
33+
34+
fn filter(self, selection_mask: &Mask) {
35+
assert_eq!(
36+
selection_mask.len(),
37+
self.len(),
38+
"Selection mask length must equal the mask length"
39+
);
40+
41+
// TODO(connor): There is definitely a better way to do this (in place).
42+
let filtered = self.clone().freeze().filter(selection_mask).into_mut();
43+
*self = filtered;
44+
}
45+
}

vortex-compute/src/filter/vector/primitive.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,37 @@
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

44
use vortex_mask::Mask;
5-
use vortex_vector::match_each_pvector_mut;
6-
use vortex_vector::primitive::PrimitiveVectorMut;
5+
use vortex_vector::primitive::{PrimitiveVector, PrimitiveVectorMut};
6+
use vortex_vector::{VectorOps, match_each_pvector, match_each_pvector_mut};
77

88
use crate::filter::Filter;
99

10+
impl Filter for &PrimitiveVector {
11+
type Output = PrimitiveVector;
12+
13+
fn filter(self, selection_mask: &Mask) -> PrimitiveVector {
14+
match_each_pvector!(self, |v| { v.filter(selection_mask).into() })
15+
}
16+
}
17+
1018
impl Filter for &mut PrimitiveVectorMut {
1119
type Output = ();
1220

1321
fn filter(self, selection_mask: &Mask) {
1422
match_each_pvector_mut!(self, |v| { v.filter(selection_mask) })
1523
}
1624
}
25+
26+
impl Filter for PrimitiveVector {
27+
type Output = Self;
28+
29+
fn filter(self, selection_mask: &Mask) -> Self {
30+
assert_eq!(
31+
selection_mask.len(),
32+
self.len(),
33+
"Selection mask length must equal the buffer length"
34+
);
35+
36+
match_each_pvector!(self, |v| { v.filter(selection_mask).into() })
37+
}
38+
}

vortex-compute/src/filter/vector/pvector.rs

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,30 @@
33

44
use vortex_dtype::NativePType;
55
use vortex_mask::Mask;
6-
use vortex_vector::VectorMutOps;
7-
use vortex_vector::primitive::PVectorMut;
6+
use vortex_vector::primitive::{PVector, PVectorMut};
7+
use vortex_vector::{VectorMutOps, VectorOps};
88

99
use crate::filter::Filter;
1010

11+
impl<T: NativePType> Filter for &PVector<T> {
12+
type Output = PVector<T>;
13+
14+
fn filter(self, selection_mask: &Mask) -> PVector<T> {
15+
assert_eq!(
16+
selection_mask.len(),
17+
self.len(),
18+
"Selection mask length must equal the vector length"
19+
);
20+
21+
let filtered_elements = self.elements().filter(selection_mask);
22+
let filtered_validity = self.validity().filter(selection_mask);
23+
24+
// SAFETY: We filtered both components by the same mask, so the length invariants are
25+
// upheld.
26+
unsafe { PVector::new_unchecked(filtered_elements, filtered_validity) }
27+
}
28+
}
29+
1130
impl<T: NativePType> Filter for &mut PVectorMut<T> {
1231
type Output = ();
1332

@@ -18,13 +37,33 @@ impl<T: NativePType> Filter for &mut PVectorMut<T> {
1837
"Selection mask length must equal the vector length"
1938
);
2039

21-
unimplemented!()
22-
2340
// SAFETY: We filter the two components of the vector at the same time, so the length
2441
// invariants remain true.
25-
// unsafe {
26-
// self.elements_mut().filter(selection_mask);
27-
// self.validity_mut().filter(selection_mask);
28-
// }
42+
unsafe {
43+
self.elements_mut().filter(selection_mask);
44+
self.validity_mut().filter(selection_mask);
45+
}
46+
}
47+
}
48+
49+
impl<T: NativePType> Filter for PVector<T> {
50+
type Output = Self;
51+
52+
fn filter(self, selection_mask: &Mask) -> Self {
53+
assert_eq!(
54+
selection_mask.len(),
55+
self.len(),
56+
"Selection mask length must equal the buffer length"
57+
);
58+
59+
// If we have exclusive access, we can perform the filter in place.
60+
match self.try_into_mut() {
61+
Ok(mut vector_mut) => {
62+
(&mut vector_mut).filter(selection_mask);
63+
vector_mut.freeze()
64+
}
65+
// Otherwise, allocate a new buffer and fill it in (delegate to the `&PVector` impl).
66+
Err(vector) => (&vector).filter(selection_mask),
67+
}
2968
}
3069
}

0 commit comments

Comments
 (0)