Skip to content

Commit f0e39cc

Browse files
authored
Add take_arrays util for getting entries from 2d arrays (#6475)
* Add take_arrays util function * Update comments * Minor changes
1 parent f41c258 commit f0e39cc

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

arrow-select/src/take.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,65 @@ pub fn take(
9494
}
9595
}
9696

97+
/// For each [ArrayRef] in the [`Vec<ArrayRef>`], take elements by index and create a new
98+
/// [`Vec<ArrayRef>`] from those indices.
99+
///
100+
/// ```text
101+
/// ┌────────┬────────┐
102+
/// │ │ │ ┌────────┐ ┌────────┬────────┐
103+
/// │ A │ 1 │ │ │ │ │ │
104+
/// ├────────┼────────┤ │ 0 │ │ A │ 1 │
105+
/// │ │ │ ├────────┤ ├────────┼────────┤
106+
/// │ D │ 4 │ │ │ │ │ │
107+
/// ├────────┼────────┤ │ 2 │ take_arrays(values,indices) │ B │ 2 │
108+
/// │ │ │ ├────────┤ ├────────┼────────┤
109+
/// │ B │ 2 │ │ │ ───────────────────────────► │ │ │
110+
/// ├────────┼────────┤ │ 3 │ │ C │ 3 │
111+
/// │ │ │ ├────────┤ ├────────┼────────┤
112+
/// │ C │ 3 │ │ │ │ │ │
113+
/// ├────────┼────────┤ │ 1 │ │ D │ 4 │
114+
/// │ │ │ └────────┘ └────────┼────────┘
115+
/// │ E │ 5 │
116+
/// └────────┴────────┘
117+
/// values arrays indices array result
118+
/// ```
119+
///
120+
/// # Errors
121+
/// This function errors whenever:
122+
/// * An index cannot be casted to `usize` (typically 32 bit architectures)
123+
/// * An index is out of bounds and `options` is set to check bounds.
124+
///
125+
/// # Safety
126+
///
127+
/// When `options` is not set to check bounds, taking indexes after `len` will panic.
128+
///
129+
/// # Examples
130+
/// ```
131+
/// # use std::sync::Arc;
132+
/// # use arrow_array::{StringArray, UInt32Array, cast::AsArray};
133+
/// # use arrow_select::take::{take, take_arrays};
134+
/// let string_values = Arc::new(StringArray::from(vec!["zero", "one", "two"]));
135+
/// let values = Arc::new(UInt32Array::from(vec![0, 1, 2]));
136+
///
137+
/// // Take items at index 2, and 1:
138+
/// let indices = UInt32Array::from(vec![2, 1]);
139+
/// let taken_arrays = take_arrays(&[string_values, values], &indices, None).unwrap();
140+
/// let taken_string = taken_arrays[0].as_string::<i32>();
141+
/// assert_eq!(*taken_string, StringArray::from(vec!["two", "one"]));
142+
/// let taken_values = taken_arrays[1].as_primitive();
143+
/// assert_eq!(*taken_values, UInt32Array::from(vec![2, 1]));
144+
/// ```
145+
pub fn take_arrays(
146+
arrays: &[ArrayRef],
147+
indices: &dyn Array,
148+
options: Option<TakeOptions>,
149+
) -> Result<Vec<ArrayRef>, ArrowError> {
150+
arrays
151+
.iter()
152+
.map(|array| take(array.as_ref(), indices, options.clone()))
153+
.collect()
154+
}
155+
97156
/// Verifies that the non-null values of `indices` are all `< len`
98157
fn check_bounds<T: ArrowPrimitiveType>(
99158
len: usize,

0 commit comments

Comments
 (0)