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

FoR compare #1656

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
73 changes: 73 additions & 0 deletions encodings/fastlanes/src/for/compute/compare.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use num_traits::{CheckedShl, WrappingSub};
use vortex_array::array::ConstantArray;
use vortex_array::compute::{compare, CompareFn, Operator};
use vortex_array::{ArrayDType, ArrayData, ArrayLen, IntoArrayData};
use vortex_dtype::{match_each_integer_ptype, NativePType};
use vortex_error::{vortex_err, VortexError, VortexResult};
use vortex_scalar::{PValue, PrimitiveScalar, Scalar};

use crate::{FoRArray, FoREncoding};

impl CompareFn<FoRArray> for FoREncoding {
fn compare(
&self,
lhs: &FoRArray,
rhs: &ArrayData,
operator: Operator,
) -> VortexResult<Option<ArrayData>> {
if let Some(constant) = rhs.as_constant() {
if let Ok(constant) = PrimitiveScalar::try_from(&constant) {
match_each_integer_ptype!(constant.ptype(), |$T| {
return compare_constant(lhs, constant.typed_value::<$T>(), operator);
})
}
}

Ok(None)
}
}

fn compare_constant<T>(
lhs: &FoRArray,
rhs: Option<T>,
operator: Operator,
) -> VortexResult<Option<ArrayData>>
where
T: NativePType + WrappingSub + CheckedShl,
T: TryFrom<PValue, Error = VortexError>,
Scalar: From<Option<T>>,
{
// For now, we only support equals and not equals. Comparisons are a little more fiddly to
// get right regarding how to handle overflow and the wrapping subtraction.
Copy link
Contributor

Choose a reason for hiding this comment

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

If the right side is less than the reference, we can perform a short-circuit Greater Than comparison.

if !matches!(operator, Operator::Eq | Operator::NotEq) {
return Ok(None);
}

let reference = lhs.reference_scalar();
let reference = reference.as_primitive().typed_value::<T>();

// We encode the RHS into the FoR domain.
let rhs = rhs
.map(|mut rhs| {
if let Some(reference) = reference {
rhs = rhs.wrapping_sub(&reference);
}
if lhs.shift() > 0 {
rhs.checked_shl(lhs.shift() as u32)
.ok_or_else(|| vortex_err!("Shift overflow"))?;
}
Ok::<_, VortexError>(rhs)
})
.transpose()?;

// Wrap up the RHS into a scalar and cast to the encoded DType (this will be the equivalent
// unsigned integer type).
let rhs = Scalar::from(rhs).cast(lhs.encoded().dtype())?;

compare(
lhs.encoded(),
ConstantArray::new(rhs, lhs.len()).into_array(),
operator,
)
.map(Some)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod compare;

use std::ops::AddAssign;

use num_traits::{CheckedShl, CheckedShr, WrappingAdd, WrappingSub};
use vortex_array::compute::{
filter, scalar_at, search_sorted, slice, take, ComputeVTable, FilterFn, FilterMask, ScalarAtFn,
SearchResult, SearchSortedFn, SearchSortedSide, SliceFn, TakeFn,
filter, scalar_at, search_sorted, slice, take, CompareFn, ComputeVTable, FilterFn, FilterMask,
ScalarAtFn, SearchResult, SearchSortedFn, SearchSortedSide, SliceFn, TakeFn,
};
use vortex_array::variants::PrimitiveArrayTrait;
use vortex_array::{ArrayDType, ArrayData, IntoArrayData};
Expand All @@ -14,6 +16,10 @@ use vortex_scalar::{PValue, Scalar};
use crate::{FoRArray, FoREncoding};

impl ComputeVTable for FoREncoding {
fn compare_fn(&self) -> Option<&dyn CompareFn<ArrayData>> {
Some(self)
}

fn filter_fn(&self) -> Option<&dyn FilterFn<ArrayData>> {
Some(self)
}
Expand Down
Loading