-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add take_arrays util for getting entries from 2d arrays #6475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,6 +94,65 @@ pub fn take( | |
| } | ||
| } | ||
|
|
||
| /// For each [ArrayRef] in the [`Vec<ArrayRef>`], take elements by index and create a new | ||
| /// [`Vec<ArrayRef>`] from those indices. | ||
| /// | ||
| /// ```text | ||
| /// ┌────────┬────────┐ | ||
| /// │ │ │ ┌────────┐ ┌────────┬────────┐ | ||
| /// │ A │ 1 │ │ │ │ │ │ | ||
| /// ├────────┼────────┤ │ 0 │ │ A │ 1 │ | ||
| /// │ │ │ ├────────┤ ├────────┼────────┤ | ||
| /// │ D │ 4 │ │ │ │ │ │ | ||
| /// ├────────┼────────┤ │ 2 │ take_arrays(values,indices) │ B │ 2 │ | ||
| /// │ │ │ ├────────┤ ├────────┼────────┤ | ||
| /// │ B │ 2 │ │ │ ───────────────────────────► │ │ │ | ||
| /// ├────────┼────────┤ │ 3 │ │ C │ 3 │ | ||
| /// │ │ │ ├────────┤ ├────────┼────────┤ | ||
| /// │ C │ 3 │ │ │ │ │ │ | ||
| /// ├────────┼────────┤ │ 1 │ │ D │ 4 │ | ||
| /// │ │ │ └────────┘ └────────┼────────┘ | ||
| /// │ E │ 5 │ | ||
| /// └────────┴────────┘ | ||
| /// values arrays indices array result | ||
| /// ``` | ||
| /// | ||
| /// # Errors | ||
| /// This function errors whenever: | ||
| /// * An index cannot be casted to `usize` (typically 32 bit architectures) | ||
| /// * An index is out of bounds and `options` is set to check bounds. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// When `options` is not set to check bounds, taking indexes after `len` will panic. | ||
| /// | ||
| /// # Examples | ||
| /// ``` | ||
| /// # use std::sync::Arc; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 I think this example is good and also serves the case of test coverage. ❤️ |
||
| /// # use arrow_array::{StringArray, UInt32Array, cast::AsArray}; | ||
| /// # use arrow_select::take::{take, take_arrays}; | ||
| /// let string_values = Arc::new(StringArray::from(vec!["zero", "one", "two"])); | ||
| /// let values = Arc::new(UInt32Array::from(vec![0, 1, 2])); | ||
| /// | ||
| /// // Take items at index 2, and 1: | ||
| /// let indices = UInt32Array::from(vec![2, 1]); | ||
| /// let taken_arrays = take_arrays(&[string_values, values], &indices, None).unwrap(); | ||
| /// let taken_string = taken_arrays[0].as_string::<i32>(); | ||
| /// assert_eq!(*taken_string, StringArray::from(vec!["two", "one"])); | ||
| /// let taken_values = taken_arrays[1].as_primitive(); | ||
| /// assert_eq!(*taken_values, UInt32Array::from(vec![2, 1])); | ||
| /// ``` | ||
| pub fn take_arrays( | ||
| arrays: &[ArrayRef], | ||
| indices: &dyn Array, | ||
| options: Option<TakeOptions>, | ||
| ) -> Result<Vec<ArrayRef>, ArrowError> { | ||
| arrays | ||
| .iter() | ||
| .map(|array| take(array.as_ref(), indices, options.clone())) | ||
| .collect() | ||
| } | ||
|
|
||
| /// Verifies that the non-null values of `indices` are all `< len` | ||
| fn check_bounds<T: ArrowPrimitiveType>( | ||
| len: usize, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️