Skip to content

Commit

Permalink
Add conversions between Buffer and ScalarBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jan 9, 2025
1 parent c762f39 commit 3933885
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/buffer/immutable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::{iter::FromIterator, ops::Deref, sync::Arc, usize};

use arrow_buffer::ArrowNativeType;

Check failure on line 3 in src/buffer/immutable.rs

View workflow job for this annotation

GitHub Actions / cross (wasm32-unknown-unknown)

unresolved import `arrow_buffer`
use either::Either;

use crate::types::NativeType;

use super::Bytes;
use super::IntoIter;

Expand Down Expand Up @@ -324,3 +327,39 @@ impl<T: crate::types::NativeType> From<Buffer<T>> for arrow_buffer::Buffer {
)
}
}

/// arrow2 Buffer -> arrow1 ScalarBuffer
#[cfg(feature = "arrow")]
impl<T> From<Buffer<T>> for arrow_buffer::ScalarBuffer<T>
where
T: crate::types::NativeType + arrow_buffer::ArrowNativeType,
{
fn from(array: Buffer<T>) -> Self {
let len = array.len();
Self::new(array.into(), 0, len)
}
}

/// arrow1 ScalarBuffer -> arrow2 Buffer
#[cfg(feature = "arrow")]
impl<T> From<arrow_buffer::ScalarBuffer<T>> for Buffer<T>
where
T: crate::types::NativeType + arrow_buffer::ArrowNativeType,
{
fn from(array: arrow_buffer::ScalarBuffer<T>) -> Self {
Self::from_bytes(crate::buffer::to_bytes(array.into()))
}
}

#[cfg(feature = "arrow")]
#[test]
fn test_scalar_buffer_arrow_conversion() {
let original = Buffer::<f64>::from(vec![0.0, 1.0, 2.0, 3.0]).sliced(1, 2);
assert_eq!(original.as_slice(), [1.0, 2.0]);

let arrow: arrow_buffer::ScalarBuffer<f64> = original.clone().into();
assert_eq!(arrow.as_ref(), [1.0, 2.0]);

let roundtrip: Buffer<f64> = arrow.into();
assert_eq!(roundtrip, original);
}

0 comments on commit 3933885

Please sign in to comment.