You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "viirya (via GitHub)" <gi...@apache.org> on 2023/04/15 16:58:12 UTC

[GitHub] [arrow-rs] viirya commented on a diff in pull request #4067: Add PrimitiveArray::try_new (#3879)

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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org