Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d1ec381

Browse files
authored
Better scalar compare using collect_bool (#792)
1 parent 13107a3 commit d1ec381

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

  • vortex-array/src/array/primitive/compute

vortex-array/src/array/primitive/compute/compare.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use arrow_buffer::bit_util::ceil;
2-
use arrow_buffer::{BooleanBuffer, BooleanBufferBuilder, MutableBuffer};
2+
use arrow_buffer::{BooleanBuffer, MutableBuffer};
33
use vortex_dtype::{match_each_native_ptype, NativePType};
44
use vortex_error::{VortexExpect, VortexResult};
55
use vortex_scalar::PrimitiveScalar;
66

77
use crate::array::primitive::PrimitiveArray;
88
use crate::array::{BoolArray, ConstantArray};
99
use crate::compute::{MaybeCompareFn, Operator};
10-
use crate::validity::ArrayValidity;
1110
use crate::{Array, IntoArray};
1211

1312
impl MaybeCompareFn for PrimitiveArray {
@@ -42,23 +41,28 @@ fn primitive_const_compare(
4241
other: ConstantArray,
4342
operator: Operator,
4443
) -> VortexResult<Array> {
45-
let mut builder = BooleanBufferBuilder::new(this.len());
4644
let primitive_scalar =
4745
PrimitiveScalar::try_from(other.scalar()).vortex_expect("Expected a primitive scalar");
4846

49-
match_each_native_ptype!(this.ptype(), |$T| {
50-
let op_fn = operator.to_fn::<$T>();
47+
let buffer = match_each_native_ptype!(this.ptype(), |$T| {
5148
let typed_value = primitive_scalar.typed_value::<$T>().unwrap();
52-
for v in this.maybe_null_slice::<$T>() {
53-
builder.append(op_fn(*v, typed_value));
54-
}
49+
primitive_value_compare::<$T>(this, typed_value, operator)
5550
});
5651

57-
let validity = this
58-
.validity()
59-
.and(other.logical_validity().into_validity())?
60-
.into_nullable();
61-
Ok(BoolArray::try_new(builder.finish(), validity)?.into_array())
52+
Ok(BoolArray::try_new(buffer, this.validity().into_nullable())?.into_array())
53+
}
54+
55+
fn primitive_value_compare<T: NativePType>(
56+
this: &PrimitiveArray,
57+
value: T,
58+
op: Operator,
59+
) -> BooleanBuffer {
60+
let op_fn = op.to_fn::<T>();
61+
let slice = this.maybe_null_slice::<T>();
62+
63+
BooleanBuffer::collect_bool(this.len(), |idx| {
64+
op_fn(unsafe { *slice.get_unchecked(idx) }, value)
65+
})
6266
}
6367

6468
fn apply_predicate<T: NativePType, F: Fn(T, T) -> bool>(
@@ -78,7 +82,9 @@ fn apply_predicate<T: NativePType, F: Fn(T, T) -> bool>(
7882
let mut packed_block = 0_u64;
7983
for bit_idx in 0..BLOCK_SIZE {
8084
let idx = bit_idx + block * BLOCK_SIZE;
81-
let r = f(lhs[idx], rhs[idx]);
85+
let r = f(unsafe { *lhs.get_unchecked(idx) }, unsafe {
86+
*rhs.get_unchecked(idx)
87+
});
8288
packed_block |= (r as u64) << bit_idx;
8389
}
8490

0 commit comments

Comments
 (0)