-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathtake.rs
More file actions
180 lines (169 loc) · 5.69 KB
/
Copy pathtake.rs
File metadata and controls
180 lines (169 loc) · 5.69 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use vortex_array::ArrayRef;
use vortex_array::DynArray;
use vortex_array::IntoArray;
use vortex_array::ToCanonical;
use vortex_array::accessor::ArrayAccessor;
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::builders::builder_with_capacity;
use vortex_array::dtype::DType;
use vortex_array::dtype::DecimalDType;
use vortex_array::dtype::NativeDecimalType;
use vortex_array::dtype::NativePType;
use vortex_array::dtype::Nullability;
use vortex_array::match_each_decimal_value_type;
use vortex_array::match_each_native_ptype;
use vortex_array::validity::Validity;
use vortex_buffer::Buffer;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
pub fn take_canonical_array_non_nullable_indices(
array: &ArrayRef,
indices: &[usize],
) -> VortexResult<ArrayRef> {
take_canonical_array(
array,
indices
.iter()
.map(|i| Some(*i))
.collect::<Vec<_>>()
.as_slice(),
)
}
pub fn take_canonical_array(array: &ArrayRef, indices: &[Option<usize>]) -> VortexResult<ArrayRef> {
let nullable: Nullability = indices.contains(&None).into();
let validity = if array.dtype().is_nullable() || nullable == Nullability::Nullable {
let validity_idx = array.validity_mask()?.to_bit_buffer();
Validity::from_iter(
indices
.iter()
.map(|i| i.is_some_and(|i| validity_idx.value(i))),
)
} else {
Validity::NonNullable
};
let indices_non_opt = indices.iter().map(|i| i.unwrap_or(0)).collect::<Vec<_>>();
let indices_slice_non_opt = indices_non_opt.as_slice();
match array.dtype() {
DType::Bool(_) => {
let bool_array = array.to_bool();
let vec_values = bool_array.to_bit_buffer().iter().collect::<Vec<_>>();
Ok(BoolArray::new(
indices_slice_non_opt
.iter()
.map(|i| vec_values[*i])
.collect(),
validity,
)
.into_array())
}
DType::Primitive(p, _) => {
let primitive_array = array.to_primitive();
match_each_native_ptype!(p, |P| {
Ok(take_primitive::<P>(
primitive_array,
validity,
indices_slice_non_opt,
))
})
}
DType::Decimal(d, _) => {
let decimal_array = array.to_decimal();
match_each_decimal_value_type!(decimal_array.values_type(), |D| {
Ok(take_decimal::<D>(
decimal_array,
d,
validity,
indices_slice_non_opt,
))
})
}
DType::Utf8(_) | DType::Binary(_) => {
let utf8 = array.to_varbinview();
let values =
utf8.with_iterator(|iter| iter.map(|v| v.map(|u| u.to_vec())).collect::<Vec<_>>());
Ok(VarBinViewArray::from_iter(
indices
.iter()
.map(|i| i.and_then(|idx| values[idx].clone())),
array.dtype().clone().union_nullability(nullable),
)
.into_array())
}
DType::Struct(..) => {
let struct_array = array.to_struct();
let taken_children = struct_array
.unmasked_fields()
.iter()
.map(|c| take_canonical_array_non_nullable_indices(c, indices_slice_non_opt))
.collect::<VortexResult<Vec<_>>>()?;
StructArray::try_new(
struct_array.names().clone(),
taken_children,
indices_slice_non_opt.len(),
validity,
)
.map(|a| a.into_array())
}
DType::List(..) | DType::FixedSizeList(..) => {
let mut builder = builder_with_capacity(
&array.dtype().union_nullability(nullable),
indices_slice_non_opt.len(),
);
for idx in indices {
if let Some(idx) = idx {
builder.append_scalar(
&array
.scalar_at(*idx)?
.cast(&array.dtype().union_nullability(nullable))
.vortex_expect("cannot cast scalar nullability"),
)?;
} else {
builder.append_null()
}
}
Ok(builder.finish())
}
d @ (DType::Null | DType::Extension(_) | DType::Variant(_)) => {
unreachable!("DType {d} not supported for fuzzing")
}
}
}
fn take_primitive<T: NativePType>(
primitive_array: PrimitiveArray,
validity: Validity,
indices: &[usize],
) -> ArrayRef {
let vec_values = primitive_array.as_slice::<T>().to_vec();
PrimitiveArray::new(
indices
.iter()
.map(|i| vec_values[*i])
.collect::<Buffer<T>>(),
validity,
)
.into_array()
}
fn take_decimal<D: NativeDecimalType>(
array: DecimalArray,
decimal_type: &DecimalDType,
validity: Validity,
indices: &[usize],
) -> ArrayRef {
let buf = array.buffer::<D>();
let vec_values = buf.as_slice();
DecimalArray::new(
indices
.iter()
.map(|i| vec_values[*i])
.collect::<Buffer<D>>(),
*decimal_type,
validity,
)
.into_array()
}