-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathfilter.rs
More file actions
139 lines (134 loc) · 5.02 KB
/
Copy pathfilter.rs
File metadata and controls
139 lines (134 loc) · 5.02 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use vortex_array::ArrayRef;
use vortex_array::ExecutionCtx;
use vortex_array::IntoArray;
use vortex_array::arrays::BoolArray;
use vortex_array::arrays::DecimalArray;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::arrays::StructArray;
use vortex_array::arrays::VarBinViewArray;
use vortex_array::arrays::bool::BoolArrayExt;
use vortex_array::arrays::struct_::StructArrayExt;
use vortex_array::builders::builder_with_capacity;
use vortex_array::dtype::DType;
use vortex_array::match_each_decimal_value_type;
use vortex_array::match_each_native_ptype;
use vortex_array::validity::Validity;
use vortex_buffer::BitBuffer;
use vortex_buffer::Buffer;
use vortex_error::VortexResult;
use crate::array::take_canonical_array_non_nullable_indices;
pub fn filter_canonical_array(
array: &ArrayRef,
filter: &[bool],
ctx: &mut ExecutionCtx,
) -> VortexResult<ArrayRef> {
let validity = if array.dtype().is_nullable() {
let validity_buff = array
.validity()?
.execute_mask(array.len(), ctx)?
.to_bit_buffer();
Validity::from_iter(
filter
.iter()
.zip(validity_buff.iter())
.filter(|(f, _)| **f)
.map(|(_, v)| v),
)
} else {
Validity::NonNullable
};
match array.dtype() {
DType::Bool(_) => {
let bool_array = array.clone().execute::<BoolArray>(ctx)?;
Ok(BoolArray::new(
BitBuffer::from_iter(
filter
.iter()
.zip(bool_array.to_bit_buffer().iter())
.filter(|(f, _)| **f)
.map(|(_, v)| v),
),
validity,
)
.into_array())
}
DType::Primitive(p, _) => match_each_native_ptype!(p, |P| {
let primitive_array = array.clone().execute::<PrimitiveArray>(ctx)?;
Ok(PrimitiveArray::new(
filter
.iter()
.zip(primitive_array.as_slice::<P>().iter().copied())
.filter(|(f, _)| **f)
.map(|(_, v)| v)
.collect::<Buffer<_>>(),
validity,
)
.into_array())
}),
DType::Decimal(d, _) => {
let decimal_array = array.clone().execute::<DecimalArray>(ctx)?;
match_each_decimal_value_type!(decimal_array.values_type(), |D| {
let buf = decimal_array.buffer::<D>();
Ok(DecimalArray::new(
filter
.iter()
.zip(buf.as_slice().iter().copied())
.filter(|(f, _)| **f)
.map(|(_, v)| v)
.collect::<Buffer<_>>(),
*d,
validity,
)
.into_array())
})
}
DType::Utf8(_) | DType::Binary(_) => {
let utf8 = array.clone().execute::<VarBinViewArray>(ctx)?;
let mask = utf8.validity()?.execute_mask(utf8.len(), ctx)?;
let values = (0..utf8.len())
.zip(filter.iter())
.filter(|(_, f)| **f)
.map(|(i, _)| mask.value(i).then(|| utf8.bytes_at(i).to_vec()))
.collect::<Vec<_>>();
Ok(VarBinViewArray::from_iter(values, array.dtype().clone()).into_array())
}
DType::List(..) | DType::FixedSizeList(..) => {
let mut indices = Vec::new();
for (idx, bool) in filter.iter().enumerate() {
if *bool {
indices.push(idx);
}
}
take_canonical_array_non_nullable_indices(array, indices.as_slice(), ctx)
}
DType::Struct(..) => {
let struct_array = array.clone().execute::<StructArray>(ctx)?;
let filtered_children = struct_array
.iter_unmasked_fields()
.map(|c| filter_canonical_array(c, filter, ctx))
.collect::<VortexResult<Vec<_>>>()?;
StructArray::try_new_with_dtype(
filtered_children,
struct_array.struct_fields().clone(),
filter.iter().filter(|b| **b).map(|b| *b as usize).sum(),
validity,
)
.map(|a| a.into_array())
}
DType::Map(..) => {
let mut builder =
builder_with_capacity(array.dtype(), filter.iter().filter(|b| **b).count());
for (idx, keep) in filter.iter().enumerate() {
if *keep {
builder.append_scalar(&array.execute_scalar(idx, ctx)?)?;
}
}
Ok(builder.finish())
}
d @ (DType::Null | DType::Union(..) | DType::Variant(_) | DType::Extension(_)) => {
unreachable!("DType {d} not supported for fuzzing")
}
}
}