diff --git a/vortex-array/src/array/primitive/compute/compare.rs b/vortex-array/src/array/primitive/compute/compare.rs index cf6e5fc4f4d..786562e6eb5 100644 --- a/vortex-array/src/array/primitive/compute/compare.rs +++ b/vortex-array/src/array/primitive/compute/compare.rs @@ -82,9 +82,7 @@ fn apply_predicate bool>( let mut packed_block = 0_u64; for bit_idx in 0..BLOCK_SIZE { let idx = bit_idx + block * BLOCK_SIZE; - let r = f(unsafe { *lhs.get_unchecked(idx) }, unsafe { - *rhs.get_unchecked(idx) - }); + let r = unsafe { f(*lhs.get_unchecked(idx), *rhs.get_unchecked(idx)) }; packed_block |= (r as u64) << bit_idx; } @@ -97,7 +95,7 @@ fn apply_predicate bool>( let mut packed_block = 0_u64; for bit_idx in 0..reminder { let idx = bit_idx + block_count * BLOCK_SIZE; - let r = f(lhs[idx], rhs[idx]); + let r = unsafe { f(*lhs.get_unchecked(idx), *rhs.get_unchecked(idx)) }; packed_block |= (r as u64) << bit_idx; } diff --git a/vortex-array/src/compute/compare.rs b/vortex-array/src/compute/compare.rs index b0a5354ed4d..5ab56391165 100644 --- a/vortex-array/src/compute/compare.rs +++ b/vortex-array/src/compute/compare.rs @@ -89,10 +89,34 @@ pub fn compare(left: &Array, right: &Array, operator: Operator) -> VortexResult< vortex_bail!("Compare operations only support arrays of the same type"); } - if ConstantArray::try_from(left).is_ok() { - let scalar = scalar_at(left, 0)?; - let left_const = ConstantArray::new(scalar, left.len()).into_array(); - return compare(right, &left_const, operator.swap()); + use crate::stats::{ArrayStatistics, Stat}; + + let l_is_const = ConstantArray::try_from(left).is_ok() + || left + .statistics() + .get_as::(Stat::IsConstant) + .unwrap_or_default(); + let r_is_const = ConstantArray::try_from(right).is_ok() + || right + .statistics() + .get_as::(Stat::IsConstant) + .unwrap_or_default(); + + match (l_is_const, r_is_const) { + (true, false) => { + let scalar = scalar_at(left, 0)?; + let left_const = ConstantArray::new(scalar, left.len()).into_array(); + return compare(right, &left_const, operator.swap()); + } + (true, true) => { + let original_len = left.len(); + let left = scalar_at(left, 0)?; + let right = scalar_at(right, 0)?; + let r = scalar_cmp(&left, &right, operator); + let arr = ConstantArray::new(r, original_len); + return Ok(arr.into_array()); + } + (false, true) | (false, false) => {} } if let Some(selection) = left.with_dyn(|lhs| lhs.compare(right, operator)) {