From 1f869af80ae51a41e5eee99242f4ff85680ad7b2 Mon Sep 17 00:00:00 2001 From: Raphael Taylor-Davies Date: Wed, 12 Apr 2023 16:39:08 +0100 Subject: [PATCH 1/3] Add PrimitiveArray::try_new (#3879) --- arrow-array/src/array/primitive_array.rs | 45 ++++++++++++++++++++---- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/arrow-array/src/array/primitive_array.rs b/arrow-array/src/array/primitive_array.rs index 3199104382a6..7b50778d80ac 100644 --- a/arrow-array/src/array/primitive_array.rs +++ b/arrow-array/src/array/primitive_array.rs @@ -269,24 +269,55 @@ impl PrimitiveArray { /// /// # 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, nulls: Option, ) -> 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, + nulls: Option, + ) -> Result { + 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 {}", + 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, Option) { + (self.data_type, self.values, self.nulls) } /// Asserts that `data_type` is compatible with `Self` From c297b24e23f6fac15d352ba5689a03b82185eef5 Mon Sep 17 00:00:00 2001 From: Raphael Taylor-Davies Date: Sat, 15 Apr 2023 07:46:48 -0400 Subject: [PATCH 2/3] Add tests --- arrow-array/src/array/primitive_array.rs | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/arrow-array/src/array/primitive_array.rs b/arrow-array/src/array/primitive_array.rs index 7b50778d80ac..9ea009a28f52 100644 --- a/arrow-array/src/array/primitive_array.rs +++ b/arrow-array/src/array/primitive_array.rs @@ -2293,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" + ); + } } From 32e01a6e86317aed8ea4df7d5ada223a206eef66 Mon Sep 17 00:00:00 2001 From: Raphael Taylor-Davies Date: Tue, 18 Apr 2023 07:53:29 -0400 Subject: [PATCH 3/3] Review feedback --- arrow-array/src/array/primitive_array.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arrow-array/src/array/primitive_array.rs b/arrow-array/src/array/primitive_array.rs index 9ea009a28f52..febafcc6f02a 100644 --- a/arrow-array/src/array/primitive_array.rs +++ b/arrow-array/src/array/primitive_array.rs @@ -301,7 +301,7 @@ impl PrimitiveArray { if let Some(n) = nulls.as_ref() { if n.len() != values.len() { return Err(ArrowError::InvalidArgumentError(format!( - "Incorrect number of nulls for PrimitiveArray, expected {} got {}", + "Incorrect length of null buffer for PrimitiveArray, expected {} got {}", values.len(), n.len(), ))); @@ -2319,7 +2319,7 @@ mod tests { assert_eq!( err.to_string(), - "Invalid argument error: Incorrect number of nulls for PrimitiveArray, expected 4 got 3" + "Invalid argument error: Incorrect length of null buffer for PrimitiveArray, expected 4 got 3" ); } }