Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Implementd any and all kernel (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Jan 8, 2022
1 parent abc6257 commit 1c78fe9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/compute/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,25 @@ pub fn or_scalar(array: &BooleanArray, scalar: &BooleanScalar) -> BooleanArray {
None => BooleanArray::new_null(DataType::Boolean, array.len()),
}
}

/// Check if any of the values in the array is `true`
pub fn any(array: &BooleanArray) -> bool {
if array.is_empty() {
false
} else if array.validity().is_some() {
array.into_iter().any(|v| v == Some(true))
} else {
let vals = array.values();
vals.null_count() != 0
}
}

/// Check if all of the values in the array are `true`
pub fn all(array: &BooleanArray) -> bool {
if array.is_empty() || array.null_count() > 0 {
false
} else {
let vals = array.values();
vals.null_count() == 0
}
}
23 changes: 23 additions & 0 deletions tests/it/compute/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use arrow2::array::*;
use arrow2::compute::boolean::*;
use arrow2::scalar::BooleanScalar;
use std::iter::FromIterator;

#[test]
fn array_and() {
Expand Down Expand Up @@ -418,3 +419,25 @@ fn array_or_scalar_validity() {
let expected = BooleanArray::from(&[None; 3]);
assert_eq!(real, expected);
}

#[test]
fn test_any_all() {
let array = BooleanArray::from(&[None, Some(false), Some(true)]);
assert!(any(&array));
assert!(!all(&array));
let array = BooleanArray::from(&[None, Some(false), Some(false)]);
assert!(!any(&array));
assert!(!all(&array));
let array = BooleanArray::from(&[None, Some(true), Some(true)]);
assert!(!all(&array));
assert!(any(&array));
let array = BooleanArray::from_iter(std::iter::repeat(false).take(10).map(Some));
assert!(!any(&array));
assert!(!all(&array));
let array = BooleanArray::from_iter(std::iter::repeat(true).take(10).map(Some));
assert!(all(&array));
assert!(any(&array));
let array = BooleanArray::from_iter([true, false, true, true].map(Some));
assert!(!all(&array));
assert!(any(&array));
}

0 comments on commit 1c78fe9

Please sign in to comment.