Skip to content

Apache DataFusion SQL Query Engine

ARROW-2382: [Rust] Bug fix: List was not using aligned mem

Andy Grovef1f0add927f5
diff --git a/rust/src/buffer.rs b/rust/src/buffer.rs
index 72b2a27d89..6c4f1c7fd9 100644
--- a/rust/src/buffer.rs
+++ b/rust/src/buffer.rs
@@ -15,6 +15,7 @@
// specific language governing permissions and limitations
// under the License.
+use bytes::Bytes;
use libc;
use std::mem;
use std::slice;
@@ -27,10 +28,6 @@ pub struct Buffer<T> {
}
impl<T> Buffer<T> {
- pub fn new(data: *const T, len: i32) -> Self {
- Buffer { data, len }
- }
-
pub fn len(&self) -> i32 {
self.len
}
@@ -40,6 +37,9 @@ impl<T> Buffer<T> {
}
pub fn slice(&self, start: usize, end: usize) -> &[T] {
+ assert!(start <= end);
+ assert!(start < self.len as usize);
+ assert!(end <= self.len as usize);
unsafe { slice::from_raw_parts(self.data.offset(start as isize), (end - start) as usize) }
}
@@ -128,6 +128,27 @@ array_from_primitive!(i16);
array_from_primitive!(i32);
array_from_primitive!(i64);
+impl From<Bytes> for Buffer<u8> {
+ fn from(bytes: Bytes) -> Self {
+ // allocate aligned
+ let len = bytes.len();
+ let sz = mem::size_of::<u8>();
+ let buf_mem = allocate_aligned((len * sz) as i64).unwrap();
+ Buffer {
+ len: len as i32,
+ data: unsafe {
+ let dst = mem::transmute::<*const u8, *mut libc::c_void>(buf_mem);
+ libc::memcpy(
+ dst,
+ mem::transmute::<*const u8, *const libc::c_void>(bytes.as_ptr()),
+ len * sz,
+ );
+ mem::transmute::<*mut libc::c_void, *const u8>(dst)
+ },
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/rust/src/list.rs b/rust/src/list.rs
index aee8c763aa..461f8e6459 100644
--- a/rust/src/list.rs
+++ b/rust/src/list.rs
@@ -47,11 +47,8 @@ impl From<Vec<String>> for List<u8> {
buf.put(s.as_bytes());
offsets.push(buf.len() as i32);
});
- let bytes = buf.freeze();
- let buffer = Buffer::new(bytes.as_ptr(), bytes.len() as i32);
-
List {
- data: buffer,
+ data: Buffer::from(buf.freeze()),
offsets: Buffer::from(offsets),
}
}