Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-ops/src/chunked_array/array/any_all.rs
6939 views
1
use arrow::array::{BooleanArray, FixedSizeListArray};
2
use arrow::bitmap::Bitmap;
3
use arrow::legacy::utils::CustomIterTools;
4
5
use super::*;
6
7
fn array_all_any<F>(arr: &FixedSizeListArray, op: F, is_all: bool) -> PolarsResult<BooleanArray>
8
where
9
F: Fn(&BooleanArray) -> bool,
10
{
11
let values = arr.values();
12
13
polars_ensure!(values.dtype() == &ArrowDataType::Boolean, ComputeError: "expected boolean elements in array");
14
15
let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();
16
let validity = arr.validity().cloned();
17
18
// Fast path where all values set (all is free).
19
if is_all {
20
let all_set = arrow::compute::boolean::all(values);
21
if all_set {
22
let bits = Bitmap::new_with_value(true, arr.len());
23
return Ok(BooleanArray::from_data_default(bits, None).with_validity(validity));
24
}
25
}
26
27
let len = arr.size();
28
let iter = (0..values.len()).step_by(len).map(|start| {
29
// SAFETY: start + len is in bound guarded by invariant of FixedSizeListArray
30
let val = unsafe { values.clone().sliced_unchecked(start, len) };
31
op(&val)
32
});
33
34
Ok(BooleanArray::from_trusted_len_values_iter(
35
// SAFETY: we evaluate for every sub-array, the length is equals to arr.len().
36
unsafe { iter.trust_my_length(arr.len()) },
37
)
38
.with_validity(validity))
39
}
40
41
pub(super) fn array_all(ca: &ArrayChunked) -> PolarsResult<Series> {
42
let chunks = ca
43
.downcast_iter()
44
.map(|arr| array_all_any(arr, arrow::compute::boolean::all, true));
45
Ok(BooleanChunked::try_from_chunk_iter(ca.name().clone(), chunks)?.into_series())
46
}
47
48
pub(super) fn array_any(ca: &ArrayChunked) -> PolarsResult<Series> {
49
let chunks = ca
50
.downcast_iter()
51
.map(|arr| array_all_any(arr, arrow::compute::boolean::any, false));
52
Ok(BooleanChunked::try_from_chunk_iter(ca.name().clone(), chunks)?.into_series())
53
}
54
55