-
Notifications
You must be signed in to change notification settings - Fork 126
Normalize an Array #6213
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
gatesn
wants to merge
2
commits into
develop
Choose a base branch
from
ngates/normalize
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.
+210
−0
Open
Normalize an Array #6213
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| use vortex_error::VortexResult; | ||
| use vortex_error::vortex_bail; | ||
|
|
||
| use crate::Array; | ||
| use crate::ArrayEq; | ||
| use crate::ArrayRef; | ||
| use crate::Canonical; | ||
| use crate::ExecutionCtx; | ||
| use crate::IntoArray; | ||
| use crate::Precision; | ||
| use crate::session::ArrayRegistry; | ||
| use crate::vtable::ArrayId; | ||
|
|
||
| /// Options for normalizing an array. | ||
| pub struct NormalizeOptions<'a> { | ||
| /// The set of allowed array encodings (in addition to the canonical ones) that are permitted | ||
| /// in the normalized array. | ||
| allowed: &'a ArrayRegistry, | ||
| /// The operation to perform when a non-allowed encoding is encountered. | ||
| operation: Operation<'a>, | ||
| } | ||
|
|
||
| /// The operation to perform when a non-allowed encoding is encountered. | ||
| enum Operation<'a> { | ||
| IntoCanonical(&'a mut ExecutionCtx), | ||
| Error, | ||
| } | ||
|
|
||
| impl<'a> NormalizeOptions<'a> { | ||
| /// Create a new `NormalizeOptions` that returns an error for non-allowed encodings. | ||
| pub fn error(allowed: &'a ArrayRegistry) -> Self { | ||
| Self { | ||
| allowed, | ||
| operation: Operation::Error, | ||
| } | ||
| } | ||
|
|
||
| /// Create a new `NormalizeOptions` that canonicalizes non-allowed encodings. | ||
| pub fn canonicalize(allowed: &'a ArrayRegistry, ctx: &'a mut ExecutionCtx) -> Self { | ||
| Self { | ||
| allowed, | ||
| operation: Operation::IntoCanonical(ctx), | ||
| } | ||
| } | ||
|
|
||
| /// Check if the given array ID is allowed. | ||
| fn is_allowed(&self, id: &ArrayId) -> bool { | ||
| self.allowed.find(id).is_some() | ||
| } | ||
| } | ||
|
|
||
| impl dyn Array + '_ { | ||
| /// Normalize the array according to given options. | ||
| /// | ||
| /// This operation performs a recursive traversal of the array. Any non-allowed encoding is | ||
| /// normalized per the configured operation. | ||
| pub fn normalize(&self, options: &mut NormalizeOptions) -> VortexResult<ArrayRef> { | ||
| if !self.is_canonical() && !options.is_allowed(&self.encoding_id()) { | ||
| match &mut options.operation { | ||
| Operation::IntoCanonical(ctx) => { | ||
| return self | ||
| .to_array() | ||
| .execute::<Canonical>(ctx)? | ||
| .into_array() | ||
| .normalize(options); | ||
| } | ||
| Operation::Error => vortex_bail!( | ||
| "Array encoding '{}' is not allowed in normalized array", | ||
| self.encoding_id() | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| let children = self.children(); | ||
| let mut new_children = Vec::with_capacity(children.len()); | ||
| for child in &children { | ||
| new_children.push(child.normalize(options)?); | ||
| } | ||
|
|
||
| if children | ||
| .iter() | ||
| .zip(new_children.iter()) | ||
| .all(|(a, b)| a.array_eq(b, Precision::Ptr)) | ||
| { | ||
| // No children changed; clone self. | ||
| return Ok(self.to_array()); | ||
| } | ||
|
|
||
| self.with_children(new_children) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use vortex_dtype::FieldNames; | ||
| use vortex_error::VortexResult; | ||
|
|
||
| use super::*; | ||
| use crate::LEGACY_SESSION; | ||
| use crate::VortexSessionExecute; | ||
| use crate::arrays::ConstantArray; | ||
| use crate::arrays::ConstantVTable; | ||
| use crate::arrays::PrimitiveArray; | ||
| use crate::arrays::StructArray; | ||
| use crate::assert_arrays_eq; | ||
| use crate::session::ArraySessionExt; | ||
| use crate::validity::Validity; | ||
|
|
||
| #[test] | ||
| fn canonical_array_passes_through() -> VortexResult<()> { | ||
| let array = PrimitiveArray::from_iter([1i32, 2, 3]).into_array(); | ||
| let registry = LEGACY_SESSION.arrays().registry().clone(); | ||
| let mut opts = NormalizeOptions::error(®istry); | ||
|
|
||
| let result = array.normalize(&mut opts)?; | ||
| assert_arrays_eq!(&result, &array); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn non_allowed_encoding_errors() { | ||
| let array = ConstantArray::new(42i32, 5).into_array(); | ||
| let registry = ArrayRegistry::empty(); | ||
| let mut opts = NormalizeOptions::error(®istry); | ||
|
|
||
| let result = array.normalize(&mut opts); | ||
| assert!(result.is_err()); | ||
| let err = result.unwrap_err().to_string(); | ||
| assert!( | ||
| err.contains("vortex.constant"), | ||
| "Expected error to mention encoding id, got: {err}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn non_allowed_encoding_is_canonicalized() -> VortexResult<()> { | ||
| let array = ConstantArray::new(42i32, 5).into_array(); | ||
| // Use an empty registry so ConstantArray is not allowed. | ||
| // Canonical encodings are always allowed via is_canonical(). | ||
| let registry = ArrayRegistry::empty(); | ||
| let mut ctx = LEGACY_SESSION.create_execution_ctx(); | ||
| let mut opts = NormalizeOptions::canonicalize(®istry, &mut ctx); | ||
|
|
||
| let result = array.normalize(&mut opts)?; | ||
| assert_arrays_eq!(&result, &array); | ||
| assert!(result.is_canonical()); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn allowed_encoding_passes_through() -> VortexResult<()> { | ||
| let array = ConstantArray::new(42i32, 5).into_array(); | ||
| // Create a registry that allows ConstantArray. | ||
| let registry = ArrayRegistry::default(); | ||
| registry.register(ConstantVTable::ID, ConstantVTable); | ||
| let mut opts = NormalizeOptions::error(®istry); | ||
|
|
||
| let result = array.normalize(&mut opts)?; | ||
| assert_arrays_eq!(&result, &array); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn recursive_children_are_normalized() -> VortexResult<()> { | ||
| // Struct with a constant child - canonical struct is allowed via is_canonical(), | ||
| // but its constant child is not in the empty registry and should be canonicalized. | ||
| let child = ConstantArray::new(7i32, 3).into_array(); | ||
| let struct_array = StructArray::try_new( | ||
| FieldNames::from(["values"]), | ||
| vec![child], | ||
| 3, | ||
| Validity::NonNullable, | ||
| )? | ||
| .into_array(); | ||
|
|
||
| let registry = ArrayRegistry::empty(); | ||
| let mut ctx = LEGACY_SESSION.create_execution_ctx(); | ||
| let mut opts = NormalizeOptions::canonicalize(®istry, &mut ctx); | ||
|
|
||
| let result = struct_array.normalize(&mut opts)?; | ||
| assert_arrays_eq!(&result, &struct_array); | ||
| let result_child = &result.children()[0]; | ||
| assert!(result_child.is_canonical()); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn canonical_children_are_not_reconstructed() -> VortexResult<()> { | ||
| // When all children are already canonical, the original array is returned (by pointer). | ||
| let child = PrimitiveArray::from_iter([1i32, 2, 3]).into_array(); | ||
| let struct_array = StructArray::try_new( | ||
| FieldNames::from(["values"]), | ||
| vec![child], | ||
| 3, | ||
| Validity::NonNullable, | ||
| )? | ||
| .into_array(); | ||
|
|
||
| let registry = LEGACY_SESSION.arrays().registry().clone(); | ||
| let mut opts = NormalizeOptions::error(®istry); | ||
|
|
||
| let result = struct_array.normalize(&mut opts)?; | ||
| assert!(result.array_eq(&struct_array, Precision::Ptr)); | ||
| Ok(()) | ||
| } | ||
| } | ||
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.
When would we use this?