tustvold commented on code in PR #4067:
URL: https://github.com/apache/arrow-rs/pull/4067#discussion_r1167473223


##########
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() {

Review Comment:
   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



-- 
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]

Reply via email to