-
Notifications
You must be signed in to change notification settings - Fork 237
Optimize list_contains with prepared constant sets #10052
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
robert3005
wants to merge
10
commits into
rk/list-contains-benchmarks
from
claude/friendly-knuth-dj6ed1-list-contains
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f3e5ee0
Optimize list_contains with prepared constant sets
robert3005 983a89b
Fix prepared-set test compilation
robert3005 c884836
Format prepared membership probes and tests
robert3005 09547e7
fixes
robert3005 e4c0339
less
robert3005 a351995
less
robert3005 77f114d
chunkedcompute
robert3005 843f9c8
more
robert3005 6791e82
more
robert3005 77467d3
move
robert3005 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
129 changes: 129 additions & 0 deletions
129
vortex-array/src/arrays/chunked/compute/list_contains.rs
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 |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| use vortex_error::VortexResult; | ||
|
|
||
| use crate::ArrayRef; | ||
| use crate::ExecutionCtx; | ||
| use crate::IntoArray; | ||
| use crate::array::ArrayView; | ||
| use crate::arrays::Chunked; | ||
| use crate::arrays::ChunkedArray; | ||
| use crate::arrays::chunked::ChunkedArrayExt; | ||
| use crate::dtype::DType; | ||
| use crate::scalar_fn::fns::list_contains::ListContains; | ||
| use crate::scalar_fn::fns::list_contains::ListContainsElementKernel; | ||
| use crate::scalar_fn::fns::list_contains::ListContainsOptions; | ||
| use crate::scalar_fn::fns::list_contains::PreparedSet; | ||
|
|
||
| /// Probes each chunk of the needles against one prepared set. | ||
| /// | ||
| /// A constant list is prepared first, by the execution of [`ListContains`]. Each chunk then gets a | ||
| /// lazy [`ListContains`] over a slice of the prepared set, which shares the one probe, so that the | ||
| /// kernels of the chunk's own encoding can probe it. | ||
| impl ListContainsElementKernel for Chunked { | ||
| fn list_contains( | ||
| list: &ArrayRef, | ||
| needles: ArrayView<'_, Chunked>, | ||
| options: &ListContainsOptions, | ||
| _ctx: &mut ExecutionCtx, | ||
| ) -> VortexResult<Option<ArrayRef>> { | ||
| if !list.is::<PreparedSet>() { | ||
| return Ok(None); | ||
| } | ||
|
|
||
| let mut offset = 0; | ||
| let chunks = needles | ||
| .iter_chunks() | ||
| .map(|chunk| { | ||
| let set = list.slice(offset..offset + chunk.len())?; | ||
| offset += chunk.len(); | ||
| Ok(ListContains::try_new_opts(set, chunk.clone(), *options)?.into_array()) | ||
| }) | ||
| .collect::<VortexResult<Vec<_>>>()?; | ||
|
|
||
| let dtype = DType::Bool(options.result_nullability(list.dtype(), needles.dtype())); | ||
|
|
||
| // SAFETY: each chunk is `list_contains` of the prepared set and a needle chunk of one dtype, | ||
| // so every chunk has the dtype of the whole result. | ||
| Ok(Some( | ||
| unsafe { ChunkedArray::new_unchecked(chunks, dtype) }.into_array(), | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use rstest::rstest; | ||
| use vortex_error::VortexResult; | ||
|
|
||
| use crate::ArrayRef; | ||
| use crate::IntoArray; | ||
| use crate::VortexSessionExecute; | ||
| use crate::array_session; | ||
| use crate::arrays::BoolArray; | ||
| use crate::arrays::Chunked; | ||
| use crate::arrays::ChunkedArray; | ||
| use crate::arrays::ConstantArray; | ||
| use crate::arrays::PrimitiveArray; | ||
| use crate::assert_arrays_eq; | ||
| use crate::dtype::DType; | ||
| use crate::dtype::Nullability; | ||
| use crate::dtype::PType; | ||
| use crate::optimizer::ArrayOptimizer; | ||
| use crate::scalar::Scalar; | ||
| use crate::scalar_fn::fns::list_contains::ListContains; | ||
| use crate::scalar_fn::fns::list_contains::ListContainsOptions; | ||
|
|
||
| /// The set `{2, null}` of nullable `i32`. | ||
| fn set_with_null() -> Scalar { | ||
| let element = DType::Primitive(PType::I32, Nullability::Nullable); | ||
| Scalar::list( | ||
| element.clone(), | ||
| vec![ | ||
| Scalar::primitive(2i32, Nullability::Nullable), | ||
| Scalar::null(element), | ||
| ], | ||
| Nullability::NonNullable, | ||
| ) | ||
| } | ||
|
|
||
| fn chunked_needles() -> VortexResult<ArrayRef> { | ||
| Ok(ChunkedArray::try_new( | ||
| vec![ | ||
| PrimitiveArray::from_option_iter([Some(1i32), Some(2)]).into_array(), | ||
| PrimitiveArray::from_option_iter::<i32, _>([]).into_array(), | ||
| PrimitiveArray::from_option_iter([None, Some(3), Some(2)]).into_array(), | ||
| ], | ||
| DType::Primitive(PType::I32, Nullability::Nullable), | ||
| )? | ||
| .into_array()) | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[case::default( | ||
| ListContainsOptions::default(), | ||
| [Some(false), Some(true), None, Some(false), Some(true)], | ||
| )] | ||
| #[case::sql( | ||
| ListContainsOptions { sql_null_semantics: true }, | ||
| [None, Some(true), None, None, Some(true)], | ||
| )] | ||
| fn test_constant_list_over_chunked_needles( | ||
| #[case] options: ListContainsOptions, | ||
| #[case] expected: [Option<bool>; 5], | ||
| ) -> VortexResult<()> { | ||
| let mut ctx = array_session().create_execution_ctx(); | ||
| let needles = chunked_needles()?; | ||
| let list = ConstantArray::new(set_with_null(), needles.len()).into_array(); | ||
|
|
||
| // The node stays whole, so that execution prepares the list once for every chunk. | ||
| let array = ListContains::try_new_opts(list, needles, options)? | ||
| .into_array() | ||
| .optimize()?; | ||
| assert!(!array.is::<Chunked>()); | ||
|
|
||
| assert_arrays_eq!(array, BoolArray::from_iter(expected), &mut ctx); | ||
| Ok(()) | ||
| } | ||
| } |
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
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.
this is a hack, we essentially have common setup we would like to share across chunks, if we just dispatch over each chunk there's no place to store the shared state