Skip to content

Commit ea7d119

Browse files
authored
Add arrow::compute::take_arrays
Adds a modified version of the take_arrays function from upstream arrow-rs.
1 parent 1963799 commit ea7d119

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

arrow/src/compute/kernels/take.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,74 @@ where
286286
}
287287
}
288288

289+
// From upstream arrow-rs. But upstream had `indices: &dyn Array` -- we have the generic parameter
290+
// IndexType and use `indices: &PrimitiveArray<IndexType>` in order to work with the existing `take`
291+
// function.
292+
/// For each [ArrayRef] in the [`Vec<ArrayRef>`], take elements by index and create a new
293+
/// [`Vec<ArrayRef>`] from those indices.
294+
///
295+
/// ```text
296+
/// ┌────────┬────────┐
297+
/// │ │ │ ┌────────┐ ┌────────┬────────┐
298+
/// │ A │ 1 │ │ │ │ │ │
299+
/// ├────────┼────────┤ │ 0 │ │ A │ 1 │
300+
/// │ │ │ ├────────┤ ├────────┼────────┤
301+
/// │ D │ 4 │ │ │ │ │ │
302+
/// ├────────┼────────┤ │ 2 │ take_arrays(values,indices) │ B │ 2 │
303+
/// │ │ │ ├────────┤ ├────────┼────────┤
304+
/// │ B │ 2 │ │ │ ───────────────────────────► │ │ │
305+
/// ├────────┼────────┤ │ 3 │ │ C │ 3 │
306+
/// │ │ │ ├────────┤ ├────────┼────────┤
307+
/// │ C │ 3 │ │ │ │ │ │
308+
/// ├────────┼────────┤ │ 1 │ │ D │ 4 │
309+
/// │ │ │ └────────┘ └────────┼────────┘
310+
/// │ E │ 5 │
311+
/// └────────┴────────┘
312+
/// values arrays indices array result
313+
/// ```
314+
///
315+
/// # Errors
316+
/// This function errors whenever:
317+
/// * An index cannot be casted to `usize` (typically 32 bit architectures)
318+
/// * An index is out of bounds and `options` is set to check bounds.
319+
///
320+
/// # Safety
321+
///
322+
/// When `options` is not set to check bounds, taking indexes after `len` will panic.
323+
///
324+
/// # Examples
325+
/// ```
326+
/// /* Commented out so doc-comment compilation checks pass (as this uses upstream stuff like cast::AsArray).
327+
/// # use std::sync::Arc;
328+
/// # use arrow_array::{StringArray, UInt32Array, cast::AsArray};
329+
/// # use arrow_select::take::{take, take_arrays};
330+
/// let string_values = Arc::new(StringArray::from(vec!["zero", "one", "two"]));
331+
/// let values = Arc::new(UInt32Array::from(vec![0, 1, 2]));
332+
///
333+
/// // Take items at index 2, and 1:
334+
/// let indices = UInt32Array::from(vec![2, 1]);
335+
/// let taken_arrays = take_arrays(&[string_values, values], &indices, None).unwrap();
336+
/// let taken_string = taken_arrays[0].as_string::<i32>();
337+
/// assert_eq!(*taken_string, StringArray::from(vec!["two", "one"]));
338+
/// let taken_values = taken_arrays[1].as_primitive();
339+
/// assert_eq!(*taken_values, UInt32Array::from(vec![2, 1]));
340+
/// */
341+
/// ```
342+
pub fn take_arrays<IndexType>(
343+
arrays: &[ArrayRef],
344+
indices: &PrimitiveArray<IndexType>,
345+
options: Option<TakeOptions>,
346+
) -> std::result::Result<Vec<ArrayRef>, ArrowError>
347+
where
348+
IndexType: ArrowNumericType,
349+
IndexType::Native: num::ToPrimitive,
350+
{
351+
arrays
352+
.iter()
353+
.map(|array| take(array.as_ref(), indices, options.clone()))
354+
.collect()
355+
}
356+
289357
/// Options that define how `take` should behave
290358
#[derive(Clone, Debug)]
291359
pub struct TakeOptions {

0 commit comments

Comments
 (0)