You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by al...@apache.org on 2022/04/25 20:27:35 UTC

[arrow-rs] branch master updated: Don't access and validate offset buffer in ListArray::from(ArrayData) (#1602)

This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new 8ecccfbd8 Don't access and validate offset buffer in ListArray::from(ArrayData) (#1602)
8ecccfbd8 is described below

commit 8ecccfbd85515ccba3f98ebe06d73aca4da7fc15
Author: Jörn Horstmann <gi...@jhorstmann.net>
AuthorDate: Mon Apr 25 22:27:30 2022 +0200

    Don't access and validate offset buffer in ListArray::from(ArrayData) (#1602)
    
    * Don't access and validate offset buffer in ListArray::from(ArrayData)
    
    * Simplify empty buffer creation
    
    * fix clippy
    
    Co-authored-by: Andrew Lamb <an...@nerdnetworks.org>
---
 arrow/src/array/array_list.rs | 43 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/arrow/src/array/array_list.rs b/arrow/src/array/array_list.rs
index c1d948b92..acfc8e932 100644
--- a/arrow/src/array/array_list.rs
+++ b/arrow/src/array/array_list.rs
@@ -239,15 +239,7 @@ impl<OffsetSize: OffsetSizeTrait> GenericListArray<OffsetSize> {
 
         let values = make_array(values);
         let value_offsets = data.buffers()[0].as_ptr();
-
         let value_offsets = unsafe { RawPtrBox::<OffsetSize>::new(value_offsets) };
-        unsafe {
-            if !(*value_offsets.as_ptr().offset(0)).is_zero() {
-                return Err(ArrowError::InvalidArgumentError(String::from(
-                    "offsets do not start at zero",
-                )));
-            }
-        }
         Ok(Self {
             data,
             values,
@@ -524,6 +516,32 @@ mod tests {
         assert_eq!(list_array, another)
     }
 
+    #[test]
+    fn test_empty_list_array() {
+        // Construct an empty value array
+        let value_data = ArrayData::builder(DataType::Int32)
+            .len(0)
+            .add_buffer(Buffer::from([]))
+            .build()
+            .unwrap();
+
+        // Construct an empty offset buffer
+        let value_offsets = Buffer::from([]);
+
+        // Construct a list array from the above two
+        let list_data_type =
+            DataType::List(Box::new(Field::new("item", DataType::Int32, false)));
+        let list_data = ArrayData::builder(list_data_type)
+            .len(0)
+            .add_buffer(value_offsets)
+            .add_child_data(value_data)
+            .build()
+            .unwrap();
+
+        let list_array = ListArray::from(list_data);
+        assert_eq!(list_array.len(), 0)
+    }
+
     #[test]
     fn test_list_array() {
         // Construct a value array
@@ -1110,8 +1128,7 @@ mod tests {
     }
 
     #[test]
-    #[should_panic(expected = "offsets do not start at zero")]
-    fn test_list_array_invalid_value_offset_start() {
+    fn test_list_array_offsets_need_not_start_at_zero() {
         let value_data = ArrayData::builder(DataType::Int32)
             .len(8)
             .add_buffer(Buffer::from_slice_ref(&[0, 1, 2, 3, 4, 5, 6, 7]))
@@ -1128,7 +1145,11 @@ mod tests {
             .add_child_data(value_data)
             .build()
             .unwrap();
-        drop(ListArray::from(list_data));
+
+        let list_array = ListArray::from(list_data);
+        assert_eq!(list_array.value_length(0), 0);
+        assert_eq!(list_array.value_length(1), 3);
+        assert_eq!(list_array.value_length(2), 2);
     }
 
     #[test]