You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by vi...@apache.org on 2023/06/27 07:05:04 UTC

[arrow-rs] branch master updated: Revise error message in From for ScalarBuffer (#4446)

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

viirya 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 7b6896fee Revise error message in From<Buffer> for ScalarBuffer (#4446)
7b6896fee is described below

commit 7b6896feecabfad97899fe8562f3f098608fbdde
Author: Liang-Chi Hsieh <vi...@gmail.com>
AuthorDate: Tue Jun 27 00:04:58 2023 -0700

    Revise error message in From<Buffer> for ScalarBuffer (#4446)
    
    * Revise error message in From<Buffer> for ScalaBuffer
    
    * Update arrow-buffer/src/buffer/scalar.rs
    
    Co-authored-by: Raphael Taylor-Davies <17...@users.noreply.github.com>
    
    * Fix tests
    
    ---------
    
    Co-authored-by: Raphael Taylor-Davies <17...@users.noreply.github.com>
---
 arrow-array/src/array/list_array.rs  |  8 ++++++--
 arrow-buffer/src/buffer/immutable.rs |  4 ++++
 arrow-buffer/src/buffer/scalar.rs    | 20 ++++++++++++++------
 3 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/arrow-array/src/array/list_array.rs b/arrow-array/src/array/list_array.rs
index 2205d846e..0c1fea6f4 100644
--- a/arrow-array/src/array/list_array.rs
+++ b/arrow-array/src/array/list_array.rs
@@ -968,7 +968,9 @@ mod tests {
     }
 
     #[test]
-    #[should_panic(expected = "memory is not aligned")]
+    #[should_panic(
+        expected = "Memory pointer is not aligned with the specified scalar type"
+    )]
     fn test_primitive_array_alignment() {
         let buf = Buffer::from_slice_ref([0_u64]);
         let buf2 = buf.slice(1);
@@ -980,7 +982,9 @@ mod tests {
     }
 
     #[test]
-    #[should_panic(expected = "memory is not aligned")]
+    #[should_panic(
+        expected = "Memory pointer is not aligned with the specified scalar type"
+    )]
     // Different error messages, so skip for now
     // https://github.com/apache/arrow-rs/issues/1545
     #[cfg(not(feature = "force_validate"))]
diff --git a/arrow-buffer/src/buffer/immutable.rs b/arrow-buffer/src/buffer/immutable.rs
index a4ab64b84..2ecd3b419 100644
--- a/arrow-buffer/src/buffer/immutable.rs
+++ b/arrow-buffer/src/buffer/immutable.rs
@@ -165,6 +165,10 @@ impl Buffer {
         unsafe { std::slice::from_raw_parts(self.ptr, self.length) }
     }
 
+    pub(crate) fn deallocation(&self) -> &Deallocation {
+        self.data.deallocation()
+    }
+
     /// Returns a new [Buffer] that is a slice of this buffer starting at `offset`.
     /// Doing so allows the same memory region to be shared between buffers.
     /// # Panics
diff --git a/arrow-buffer/src/buffer/scalar.rs b/arrow-buffer/src/buffer/scalar.rs
index 920463b36..70c86f118 100644
--- a/arrow-buffer/src/buffer/scalar.rs
+++ b/arrow-buffer/src/buffer/scalar.rs
@@ -15,6 +15,7 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use crate::alloc::Deallocation;
 use crate::buffer::Buffer;
 use crate::native::ArrowNativeType;
 use crate::MutableBuffer;
@@ -118,11 +119,16 @@ impl<T: ArrowNativeType> From<MutableBuffer> for ScalarBuffer<T> {
 impl<T: ArrowNativeType> From<Buffer> for ScalarBuffer<T> {
     fn from(buffer: Buffer) -> Self {
         let align = std::mem::align_of::<T>();
-        assert_eq!(
-            buffer.as_ptr().align_offset(align),
-            0,
-            "memory is not aligned"
-        );
+        let is_aligned = buffer.as_ptr().align_offset(align) == 0;
+
+        match buffer.deallocation() {
+            Deallocation::Standard(_) => assert!(
+                is_aligned,
+                "Memory pointer is not aligned with the specified scalar type"
+            ),
+            Deallocation::Custom(_) =>
+                assert!(is_aligned, "Memory pointer from external source (e.g, FFI) is not aligned with the specified scalar type. Before importing buffer through FFI, please make sure the allocation is aligned."),
+        }
 
         Self {
             buffer,
@@ -207,7 +213,9 @@ mod tests {
     }
 
     #[test]
-    #[should_panic(expected = "memory is not aligned")]
+    #[should_panic(
+        expected = "Memory pointer is not aligned with the specified scalar type"
+    )]
     fn test_unaligned() {
         let expected = [0_i32, 1, 2];
         let buffer = Buffer::from_iter(expected.iter().cloned());