-
Notifications
You must be signed in to change notification settings - Fork 216
Compare primitive values with RowFn #9703
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
Open
connortsui20
wants to merge
1
commit into
develop
Choose a base branch
from
ct/primitive-comparison-simd
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,28 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| //! Native comparison of primitive arrays via bit-packing lane kernels. | ||
| //! Primitive comparison execution through [`RowFn`]. | ||
| //! | ||
| //! [`PrimitiveCompare`] delegates decoding, constant handling, validity, and packed Boolean output | ||
| //! to the row executor. Its row kernel contains only the comparison selected by [`CompareOperator`]. | ||
|
|
||
| use vortex_buffer::BitBuffer; | ||
| use vortex_error::VortexResult; | ||
| use vortex_error::vortex_bail; | ||
|
|
||
| use crate::ArrayRef; | ||
| use crate::ExecutionCtx; | ||
| use crate::IntoArray; | ||
| use crate::arrays::BoolArray; | ||
| use crate::arrays::ConstantArray; | ||
| use crate::dtype::DType; | ||
| use crate::dtype::NativePType; | ||
| use crate::dtype::Nullability; | ||
| use crate::dtype::PType; | ||
| use crate::match_each_native_ptype; | ||
| use crate::scalar::Scalar; | ||
| use crate::scalar_fn::fns::binary::compare::collect_bits; | ||
| use crate::scalar_fn::fns::binary::compare::collect_zip_bits; | ||
| use crate::scalar_fn::fns::binary::compare::compare_validity; | ||
| use crate::scalar_fn::fns::binary::primitive_operand::PrimitiveOperand; | ||
| use crate::scalar_fn::ScalarFnId; | ||
| use crate::scalar_fn::ScalarFnVTable; | ||
| use crate::scalar_fn::VecExecutionArgs; | ||
| use crate::scalar_fn::fns::binary::Binary; | ||
| use crate::scalar_fn::fns::operators::CompareOperator; | ||
| use crate::scalar_fn::unstable::row::RowFn; | ||
| use crate::scalar_fn::unstable::row::RowVisitor; | ||
| use crate::scalar_fn::unstable::row::execute_rows; | ||
|
|
||
| /// Compare two primitive arrays of the same [`PType`]. | ||
| /// | ||
|
|
@@ -32,99 +32,71 @@ pub(super) fn compare_primitive( | |
| lhs: &ArrayRef, | ||
| rhs: &ArrayRef, | ||
| op: CompareOperator, | ||
| nullability: Nullability, | ||
| ctx: &mut ExecutionCtx, | ||
| ) -> VortexResult<ArrayRef> { | ||
| let ptype = PType::try_from(lhs.dtype())?; | ||
| match_each_native_ptype!(ptype, |T| { | ||
| compare_primitive_typed::<T>(lhs, rhs, op, nullability, ctx) | ||
| }) | ||
| let args = VecExecutionArgs::new(vec![lhs.clone(), rhs.clone()], lhs.len()); | ||
|
|
||
| execute_rows(&PrimitiveCompare, &op, &args, ctx) | ||
| } | ||
|
|
||
| fn compare_primitive_typed<T: NativePType>( | ||
| lhs: &ArrayRef, | ||
| rhs: &ArrayRef, | ||
| op: CompareOperator, | ||
| nullability: Nullability, | ||
| ctx: &mut ExecutionCtx, | ||
| ) -> VortexResult<ArrayRef> { | ||
| let len = lhs.len(); | ||
| let lhs = PrimitiveOperand::<T>::try_new(lhs, ctx)?; | ||
| let rhs = PrimitiveOperand::<T>::try_new(rhs, ctx)?; | ||
| if lhs.len() != rhs.len() { | ||
| vortex_bail!( | ||
| "compare operator requires equal lengths, got {} and {}", | ||
| lhs.len(), | ||
| rhs.len() | ||
| ); | ||
| } | ||
| /// Internal row execution for primitive comparison operators. | ||
| #[derive(Clone)] | ||
| struct PrimitiveCompare; | ||
|
|
||
| let validity = compare_validity(lhs.validity(), rhs.validity(), nullability)?; | ||
| impl RowFn for PrimitiveCompare { | ||
| type Options = CompareOperator; | ||
|
|
||
| let bits = match (&lhs, &rhs) { | ||
| ( | ||
| PrimitiveOperand::Array { values: lhs, .. }, | ||
| PrimitiveOperand::Array { values: rhs, .. }, | ||
| ) => compare_slices(lhs, rhs, op), | ||
| ( | ||
| PrimitiveOperand::Array { values: lhs, .. }, | ||
| PrimitiveOperand::Constant { value: rhs, .. }, | ||
| ) => compare_slice_constant(lhs, *rhs, op), | ||
| ( | ||
| PrimitiveOperand::Constant { value: lhs, .. }, | ||
| PrimitiveOperand::Array { values: rhs, .. }, | ||
| ) => compare_slice_constant(rhs, *lhs, op.swap()), | ||
| ( | ||
| PrimitiveOperand::Constant { value: lhs, .. }, | ||
| PrimitiveOperand::Constant { value: rhs, .. }, | ||
| ) => { | ||
| // Unreachable through `execute_compare` (constant-constant is folded there), but | ||
| // cheap to answer anyway. | ||
| BitBuffer::full(apply_op(*lhs, *rhs, op), len) | ||
| } | ||
| (PrimitiveOperand::Null(_), _) | (_, PrimitiveOperand::Null(_)) => { | ||
| return Ok( | ||
| ConstantArray::new(Scalar::null(DType::Bool(Nullability::Nullable)), len) | ||
| .into_array(), | ||
| ); | ||
| } | ||
| }; | ||
| const ARG_NAMES: &'static [&'static str] = &["lhs", "rhs"]; | ||
|
|
||
| Ok(BoolArray::try_new(bits, validity)?.into_array()) | ||
| } | ||
| const INFALLIBLE: bool = true; | ||
|
|
||
| #[inline(always)] | ||
| fn apply_op<T: NativePType>(lhs: T, rhs: T, op: CompareOperator) -> bool { | ||
| match op { | ||
| CompareOperator::Eq => lhs.is_eq(rhs), | ||
| CompareOperator::NotEq => !lhs.is_eq(rhs), | ||
| CompareOperator::Gt => lhs.is_gt(rhs), | ||
| CompareOperator::Gte => lhs.is_ge(rhs), | ||
| CompareOperator::Lt => lhs.is_lt(rhs), | ||
| CompareOperator::Lte => lhs.is_le(rhs), | ||
| fn id(&self) -> ScalarFnId { | ||
| // `PrimitiveCompare` is a private implementation detail of `Binary`: it is never registered | ||
| // or serialized independently. Reusing the public ID keeps execution errors attributed to | ||
| // `Binary`. If this type becomes registrable, it needs its own ID and persistence contract. | ||
| ScalarFnVTable::id(&Binary) | ||
| } | ||
| } | ||
|
|
||
| fn compare_slices<T: NativePType>(lhs: &[T], rhs: &[T], op: CompareOperator) -> BitBuffer { | ||
| // Dispatch the operator outside the lane loop so each instantiation vectorizes a single | ||
| // branch-free predicate. | ||
| match op { | ||
| CompareOperator::Eq => collect_zip_bits(lhs, rhs, |a: T, b: T| a.is_eq(b)), | ||
| CompareOperator::NotEq => collect_zip_bits(lhs, rhs, |a: T, b: T| !a.is_eq(b)), | ||
| CompareOperator::Gt => collect_zip_bits(lhs, rhs, T::is_gt), | ||
| CompareOperator::Gte => collect_zip_bits(lhs, rhs, T::is_ge), | ||
| CompareOperator::Lt => collect_zip_bits(lhs, rhs, T::is_lt), | ||
| CompareOperator::Lte => collect_zip_bits(lhs, rhs, T::is_le), | ||
| fn dispatch<V: RowVisitor>( | ||
| &self, | ||
| op: &Self::Options, | ||
| args: &[DType], | ||
| visitor: V, | ||
| ) -> VortexResult<V::VisitResult> { | ||
| let [lhs_dtype, _] = args else { | ||
| vortex_bail!( | ||
| "a primitive comparison requires two operands, got {}", | ||
| args.len(), | ||
| ); | ||
| }; | ||
| let ptype = PType::try_from(lhs_dtype)?; | ||
|
|
||
| match_each_native_ptype!(ptype, |T| { visit_compare::<T, V>(*op, visitor) }) | ||
| } | ||
| } | ||
|
|
||
| fn compare_slice_constant<T: NativePType>(lhs: &[T], rhs: T, op: CompareOperator) -> BitBuffer { | ||
| fn visit_compare<T, V>(op: CompareOperator, visitor: V) -> VortexResult<V::VisitResult> | ||
| where | ||
| T: NativePType, | ||
| V: RowVisitor, | ||
| { | ||
| match op { | ||
| CompareOperator::Eq => collect_bits(lhs, |a: T| a.is_eq(rhs)), | ||
| CompareOperator::NotEq => collect_bits(lhs, |a: T| !a.is_eq(rhs)), | ||
| CompareOperator::Gt => collect_bits(lhs, |a: T| a.is_gt(rhs)), | ||
| CompareOperator::Gte => collect_bits(lhs, |a: T| a.is_ge(rhs)), | ||
| CompareOperator::Lt => collect_bits(lhs, |a: T| a.is_lt(rhs)), | ||
| CompareOperator::Lte => collect_bits(lhs, |a: T| a.is_le(rhs)), | ||
| CompareOperator::Eq => visit_compare_with::<T, V>(visitor, T::is_eq), | ||
| CompareOperator::NotEq => visit_compare_with::<T, V>(visitor, |lhs, rhs| !lhs.is_eq(rhs)), | ||
| CompareOperator::Gt => visit_compare_with::<T, V>(visitor, T::is_gt), | ||
| CompareOperator::Gte => visit_compare_with::<T, V>(visitor, T::is_ge), | ||
| CompareOperator::Lt => visit_compare_with::<T, V>(visitor, T::is_lt), | ||
| CompareOperator::Lte => visit_compare_with::<T, V>(visitor, T::is_le), | ||
| } | ||
| } | ||
|
|
||
| fn visit_compare_with<T, V>( | ||
| visitor: V, | ||
| compare: impl Fn(T, T) -> bool, | ||
| ) -> VortexResult<V::VisitResult> | ||
| where | ||
| T: NativePType, | ||
| V: RowVisitor, | ||
| { | ||
| visitor.visit_bool::<(T, T), true>(move |(lhs, rhs)| compare(lhs, rhs)) | ||
| } | ||
|
Comment on lines
+93
to
+102
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? did this save you much?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, see #9626 |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 0 additions & 69 deletions
69
vortex-array/src/scalar_fn/fns/binary/primitive_operand.rs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you use
MULTIVERSIONEDhere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are small operators and cheap functions, isn't that the whole point of multiversioned?