-
Notifications
You must be signed in to change notification settings - Fork 819
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
Add PrimitiveArray::try_new (#3879) #4067
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -269,24 +269,55 @@ impl<T: ArrowPrimitiveType> PrimitiveArray<T> { | |||||
/// | ||||||
/// # Panics | ||||||
/// | ||||||
/// Panics if: | ||||||
/// - `values.len() != nulls.len()` | ||||||
/// - `!Self::is_compatible(data_type)` | ||||||
/// Panics if [`Self::try_new`] returns an error | ||||||
pub fn new( | ||||||
data_type: DataType, | ||||||
values: ScalarBuffer<T::Native>, | ||||||
nulls: Option<NullBuffer>, | ||||||
) -> Self { | ||||||
Self::assert_compatible(&data_type); | ||||||
Self::try_new(data_type, values, nulls).unwrap() | ||||||
} | ||||||
|
||||||
/// Create a new [`PrimitiveArray`] from the provided data_type, values, nulls | ||||||
/// | ||||||
/// # Errors | ||||||
/// | ||||||
/// Errors if: | ||||||
/// - `values.len() != nulls.len()` | ||||||
/// - `!Self::is_compatible(data_type)` | ||||||
pub fn try_new( | ||||||
data_type: DataType, | ||||||
values: ScalarBuffer<T::Native>, | ||||||
nulls: Option<NullBuffer>, | ||||||
) -> Result<Self, ArrowError> { | ||||||
if !Self::is_compatible(&data_type) { | ||||||
return Err(ArrowError::InvalidArgumentError(format!( | ||||||
"PrimitiveArray expected data type {} got {}", | ||||||
T::DATA_TYPE, | ||||||
data_type | ||||||
))); | ||||||
} | ||||||
|
||||||
if let Some(n) = nulls.as_ref() { | ||||||
assert_eq!(values.len(), n.len()); | ||||||
if n.len() != values.len() { | ||||||
return Err(ArrowError::InvalidArgumentError(format!( | ||||||
"Incorrect number of nulls for PrimitiveArray, expected {} got {}", | ||||||
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.
Suggested change
|
||||||
values.len(), | ||||||
n.len(), | ||||||
))); | ||||||
} | ||||||
} | ||||||
|
||||||
Self { | ||||||
Ok(Self { | ||||||
data_type, | ||||||
values, | ||||||
nulls, | ||||||
} | ||||||
}) | ||||||
} | ||||||
|
||||||
/// Deconstruct this array into its constituent parts | ||||||
pub fn into_parts(self) -> (DataType, ScalarBuffer<T::Native>, Option<NullBuffer>) { | ||||||
(self.data_type, self.values, self.nulls) | ||||||
} | ||||||
|
||||||
/// Asserts that `data_type` is compatible with `Self` | ||||||
|
@@ -2262,4 +2293,33 @@ mod tests { | |||||
let array = array.with_timezone("+02:00"); | ||||||
assert_eq!(array.timezone(), Some("+02:00")); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn test_try_new() { | ||||||
Int32Array::new(DataType::Int32, vec![1, 2, 3, 4].into(), None); | ||||||
Int32Array::new( | ||||||
DataType::Int32, | ||||||
vec![1, 2, 3, 4].into(), | ||||||
Some(NullBuffer::new_null(4)), | ||||||
); | ||||||
let err = Int32Array::try_new(DataType::Int64, vec![1, 2, 3, 4].into(), None) | ||||||
.unwrap_err(); | ||||||
|
||||||
assert_eq!( | ||||||
err.to_string(), | ||||||
"Invalid argument error: PrimitiveArray expected data type Int32 got Int64" | ||||||
); | ||||||
|
||||||
let err = Int32Array::try_new( | ||||||
DataType::Int32, | ||||||
vec![1, 2, 3, 4].into(), | ||||||
Some(NullBuffer::new_null(3)), | ||||||
) | ||||||
.unwrap_err(); | ||||||
|
||||||
assert_eq!( | ||||||
err.to_string(), | ||||||
"Invalid argument error: Incorrect number of nulls for PrimitiveArray, expected 4 got 3" | ||||||
); | ||||||
} | ||||||
} |
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.
Is this redundant with checks from ArrayData. Is it inevitable that we'll have redundancy between the checks?
I am thinking of https://docs.rs/arrow/latest/arrow/array/struct.ArrayData.html#method.validate and friends
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.
In the long-run we may be able to share code, but as the types of the buffers and the already validated constraints are different, I think some redundancy is inevitable. Fortunately most of this logic is fairly straightforward