Path: blob/main/crates/polars-ops/src/chunked_array/array/any_all.rs
6939 views
use arrow::array::{BooleanArray, FixedSizeListArray};1use arrow::bitmap::Bitmap;2use arrow::legacy::utils::CustomIterTools;34use super::*;56fn array_all_any<F>(arr: &FixedSizeListArray, op: F, is_all: bool) -> PolarsResult<BooleanArray>7where8F: Fn(&BooleanArray) -> bool,9{10let values = arr.values();1112polars_ensure!(values.dtype() == &ArrowDataType::Boolean, ComputeError: "expected boolean elements in array");1314let values = values.as_any().downcast_ref::<BooleanArray>().unwrap();15let validity = arr.validity().cloned();1617// Fast path where all values set (all is free).18if is_all {19let all_set = arrow::compute::boolean::all(values);20if all_set {21let bits = Bitmap::new_with_value(true, arr.len());22return Ok(BooleanArray::from_data_default(bits, None).with_validity(validity));23}24}2526let len = arr.size();27let iter = (0..values.len()).step_by(len).map(|start| {28// SAFETY: start + len is in bound guarded by invariant of FixedSizeListArray29let val = unsafe { values.clone().sliced_unchecked(start, len) };30op(&val)31});3233Ok(BooleanArray::from_trusted_len_values_iter(34// SAFETY: we evaluate for every sub-array, the length is equals to arr.len().35unsafe { iter.trust_my_length(arr.len()) },36)37.with_validity(validity))38}3940pub(super) fn array_all(ca: &ArrayChunked) -> PolarsResult<Series> {41let chunks = ca42.downcast_iter()43.map(|arr| array_all_any(arr, arrow::compute::boolean::all, true));44Ok(BooleanChunked::try_from_chunk_iter(ca.name().clone(), chunks)?.into_series())45}4647pub(super) fn array_any(ca: &ArrayChunked) -> PolarsResult<Series> {48let chunks = ca49.downcast_iter()50.map(|arr| array_all_any(arr, arrow::compute::boolean::any, false));51Ok(BooleanChunked::try_from_chunk_iter(ca.name().clone(), chunks)?.into_series())52}535455