Skip to content
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

Add boolean comparison to scalar kernels for less then, greater than #977

Merged
merged 1 commit into from
Nov 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 77 additions & 4 deletions arrow/src/compute/kernels/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,26 @@ pub fn eq_bool_scalar(left: &BooleanArray, right: bool) -> Result<BooleanArray>
Ok(BooleanArray::from(data))
}

/// Perform `left < right` operation on [`BooleanArray`] and a scalar
pub fn lt_bool_scalar(left: &BooleanArray, right: bool) -> Result<BooleanArray> {
compare_op_scalar!(left, right, |a: bool, b: bool| !a & b)
}

/// Perform `left <= right` operation on [`BooleanArray`] and a scalar
pub fn lt_eq_bool_scalar(left: &BooleanArray, right: bool) -> Result<BooleanArray> {
compare_op_scalar!(left, right, |a, b| a <= b)
}

/// Perform `left > right` operation on [`BooleanArray`] and a scalar
pub fn gt_bool_scalar(left: &BooleanArray, right: bool) -> Result<BooleanArray> {
compare_op_scalar!(left, right, |a: bool, b: bool| a & !b)
}

/// Perform `left >= right` operation on [`BooleanArray`] and a scalar
pub fn gt_eq_bool_scalar(left: &BooleanArray, right: bool) -> Result<BooleanArray> {
compare_op_scalar!(left, right, |a, b| a >= b)
}

/// Perform `left != right` operation on [`BooleanArray`] and a scalar
pub fn neq_bool_scalar(left: &BooleanArray, right: bool) -> Result<BooleanArray> {
eq_bool_scalar(left, !right)
Expand Down Expand Up @@ -897,10 +917,7 @@ where
let mut result = MutableBuffer::new(buffer_size).with_bitset(buffer_size, false);

// this is currently the case for all our datatypes and allows us to always append full bytes
assert!(
lanes % 8 == 0,
"Number of vector lanes must be multiple of 8"
);
assert_eq!(lanes % 8, 0, "Number of vector lanes must be multiple of 8");
let mut left_chunks = left.values().chunks_exact(lanes);
let mut right_chunks = right.values().chunks_exact(lanes);

Expand Down Expand Up @@ -1687,6 +1704,62 @@ mod tests {
assert_eq!(res2, vec![Some(false), Some(true), None]);
}

#[test]
fn test_boolean_array_lt_scalar() {
let a: BooleanArray = vec![Some(true), Some(false), None].into();

let res1: Vec<Option<bool>> = lt_bool_scalar(&a, false).unwrap().iter().collect();

assert_eq!(res1, vec![Some(false), Some(false), None]);

let res2: Vec<Option<bool>> = lt_bool_scalar(&a, true).unwrap().iter().collect();

assert_eq!(res2, vec![Some(false), Some(true), None]);
}

#[test]
fn test_boolean_array_lt_eq_scalar() {
let a: BooleanArray = vec![Some(true), Some(false), None].into();

let res1: Vec<Option<bool>> =
lt_eq_bool_scalar(&a, false).unwrap().iter().collect();

assert_eq!(res1, vec![Some(false), Some(true), None]);

let res2: Vec<Option<bool>> =
lt_eq_bool_scalar(&a, true).unwrap().iter().collect();

assert_eq!(res2, vec![Some(true), Some(true), None]);
}

#[test]
fn test_boolean_array_gt_scalar() {
let a: BooleanArray = vec![Some(true), Some(false), None].into();

let res1: Vec<Option<bool>> = gt_bool_scalar(&a, false).unwrap().iter().collect();

assert_eq!(res1, vec![Some(true), Some(false), None]);

let res2: Vec<Option<bool>> = gt_bool_scalar(&a, true).unwrap().iter().collect();

assert_eq!(res2, vec![Some(false), Some(false), None]);
}

#[test]
fn test_boolean_array_gt_eq_scalar() {
let a: BooleanArray = vec![Some(true), Some(false), None].into();

let res1: Vec<Option<bool>> =
gt_eq_bool_scalar(&a, false).unwrap().iter().collect();

assert_eq!(res1, vec![Some(true), Some(true), None]);

let res2: Vec<Option<bool>> =
gt_eq_bool_scalar(&a, true).unwrap().iter().collect();

assert_eq!(res2, vec![Some(true), Some(false), None]);
}

#[test]
fn test_primitive_array_lt() {
cmp_i64!(
Expand Down