viirya commented on code in PR #4067:
URL: https://github.com/apache/arrow-rs/pull/4067#discussion_r1167581580
##########
arrow-array/src/array/primitive_array.rs:
##########
@@ -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 {}",
Review Comment:
`number of nulls` could be confused.
```suggestion
"Incorrect length of null buffer for PrimitiveArray,
expected {} got {}",
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]