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 14:52:24 UTC

[GitHub] [arrow-rs] tustvold opened a new pull request, #4065: Add ListArray constructors (#3879)

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

   _Draft as builds on #4061_
   
   # 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.
   -->
   
   Adds constructors for ListArray
   
   # 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 merged pull request #4065: Add ListArray Constructors (#3879)

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


-- 
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 #4065: Add ListArray Constructors (#3879)

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


##########
arrow-buffer/src/buffer/offset.rs:
##########
@@ -29,10 +29,13 @@ impl<O: ArrowNativeType> OffsetBuffer<O> {
     /// # Panics
     ///
     /// Panics if `buffer` is not a non-empty buffer containing
-    /// monotonically increasing values greater than zero
+    /// monotonically increasing values greater than or equal to zero
     pub fn new(buffer: ScalarBuffer<O>) -> Self {
         assert!(!buffer.is_empty(), "offsets cannot be empty");
-        assert!(buffer[0] > O::usize_as(0), "offsets must be greater than 0");
+        assert!(

Review Comment:
   is this covered anywhere with tests?



##########
arrow-array/src/array/list_array.rs:
##########
@@ -73,13 +74,102 @@ impl<OffsetSize: OffsetSizeTrait> GenericListArray<OffsetSize> {
     /// The data type constructor of list array.
     /// The input is the schema of the child array and
     /// the output is the [`DataType`], List or LargeList.
-    pub const DATA_TYPE_CONSTRUCTOR: fn(Arc<Field>) -> DataType = if OffsetSize::IS_LARGE
-    {
+    pub const DATA_TYPE_CONSTRUCTOR: fn(FieldRef) -> DataType = if OffsetSize::IS_LARGE {
         DataType::LargeList
     } else {
         DataType::List
     };
 
+    /// Create a new [`GenericListArray`] from the provided parts
+    ///
+    /// # Errors
+    ///
+    /// Errors if
+    ///
+    /// * `offsets.len() - 1 != nulls.len()`
+    /// * `offsets.last() > values.len()`
+    /// * `!field.is_nullable() && values.null_count() != 0`
+    pub fn try_new(
+        field: FieldRef,
+        offsets: OffsetBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Result<Self, ArrowError> {
+        let len = offsets.len() - 1; // Offsets guaranteed to not be empty
+        let end_offset = offsets.last().unwrap().as_usize();
+        if end_offset > values.len() {

Review Comment:
   we don't need to check all elements here because that is checked during the construction of `OffsetsBuffer`, right?



##########
arrow-array/src/array/list_array.rs:
##########
@@ -73,13 +74,102 @@ impl<OffsetSize: OffsetSizeTrait> GenericListArray<OffsetSize> {
     /// The data type constructor of list array.
     /// The input is the schema of the child array and
     /// the output is the [`DataType`], List or LargeList.
-    pub const DATA_TYPE_CONSTRUCTOR: fn(Arc<Field>) -> DataType = if OffsetSize::IS_LARGE
-    {
+    pub const DATA_TYPE_CONSTRUCTOR: fn(FieldRef) -> DataType = if OffsetSize::IS_LARGE {
         DataType::LargeList
     } else {
         DataType::List
     };
 
+    /// Create a new [`GenericListArray`] from the provided parts
+    ///
+    /// # Errors
+    ///
+    /// Errors if
+    ///
+    /// * `offsets.len() - 1 != nulls.len()`
+    /// * `offsets.last() > values.len()`
+    /// * `!field.is_nullable() && values.null_count() != 0`
+    pub fn try_new(
+        field: FieldRef,
+        offsets: OffsetBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Result<Self, ArrowError> {
+        let len = offsets.len() - 1; // Offsets guaranteed to not be empty
+        let end_offset = offsets.last().unwrap().as_usize();
+        if end_offset > values.len() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "Max offset of {end_offset} exceeds length of values {}",
+                values.len()
+            )));
+        }
+
+        if let Some(n) = nulls.as_ref() {
+            if n.len() != len {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Incorrect number of nulls for {}ListArray, expected {len} got {}",
+                    OffsetSize::PREFIX,
+                    n.len(),
+                )));
+            }
+        }
+        if !field.is_nullable() && values.null_count() != 0 {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "Non-nullable field of {}ListArray {:?} cannot contain nulls",

Review Comment:
   ```suggestion
                   "Non-nullable field of {} ListArray {:?} cannot contain nulls",
   ```
   Typo maybe?



-- 
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 #4065: Add ListArray Constructors (#3879)

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


##########
arrow-array/src/array/list_array.rs:
##########
@@ -73,13 +74,102 @@ impl<OffsetSize: OffsetSizeTrait> GenericListArray<OffsetSize> {
     /// The data type constructor of list array.
     /// The input is the schema of the child array and
     /// the output is the [`DataType`], List or LargeList.
-    pub const DATA_TYPE_CONSTRUCTOR: fn(Arc<Field>) -> DataType = if OffsetSize::IS_LARGE
-    {
+    pub const DATA_TYPE_CONSTRUCTOR: fn(FieldRef) -> DataType = if OffsetSize::IS_LARGE {
         DataType::LargeList
     } else {
         DataType::List
     };
 
+    /// Create a new [`GenericListArray`] from the provided parts
+    ///
+    /// # Errors
+    ///
+    /// Errors if
+    ///
+    /// * `offsets.len() - 1 != nulls.len()`
+    /// * `offsets.last() > values.len()`
+    /// * `!field.is_nullable() && values.null_count() != 0`
+    pub fn try_new(
+        field: FieldRef,
+        offsets: OffsetBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Result<Self, ArrowError> {
+        let len = offsets.len() - 1; // Offsets guaranteed to not be empty
+        let end_offset = offsets.last().unwrap().as_usize();
+        if end_offset > values.len() {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "Max offset of {end_offset} exceeds length of values {}",
+                values.len()
+            )));
+        }
+
+        if let Some(n) = nulls.as_ref() {
+            if n.len() != len {
+                return Err(ArrowError::InvalidArgumentError(format!(
+                    "Incorrect number of nulls for {}ListArray, expected {len} got {}",
+                    OffsetSize::PREFIX,
+                    n.len(),
+                )));
+            }
+        }
+        if !field.is_nullable() && values.null_count() != 0 {
+            return Err(ArrowError::InvalidArgumentError(format!(
+                "Non-nullable field of {}ListArray {:?} cannot contain nulls",

Review Comment:
   No, this is used to render `LargeListArray`



-- 
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 #4065: Add ListArray Constructors (#3879)

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


##########
arrow-buffer/src/buffer/offset.rs:
##########
@@ -104,6 +113,11 @@ mod tests {
         OffsetBuffer::new(vec![-1, 0, 1].into());
     }
 
+    #[test]
+    fn offsets() {
+        OffsetBuffer::new(vec![0, 1, 2, 3].into());

Review Comment:
   It is kinda embarrassing that this slipped through :sweat_smile: 



-- 
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 #4065: Add ListArray Constructors (#3879)

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


##########
arrow-buffer/src/buffer/offset.rs:
##########
@@ -104,6 +113,11 @@ mod tests {
         OffsetBuffer::new(vec![-1, 0, 1].into());
     }
 
+    #[test]
+    fn offsets() {
+        OffsetBuffer::new(vec![0, 1, 2, 3].into());

Review Comment:
   This is kinda embarrassing :sweat_smile: 



-- 
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 #4065: Add ListArray Constructors (#3879)

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


##########
arrow-array/src/array/list_array.rs:
##########
@@ -73,13 +74,102 @@ impl<OffsetSize: OffsetSizeTrait> GenericListArray<OffsetSize> {
     /// The data type constructor of list array.
     /// The input is the schema of the child array and
     /// the output is the [`DataType`], List or LargeList.
-    pub const DATA_TYPE_CONSTRUCTOR: fn(Arc<Field>) -> DataType = if OffsetSize::IS_LARGE
-    {
+    pub const DATA_TYPE_CONSTRUCTOR: fn(FieldRef) -> DataType = if OffsetSize::IS_LARGE {
         DataType::LargeList
     } else {
         DataType::List
     };
 
+    /// Create a new [`GenericListArray`] from the provided parts
+    ///
+    /// # Errors
+    ///
+    /// Errors if
+    ///
+    /// * `offsets.len() - 1 != nulls.len()`
+    /// * `offsets.last() > values.len()`
+    /// * `!field.is_nullable() && values.null_count() != 0`
+    pub fn try_new(
+        field: FieldRef,
+        offsets: OffsetBuffer<OffsetSize>,
+        values: ArrayRef,
+        nulls: Option<NullBuffer>,
+    ) -> Result<Self, ArrowError> {
+        let len = offsets.len() - 1; // Offsets guaranteed to not be empty
+        let end_offset = offsets.last().unwrap().as_usize();
+        if end_offset > values.len() {

Review Comment:
   ```suggestion
           // don't need to check other values of `offsets` because they are checked
           // during construction of `OffsetsbBuffer`
           if end_offset > values.len() {
   ```



-- 
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 #4065: Add ListArray Constructors (#3879)

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


##########
arrow-buffer/src/buffer/offset.rs:
##########
@@ -29,10 +29,13 @@ impl<O: ArrowNativeType> OffsetBuffer<O> {
     /// # Panics
     ///
     /// Panics if `buffer` is not a non-empty buffer containing
-    /// monotonically increasing values greater than zero
+    /// monotonically increasing values greater than or equal to zero
     pub fn new(buffer: ScalarBuffer<O>) -> Self {
         assert!(!buffer.is_empty(), "offsets cannot be empty");
-        assert!(buffer[0] > O::usize_as(0), "offsets must be greater than 0");
+        assert!(

Review Comment:
   Yes https://github.com/apache/arrow-rs/pull/4065/files#diff-6e2926e22029402309ee910c3a79ce67f5bae87f1b6aa27a57a9541a074d319aL103



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