-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy pathdictionary.rs
More file actions
307 lines (279 loc) · 11.4 KB
/
Copy pathdictionary.rs
File metadata and controls
307 lines (279 loc) · 11.4 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
295
296
297
298
299
300
301
302
303
304
305
306
307
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use std::sync::Arc;
use arrow_array::ArrayRef as ArrowArrayRef;
use arrow_array::DictionaryArray;
use arrow_array::PrimitiveArray;
use arrow_array::cast::AsArray;
use arrow_array::new_empty_array;
use arrow_array::new_null_array;
use arrow_array::types::*;
use arrow_schema::DataType;
use vortex_array::ArrayRef;
use vortex_array::ExecutionCtx;
use vortex_array::IntoArray;
use vortex_array::arrays::Chunked;
use vortex_array::arrays::ChunkedArray;
use vortex_array::arrays::Constant;
use vortex_array::arrays::ConstantArray;
use vortex_array::arrays::Dict;
use vortex_array::arrays::DictArray;
use vortex_array::arrays::chunked::ChunkedArrayExt;
use vortex_array::arrays::dict::DictArraySlotsExt;
use vortex_array::matcher::Matcher;
use vortex_error::VortexError;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use crate::ArrowArrayExecutor;
/// Matches the encodings [`to_arrow_dictionary`] requires for export.
struct ArrowDictExportable;
impl Matcher for ArrowDictExportable {
type Match<'a> = &'a ArrayRef;
fn try_match(array: &ArrayRef) -> Option<Self::Match<'_>> {
(array.is::<Dict>() || array.is::<Chunked>() || array.is::<Constant>()).then_some(array)
}
}
pub(super) fn to_arrow_dictionary(
array: ArrayRef,
codes_type: &DataType,
values_type: &DataType,
ctx: &mut ExecutionCtx,
) -> VortexResult<ArrowArrayRef> {
let array = array.execute_until::<ArrowDictExportable>(ctx)?;
let array = match array.try_downcast::<Dict>() {
Ok(dict) => return dict_to_dict(dict, codes_type, values_type, ctx),
Err(array) => array,
};
let array = match array.try_downcast::<Constant>() {
Ok(constant) => return constant_to_dict(constant, codes_type, values_type, ctx),
Err(array) => array,
};
let array = match array.try_downcast::<Chunked>() {
Ok(chunked) => return chunked_to_dict(chunked, codes_type, values_type, ctx),
Err(array) => array,
};
// Otherwise, we should try and build a dictionary.
// Arrow hides this functionality inside the cast module!
let array = array.execute_arrow(Some(values_type), ctx)?;
arrow_cast::cast(
&array,
&DataType::Dictionary(Box::new(codes_type.clone()), Box::new(values_type.clone())),
)
.map_err(VortexError::from)
}
/// Convert a constant array to a dictionary with a single entry.
fn constant_to_dict(
array: ConstantArray,
codes_type: &DataType,
values_type: &DataType,
ctx: &mut ExecutionCtx,
) -> VortexResult<ArrowArrayRef> {
let len = array.len();
let scalar = array.scalar();
if scalar.is_null() {
let dict_type =
DataType::Dictionary(Box::new(codes_type.clone()), Box::new(values_type.clone()));
return Ok(new_null_array(&dict_type, len));
}
let values = ConstantArray::new(scalar.clone(), 1)
.into_array()
.execute_arrow(Some(values_type), ctx)?;
let codes = zeroed_codes_array(codes_type, len)?;
make_dict_array(codes_type, codes, values)
}
/// Convert a chunked array to an Arrow dictionary array by exporting each chunk separately.
fn chunked_to_dict(
array: ChunkedArray,
codes_type: &DataType,
values_type: &DataType,
ctx: &mut ExecutionCtx,
) -> VortexResult<ArrowArrayRef> {
let mut arrow_chunks = Vec::with_capacity(array.nchunks());
for chunk in array.non_empty_chunks() {
arrow_chunks.push(to_arrow_dictionary(
chunk.clone(),
codes_type,
values_type,
ctx,
)?);
}
if arrow_chunks.is_empty() {
return Ok(new_empty_array(&DataType::Dictionary(
Box::new(codes_type.clone()),
Box::new(values_type.clone()),
)));
}
if let [arrow_chunk] = arrow_chunks.as_slice() {
return Ok(Arc::clone(arrow_chunk));
}
let refs = arrow_chunks
.iter()
.map(|array| array.as_ref())
.collect::<Vec<_>>();
Ok(arrow_select::concat::concat(&refs)?)
}
/// Convert a Vortex dictionary array to an Arrow dictionary array.
fn dict_to_dict(
array: DictArray,
codes_type: &DataType,
values_type: &DataType,
ctx: &mut ExecutionCtx,
) -> VortexResult<ArrowArrayRef> {
let codes = array.codes().clone().execute_arrow(Some(codes_type), ctx)?;
let values = array
.values()
.clone()
.execute_arrow(Some(values_type), ctx)?;
make_dict_array(codes_type, codes, values)
}
/// Construct a zeroed Arrow primitive array directly.
fn zeroed_codes_array(codes_type: &DataType, len: usize) -> VortexResult<ArrowArrayRef> {
Ok(match codes_type {
DataType::Int8 => Arc::new(PrimitiveArray::<Int8Type>::from_value(0, len)),
DataType::Int16 => Arc::new(PrimitiveArray::<Int16Type>::from_value(0, len)),
DataType::Int32 => Arc::new(PrimitiveArray::<Int32Type>::from_value(0, len)),
DataType::Int64 => Arc::new(PrimitiveArray::<Int64Type>::from_value(0, len)),
DataType::UInt8 => Arc::new(PrimitiveArray::<UInt8Type>::from_value(0, len)),
DataType::UInt16 => Arc::new(PrimitiveArray::<UInt16Type>::from_value(0, len)),
DataType::UInt32 => Arc::new(PrimitiveArray::<UInt32Type>::from_value(0, len)),
DataType::UInt64 => Arc::new(PrimitiveArray::<UInt64Type>::from_value(0, len)),
_ => vortex_bail!("Unsupported dictionary codes type: {:?}", codes_type),
})
}
/// Construct an Arrow `DictionaryArray` from pre-built codes and values arrays.
fn make_dict_array(
codes_type: &DataType,
codes: ArrowArrayRef,
values: ArrowArrayRef,
) -> VortexResult<ArrowArrayRef> {
Ok(match codes_type {
DataType::Int8 => Arc::new(unsafe {
DictionaryArray::new_unchecked(codes.as_primitive::<Int8Type>().clone(), values)
}),
DataType::Int16 => Arc::new(unsafe {
DictionaryArray::new_unchecked(codes.as_primitive::<Int16Type>().clone(), values)
}),
DataType::Int32 => Arc::new(unsafe {
DictionaryArray::new_unchecked(codes.as_primitive::<Int32Type>().clone(), values)
}),
DataType::Int64 => Arc::new(unsafe {
DictionaryArray::new_unchecked(codes.as_primitive::<Int64Type>().clone(), values)
}),
DataType::UInt8 => Arc::new(unsafe {
DictionaryArray::new_unchecked(codes.as_primitive::<UInt8Type>().clone(), values)
}),
DataType::UInt16 => Arc::new(unsafe {
DictionaryArray::new_unchecked(codes.as_primitive::<UInt16Type>().clone(), values)
}),
DataType::UInt32 => Arc::new(unsafe {
DictionaryArray::new_unchecked(codes.as_primitive::<UInt32Type>().clone(), values)
}),
DataType::UInt64 => Arc::new(unsafe {
DictionaryArray::new_unchecked(codes.as_primitive::<UInt64Type>().clone(), values)
}),
_ => vortex_bail!("Unsupported dictionary codes type: {:?}", codes_type),
})
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use arrow_array::DictionaryArray as ArrowDictArray;
use arrow_array::StringArray;
use arrow_array::types::UInt8Type;
use arrow_array::types::UInt32Type;
use arrow_schema::DataType;
use rstest::rstest;
use vortex_array::IntoArray;
use vortex_array::VortexSessionExecute;
use vortex_array::array_session;
use vortex_array::arrays::BoolArray;
use vortex_array::arrays::PrimitiveArray;
use vortex_array::arrays::VarBinViewArray;
use vortex_array::arrays::scalar_fn::ScalarFnFactoryExt;
use vortex_array::dtype::DType;
use vortex_array::dtype::Nullability::Nullable;
use vortex_array::scalar::Scalar;
use vortex_array::scalar_fn::EmptyOptions;
use vortex_array::scalar_fn::fns::mask::Mask;
use vortex_buffer::buffer;
use vortex_error::VortexResult;
use crate::ArrowArrayExecutor;
use crate::executor::dictionary::ConstantArray;
use crate::executor::dictionary::DictArray;
fn dict_type(codes: DataType, values: DataType) -> DataType {
DataType::Dictionary(Box::new(codes), Box::new(values))
}
fn execute(
array: vortex_array::ArrayRef,
dt: &DataType,
) -> VortexResult<arrow_array::ArrayRef> {
array.execute_arrow(Some(dt), &mut array_session().create_execution_ctx())
}
fn dict_basic_input() -> vortex_array::ArrayRef {
DictArray::try_new(
buffer![0u8, 1, 0].into_array(),
VarBinViewArray::from_iter_str(["a", "b"]).into_array(),
)
.expect("valid dictionary input")
.into_array()
}
fn dict_with_null_codes_input() -> vortex_array::ArrayRef {
DictArray::try_new(
PrimitiveArray::from_option_iter(vec![Some(0u8), None, Some(1)]).into_array(),
VarBinViewArray::from_iter_str(["a", "b"]).into_array(),
)
.expect("valid dictionary input with null codes")
.into_array()
}
#[rstest]
#[case::constant_null(
ConstantArray::new(Scalar::null(DType::Utf8(Nullable)), 4).into_array(),
dict_type(DataType::UInt32, DataType::Utf8),
Arc::new(vec![None::<&str>, None, None, None].into_iter().collect::<ArrowDictArray<UInt32Type>>()) as arrow_array::ArrayRef,
)]
#[case::constant_non_null(
ConstantArray::new(Scalar::from("hello"), 5).into_array(),
dict_type(DataType::UInt32, DataType::Utf8),
Arc::new(vec![Some("hello"); 5].into_iter().collect::<ArrowDictArray<UInt32Type>>()) as arrow_array::ArrayRef,
)]
#[case::dict_basic(
dict_basic_input(),
dict_type(DataType::UInt8, DataType::Utf8),
Arc::new(vec![Some("a"), Some("b"), Some("a")].into_iter().collect::<ArrowDictArray<UInt8Type>>()) as arrow_array::ArrayRef,
)]
#[case::dict_with_null_codes(
dict_with_null_codes_input(),
dict_type(DataType::UInt8, DataType::Utf8),
Arc::new(vec![Some("a"), None, Some("b")].into_iter().collect::<ArrowDictArray<UInt8Type>>()) as arrow_array::ArrayRef,
)]
#[case::varbinview_fallback(
[Some("a"), None, Some("a"), Some("b"), Some("a")].into_iter().collect::<VarBinViewArray>().into_array(),
dict_type(DataType::UInt8, DataType::Utf8),
Arc::new(vec![Some("a"), None, Some("a"), Some("b"), Some("a")].into_iter().collect::<ArrowDictArray<UInt8Type>>()) as arrow_array::ArrayRef,
)]
fn to_arrow_dictionary(
#[case] input: vortex_array::ArrayRef,
#[case] target_type: DataType,
#[case] expected: arrow_array::ArrayRef,
) -> VortexResult<()> {
let actual = execute(input, &target_type)?;
assert_eq!(expected.as_ref(), actual.as_ref());
Ok(())
}
#[test]
fn mask_wrapped_dict_exports() -> VortexResult<()> {
// Dictionary behind a lazy `mask` scalar-fn — the shape a scan produces when a row
// mask is applied to a dict-encoded column.
let dict = DictArray::try_new(
buffer![0u8, 1, 0].into_array(),
VarBinViewArray::from_iter_str(["a", "b"]).into_array(),
)?;
let mask = BoolArray::from_iter([true, false, true]);
let masked = Mask.try_new_array(3, EmptyOptions, [dict.into_array(), mask.into_array()])?;
let actual = execute(masked, &dict_type(DataType::UInt8, DataType::Utf8))?;
let flat = arrow_cast::cast(&actual, &DataType::Utf8)?;
let expected = StringArray::from(vec![Some("a"), None, Some("a")]);
assert_eq!(flat.as_ref(), &expected as &dyn arrow_array::Array);
Ok(())
}
}