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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to