-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust, python): Impl any/all for array type (#13250)
- Loading branch information
Showing
17 changed files
with
281 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use arrow::array::{BooleanArray, FixedSizeListArray}; | ||
use arrow::bitmap::MutableBitmap; | ||
use arrow::legacy::utils::CustomIterTools; | ||
|
||
use super::*; | ||
|
||
fn array_all_any<F>(arr: &FixedSizeListArray, op: F, is_all: bool) -> PolarsResult<BooleanArray> | ||
where | ||
F: Fn(&BooleanArray) -> bool, | ||
{ | ||
let values = arr.values(); | ||
|
||
polars_ensure!(values.data_type() == &ArrowDataType::Boolean, ComputeError: "expected boolean elements in array"); | ||
|
||
let values = values.as_any().downcast_ref::<BooleanArray>().unwrap(); | ||
let validity = arr.validity().cloned(); | ||
|
||
// Fast path where all values set (all is free). | ||
if is_all { | ||
let all_set = arrow::compute::boolean::all(values); | ||
if all_set { | ||
let mut bits = MutableBitmap::with_capacity(arr.len()); | ||
bits.extend_constant(arr.len(), true); | ||
return Ok(BooleanArray::from_data_default(bits.into(), None).with_validity(validity)); | ||
} | ||
} | ||
|
||
let len = arr.size(); | ||
let iter = (0..values.len()).step_by(len).map(|start| { | ||
// SAFETY: start + len is in bound guarded by invariant of FixedSizeListArray | ||
let val = unsafe { values.clone().sliced_unchecked(start, len) }; | ||
op(&val) | ||
}); | ||
|
||
Ok(BooleanArray::from_trusted_len_values_iter( | ||
// SAFETY: we evaluate for every sub-array, the length is equals to arr.len(). | ||
unsafe { iter.trust_my_length(arr.len()) }, | ||
) | ||
.with_validity(validity)) | ||
} | ||
|
||
pub(super) fn array_all(ca: &ArrayChunked) -> PolarsResult<Series> { | ||
let chunks = ca | ||
.downcast_iter() | ||
.map(|arr| array_all_any(arr, arrow::compute::boolean::all, true)); | ||
Ok(BooleanChunked::try_from_chunk_iter(ca.name(), chunks)?.into_series()) | ||
} | ||
|
||
pub(super) fn array_any(ca: &ArrayChunked) -> PolarsResult<Series> { | ||
let chunks = ca | ||
.downcast_iter() | ||
.map(|arr| array_all_any(arr, arrow::compute::boolean::any, false)); | ||
Ok(BooleanChunked::try_from_chunk_iter(ca.name(), chunks)?.into_series()) | ||
} |
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#[cfg(feature = "array_any_all")] | ||
mod any_all; | ||
mod min_max; | ||
mod namespace; | ||
mod sum_mean; | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.