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

[GitHub] [arrow-rs] tustvold opened a new pull request, #4067: Add PrimitiveArray::try_new (#3879)

tustvold opened a new pull request, #4067:
URL: https://github.com/apache/arrow-rs/pull/4067

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Part of #3879
   
   # Rationale for this change
    
   <!--
   Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
   Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   -->
   
   Some codebases may want to avoid panicking, and whilst for PrimitiveArray the necessary checks are pretty inconsequential, for consistency we should provide a try_new method for all arrays.
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are there any user-facing changes?
   
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!---
   If there are any breaking changes to public APIs, please add the `breaking change` label.
   -->
   


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


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

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
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: github-unsubscribe@arrow.apache.org

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


[GitHub] [arrow-rs] tustvold merged pull request #4067: Add PrimitiveArray::try_new (#3879)

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold merged PR #4067:
URL: https://github.com/apache/arrow-rs/pull/4067


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


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

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #4067:
URL: https://github.com/apache/arrow-rs/pull/4067#discussion_r1167258767


##########
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:
   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



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


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

Posted by "viirya (via GitHub)" <gi...@apache.org>.
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