Repository navigation
Add filter-and-scatter RowFn execution #9521
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
Merged
Merged
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
95 changes: 95 additions & 0 deletions
95
vortex-array/src/scalar_fn/unstable/row/batch/execute/filter_scatter.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,95 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| //! Fallback execution for row kernels that cannot operate on the original partially valid inputs. | ||
| //! | ||
| //! Direct skip-invalid execution retains the original row domain: it decodes every input column, | ||
| //! initializes a full-length output, and visits only valid rows. Some input representations cannot | ||
| //! decode unspecified payloads behind nulls, and some output sinks cannot initialize rows that the | ||
| //! kernel skips. In either case, there is no safe row loop to enter over the original columns. | ||
| //! | ||
| //! This fallback filters every input to the valid row domain before decoding it. The ordinary | ||
| //! kernel then produces a compact, all-valid output. A take with nullable indices scatters those | ||
| //! values back to the original row domain and restores its nulls. | ||
| //! | ||
| //! Filtering and scattering add columnar work around the row loop. Filtering nested or compressed | ||
| //! inputs can also materialize their selected representation. Batch execution therefore tries | ||
| //! direct skip-invalid execution first and uses this path only when a required capability is | ||
| //! unavailable. | ||
| //! | ||
| //! A prepared, type-specific view can provide selected-row decoding without filtering. That is an | ||
| //! optional direct-execution capability rather than a replacement for this fallback: it requires a | ||
| //! representation with safe selected access, and it does not initialize an output sink that cannot | ||
| //! represent skipped rows. Calling the general scalar-extraction API once per valid row is also not | ||
| //! equivalent because it repeats array execution and scalar construction inside the row loop. | ||
|
|
||
| use smallvec::SmallVec; | ||
| use vortex_error::VortexResult; | ||
| use vortex_mask::Mask; | ||
| use vortex_mask::MaskValuesRef; | ||
|
|
||
| use super::super::RowFnExecutionArgs; | ||
| use super::super::args::BorrowedRowFnArgs; | ||
| use crate::ArrayRef; | ||
| use crate::ExecutionCtx; | ||
| use crate::IntoArray; | ||
| use crate::arrays::PrimitiveArray; | ||
| use crate::dtype::Nullability; | ||
| use crate::validity::Validity; | ||
|
|
||
| impl RowFnExecutionArgs { | ||
| /// Filter the original batch to valid rows, run the kernel, then restore its row count. | ||
| pub(super) fn filter_and_scatter( | ||
| &self, | ||
| kernel: impl Fn(BorrowedRowFnArgs<'_>, &mut ExecutionCtx) -> VortexResult<ArrayRef>, | ||
| original_validity: &MaskValuesRef, | ||
| ctx: &mut ExecutionCtx, | ||
| ) -> VortexResult<ArrayRef> { | ||
| let original_len = original_validity.len(); | ||
| let filtered_len = original_validity.true_count(); | ||
| let filter_mask = Mask::Values(MaskValuesRef::clone(original_validity)); | ||
|
|
||
| let filtered_inputs: SmallVec<[ArrayRef; 4]> = self | ||
| .inputs | ||
| .iter() | ||
| .map(|input| input.filter(filter_mask.clone())) | ||
| .collect::<VortexResult<_>>()?; | ||
|
|
||
| let filtered_args = self.execution_args(&filtered_inputs, filtered_len); | ||
| let filtered = kernel(filtered_args, ctx)?; | ||
| let filtered = self.validate_kernel_output(filtered, filtered_len, ctx)?; | ||
|
|
||
| let output = Self::scatter_to_original_rows(filtered, original_validity)?; | ||
|
|
||
| self.finalize_output(output, original_len) | ||
| } | ||
|
|
||
| /// Scatter `filtered` back to the rows selected by `original_validity`. | ||
| fn scatter_to_original_rows( | ||
| filtered: ArrayRef, | ||
| original_validity: &MaskValuesRef, | ||
| ) -> VortexResult<ArrayRef> { | ||
| let original_len = original_validity.len(); | ||
| let mut take_indices = vec![0u64; original_len]; | ||
|
|
||
| let valid_rows = original_validity | ||
| .slices() | ||
| .iter() | ||
| .flat_map(|&(start, end)| start..end); | ||
| for (filtered_index, original_index) in valid_rows.enumerate() { | ||
| take_indices[original_index] = u64::try_from(filtered_index)?; | ||
| } | ||
|
|
||
| // Null indices restore invalid rows without selecting a value from the compact output. | ||
| let take_indices = PrimitiveArray::new( | ||
| take_indices, | ||
| Validity::from_mask( | ||
| Mask::Values(MaskValuesRef::clone(original_validity)), | ||
| Nullability::Nullable, | ||
| ), | ||
| ) | ||
| .into_array(); | ||
|
|
||
| filtered.take(take_indices) | ||
| } | ||
| } |
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.
how is this the default here?
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.
the alternative is much more complicated (per-row decode needs to be defined by each input type). That can maybe be added in the future. This is just the easy way to do it