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

Speedup scalar boolean operations #546

Merged
merged 8 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion benches/comparison_kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn add_benchmark(c: &mut Criterion) {
b.iter(|| bench_op(&arr_a, &arr_b, Operator::Eq))
});
c.bench_function(&format!("bool scalar 2^{}", log2_size), |b| {
b.iter(|| bench_op_scalar(&arr_a, &BooleanScalar::from(Some(true)), Operator::Eq))
b.iter(|| bench_op_scalar(&arr_a, &BooleanScalar::from(Some(false)), Operator::Eq))
Copy link
Collaborator Author

@Dandandan Dandandan Oct 22, 2021

Choose a reason for hiding this comment

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

Otherwise the benchmark measures lhs.clone() which doesn't increase with size - and is way faster (>20000x)

});

let arr_a = create_string_array::<i32>(size, 4, 0.1, 42);
Expand Down
36 changes: 30 additions & 6 deletions src/compute/comparison/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ pub fn eq(lhs: &BooleanArray, rhs: &BooleanArray) -> Result<BooleanArray> {

/// Perform `left == right` operation on an array and a scalar value.
pub fn eq_scalar(lhs: &BooleanArray, rhs: bool) -> BooleanArray {
compare_op_scalar(lhs, rhs, |a, b| !(a ^ b))
if rhs {
lhs.clone()
} else {
compare_op_scalar(lhs, rhs, |a, _| !a)
}
}

/// Perform `left != right` operation on two arrays.
Expand All @@ -92,7 +96,7 @@ pub fn neq(lhs: &BooleanArray, rhs: &BooleanArray) -> Result<BooleanArray> {

/// Perform `left != right` operation on an array and a scalar value.
pub fn neq_scalar(lhs: &BooleanArray, rhs: bool) -> BooleanArray {
compare_op_scalar(lhs, rhs, |a, b| a ^ b)
eq_scalar(lhs, !rhs)
}

/// Perform `left < right` operation on two arrays.
Expand All @@ -102,7 +106,11 @@ pub fn lt(lhs: &BooleanArray, rhs: &BooleanArray) -> Result<BooleanArray> {

/// Perform `left < right` operation on an array and a scalar value.
pub fn lt_scalar(lhs: &BooleanArray, rhs: bool) -> BooleanArray {
compare_op_scalar(lhs, rhs, |a, b| !a & b)
if rhs {
compare_op_scalar(lhs, rhs, |a, _| !a)
} else {
lhs.clone()
}
}

/// Perform `left <= right` operation on two arrays.
Expand All @@ -113,7 +121,11 @@ pub fn lt_eq(lhs: &BooleanArray, rhs: &BooleanArray) -> Result<BooleanArray> {
/// Perform `left <= right` operation on an array and a scalar value.
/// Null values are less than non-null values.
pub fn lt_eq_scalar(lhs: &BooleanArray, rhs: bool) -> BooleanArray {
compare_op_scalar(lhs, rhs, |a, b| !a | b)
if rhs {
lhs.clone()
} else {
compare_op_scalar(lhs, rhs, |a, _| !a)
}
}

/// Perform `left > right` operation on two arrays. Non-null values are greater than null
Expand All @@ -125,7 +137,15 @@ pub fn gt(lhs: &BooleanArray, rhs: &BooleanArray) -> Result<BooleanArray> {
/// Perform `left > right` operation on an array and a scalar value.
/// Non-null values are greater than null values.
pub fn gt_scalar(lhs: &BooleanArray, rhs: bool) -> BooleanArray {
compare_op_scalar(lhs, rhs, |a, b| a & !b)
if rhs {
BooleanArray::from_data(
DataType::Boolean,
Bitmap::new_zeroed(lhs.len()),
lhs.validity().cloned(),
)
} else {
compare_op_scalar(lhs, rhs, |_, _| 0b11111111)
}
}

/// Perform `left >= right` operation on two arrays. Non-null values are greater than null
Expand All @@ -137,7 +157,11 @@ pub fn gt_eq(lhs: &BooleanArray, rhs: &BooleanArray) -> Result<BooleanArray> {
/// Perform `left >= right` operation on an array and a scalar value.
/// Non-null values are greater than null values.
pub fn gt_eq_scalar(lhs: &BooleanArray, rhs: bool) -> BooleanArray {
compare_op_scalar(lhs, rhs, |a, b| a | !b)
if rhs {
compare_op_scalar(lhs, rhs, |a, _| a)
} else {
lhs.clone()
}
}

/// Compare two [`BooleanArray`]s using the given [`Operator`].
Expand Down