-
Notifications
You must be signed in to change notification settings - Fork 107
perf: list contains scalar scalar #5792
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
0ax1
wants to merge
1
commit into
develop
Choose a base branch
from
ad/list-contains
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.
+47
−17
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,13 +20,14 @@ use vortex_error::vortex_err; | |
| use vortex_mask::Mask; | ||
| use vortex_vector::BoolDatum; | ||
| use vortex_vector::Datum; | ||
| use vortex_vector::ScalarOps; | ||
| use vortex_vector::Vector; | ||
| use vortex_vector::VectorMutOps; | ||
| use vortex_vector::VectorOps; | ||
| use vortex_vector::bool::BoolScalar; | ||
| use vortex_vector::bool::BoolVector; | ||
| use vortex_vector::listview::ListViewScalar; | ||
| use vortex_vector::listview::ListViewVector; | ||
| use vortex_vector::match_each_pvector; | ||
| use vortex_vector::primitive::PScalar; | ||
| use vortex_vector::primitive::PVector; | ||
|
|
||
| use crate::ArrayRef; | ||
|
|
@@ -128,30 +129,34 @@ impl VTable for ListContains { | |
| .try_into() | ||
| .map_err(|_| vortex_err!("Wrong number of arguments for ListContains expression"))?; | ||
|
|
||
| let matches = match (lhs.as_scalar().is_some(), rhs.as_scalar().is_some()) { | ||
| match (lhs.as_scalar().is_some(), rhs.as_scalar().is_some()) { | ||
| (true, true) => { | ||
| // Early return with Scalar to avoid allocating BitBuffer. | ||
| let list = lhs.into_scalar().vortex_expect("scalar").into_list(); | ||
| let needle = rhs.into_scalar().vortex_expect("scalar"); | ||
| // Convert the needle scalar to a vector with row_count | ||
| // elements and reuse constant_list_scalar_contains | ||
| let needle_vector = needle.repeat(args.row_count).freeze(); | ||
| constant_list_scalar_contains(list, needle_vector)? | ||
| let found = list_contains_scalar_scalar(&list, &needle)?; | ||
| Ok(Datum::Scalar(BoolScalar::new(Some(found)).into())) | ||
| } | ||
| (true, false) => { | ||
| let matches = constant_list_scalar_contains( | ||
| lhs.into_scalar().vortex_expect("scalar").into_list(), | ||
| rhs.into_vector().vortex_expect("vector"), | ||
| )?; | ||
| Ok(Datum::Vector(matches.into())) | ||
| } | ||
| (false, true) => { | ||
| let matches = list_contains_scalar( | ||
| lhs.unwrap_into_vector(args.row_count).into_list(), | ||
| rhs.into_scalar().vortex_expect("scalar").into_list(), | ||
| )?; | ||
| Ok(Datum::Vector(matches.into())) | ||
| } | ||
| (true, false) => constant_list_scalar_contains( | ||
| lhs.into_scalar().vortex_expect("scalar").into_list(), | ||
| rhs.into_vector().vortex_expect("vector"), | ||
| )?, | ||
| (false, true) => list_contains_scalar( | ||
| lhs.unwrap_into_vector(args.row_count).into_list(), | ||
| rhs.into_scalar().vortex_expect("scalar").into_list(), | ||
| )?, | ||
| (false, false) => { | ||
| vortex_bail!( | ||
| "ListContains currently only supports constant needle (RHS) or constant list (LHS)" | ||
| ) | ||
| } | ||
| }; | ||
| Ok(Datum::Vector(matches.into())) | ||
| } | ||
| } | ||
|
|
||
| fn stat_falsification( | ||
|
|
@@ -330,6 +335,31 @@ fn constant_list_scalar_contains(list: ListViewScalar, values: Vector) -> Vortex | |
| Ok(result) | ||
| } | ||
|
|
||
| /// Used when both needle and list are scalars. | ||
| fn list_contains_scalar_scalar( | ||
| list: &ListViewScalar, | ||
| needle: &vortex_vector::Scalar, | ||
| ) -> VortexResult<bool> { | ||
| let elements = list.value().elements(); | ||
|
|
||
| // Downcast to `PVector` and access slice directly to avoid `scalar_at` overhead. | ||
| let found = if let Vector::Primitive(prim) = &**elements { | ||
| match_each_pvector!(prim, |pvec| { | ||
| let slice: &[_] = pvec.as_ref(); | ||
| let validity = pvec.validity(); | ||
| slice | ||
| .iter() | ||
| .enumerate() | ||
| .any(|(i, &elem)| needle == &PScalar::new(Some(elem)).into() && validity.value(i)) | ||
| }) | ||
| } else { | ||
| // Fallback for non-primitive vectors | ||
| (0..elements.len()).any(|i| needle == &elements.scalar_at(i)) | ||
|
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. can we use a array compare here with the elements and a constant array?
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. shouldn't need to special case anything |
||
| }; | ||
|
|
||
| Ok(found) | ||
| } | ||
|
|
||
| /// Returns a [`BitBuffer`] where each bit represents if a list contains the scalar, derived from a | ||
| /// [`BoolArray`] of matches on the child elements array. | ||
| /// | ||
|
|
||
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.
should this method be in vortex-compute?