Skip to content
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 3 commits into from
Apr 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 67 additions & 7 deletions arrow-array/src/array/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Copy link
Contributor

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

Copy link
Contributor Author

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

return Err(ArrowError::InvalidArgumentError(format!(
"Incorrect length of null buffer for PrimitiveArray, expected {} got {}",
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`
Expand Down Expand Up @@ -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 length of null buffer for PrimitiveArray, expected 4 got 3"
);
}
}