Skip to content
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
127 changes: 105 additions & 22 deletions arrow/src/compute/kernels/comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,15 @@ pub fn eq_utf8_scalar<OffsetSize: StringOffsetSizeTrait>(
compare_op_scalar!(left, right, |a, b| a == b)
}

/// Perform `left == right` operation on [`BooleanArray`]
fn eq_bool(left: &BooleanArray, right: &BooleanArray) -> Result<BooleanArray> {
#[inline]
fn binary_boolean_op<F>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth a doc comment that says that op is performed 64 bits at a time withu64 (aka so it is not possible to use a < b for example, to implement lt_bool)

left: &BooleanArray,
right: &BooleanArray,
op: F,
) -> Result<BooleanArray>
where
F: Copy + Fn(u64, u64) -> u64,
{
binary_boolean_kernel(
left,
right,
Expand All @@ -640,32 +647,40 @@ fn eq_bool(left: &BooleanArray, right: &BooleanArray) -> Result<BooleanArray> {
right,
right_offset_in_bits,
len_in_bits,
|a, b| !(a ^ b),
op,
)
},
)
}

/// Perform `left == right` operation on [`BooleanArray`]
fn eq_bool(left: &BooleanArray, right: &BooleanArray) -> Result<BooleanArray> {
binary_boolean_op(left, right, |a, b| !(a ^ b))
}

/// Perform `left != right` operation on [`BooleanArray`]
fn neq_bool(left: &BooleanArray, right: &BooleanArray) -> Result<BooleanArray> {
binary_boolean_kernel(
left,
right,
|left: &Buffer,
left_offset_in_bits: usize,
right: &Buffer,
right_offset_in_bits: usize,
len_in_bits: usize| {
bitwise_bin_op_helper(
left,
left_offset_in_bits,
right,
right_offset_in_bits,
len_in_bits,
|a, b| (a ^ b),
)
},
)
binary_boolean_op(left, right, |a, b| (a ^ b))
}

/// Perform `left < right` operation on [`BooleanArray`]
fn lt_bool(left: &BooleanArray, right: &BooleanArray) -> Result<BooleanArray> {
binary_boolean_op(left, right, |a, b| ((!a) & b))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

the truth table shows it to be true:

a b a < b
t f f
t f f
f t t
f t f

I checked the others as well

}

/// Perform `left <= right` operation on [`BooleanArray`]
fn lt_eq_bool(left: &BooleanArray, right: &BooleanArray) -> Result<BooleanArray> {
binary_boolean_op(left, right, |a, b| !(a & (!b)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also be done with two operations rather than three via (!a | b) but I suspect the compiler will do that transformation if it is useful

}

/// Perform `left > right` operation on [`BooleanArray`]
fn gt_bool(left: &BooleanArray, right: &BooleanArray) -> Result<BooleanArray> {
binary_boolean_op(left, right, |a, b| (a & (!b)))
}

/// Perform `left >= right` operation on [`BooleanArray`]
fn gt_eq_bool(left: &BooleanArray, right: &BooleanArray) -> Result<BooleanArray> {
binary_boolean_op(left, right, |a, b| !((!a) & b))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likewise could also be written like (a | !b)

}

/// Perform `left == right` operation on [`BooleanArray`] and a scalar
Expand Down Expand Up @@ -1354,7 +1369,7 @@ mod tests {
vec![Some(true), Some(false), Some(false), Some(true), Some(true), None]
.into();
let b: BooleanArray =
vec![Some(true), Some(true), Some(false), Some(false), None, Some(false)]
vec![Some(true), Some(true), Some(false), Some(false), None, Some(false)]
.into();

let res: Vec<Option<bool>> = neq_bool(&a, &b).unwrap().iter().collect();
Expand All @@ -1365,6 +1380,74 @@ mod tests {
)
}

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

let res: Vec<Option<bool>> = lt_bool(&a, &b).unwrap().iter().collect();

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

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

let res: Vec<Option<bool>> = lt_eq_bool(&a, &b).unwrap().iter().collect();

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

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

let res: Vec<Option<bool>> = gt_bool(&a, &b).unwrap().iter().collect();

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

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

let res: Vec<Option<bool>> = gt_eq_bool(&a, &b).unwrap().iter().collect();

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

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