-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
arrow-rs
interop to arrow
module
- Loading branch information
Showing
13 changed files
with
216 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use crate::{ | ||
array::FixedSizePrimitiveArray, arrow::buffer::ArrowBuffer, bitmap::Bitmap, buffer::BufferType, | ||
FixedSize, Length, | ||
}; | ||
use arrow_array::{types::ArrowPrimitiveType, PrimitiveArray}; | ||
use arrow_buffer::{BooleanBuffer, NullBuffer, ScalarBuffer}; | ||
|
||
impl<T: FixedSize, U: ArrowPrimitiveType<Native = T>, Buffer: BufferType> | ||
From<FixedSizePrimitiveArray<T, false, Buffer>> for PrimitiveArray<U> | ||
where | ||
<Buffer as BufferType>::Buffer<T>: Length + Into<<ArrowBuffer as BufferType>::Buffer<T>>, | ||
{ | ||
fn from(value: FixedSizePrimitiveArray<T, false, Buffer>) -> Self { | ||
let len = value.len(); | ||
Self::new(ScalarBuffer::new(value.0.into().finish(), 0, len), None) | ||
} | ||
} | ||
|
||
impl<T: FixedSize, U: ArrowPrimitiveType<Native = T>, Buffer: BufferType> | ||
From<FixedSizePrimitiveArray<T, true, Buffer>> for PrimitiveArray<U> | ||
where | ||
<Buffer as BufferType>::Buffer<T>: Length + Into<<ArrowBuffer as BufferType>::Buffer<T>>, | ||
Bitmap<Buffer>: Into<BooleanBuffer>, | ||
{ | ||
fn from(value: FixedSizePrimitiveArray<T, true, Buffer>) -> Self { | ||
let len = value.len(); | ||
Self::new( | ||
ScalarBuffer::new(value.0.data.into().finish(), 0, len), | ||
Some(NullBuffer::new(value.0.validity.into())), | ||
) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::array::Int8Array; | ||
|
||
#[test] | ||
#[cfg(feature = "arrow-array")] | ||
fn arrow_array() { | ||
use crate::{array::Int8Array, bitmap::ValidityBitmap}; | ||
use arrow_array::{types::Int8Type, Array, PrimitiveArray}; | ||
|
||
let input = [1, 2, 3, 4]; | ||
let array = input.into_iter().collect::<Int8Array<false, ArrowBuffer>>(); | ||
let array = PrimitiveArray::<Int8Type>::from(array); | ||
assert_eq!(array.len(), 4); | ||
|
||
let input = [1, 2, 3, 4]; | ||
let array = input.into_iter().collect::<Int8Array<false>>(); | ||
let array = PrimitiveArray::<Int8Type>::from(array); | ||
assert_eq!(array.len(), 4); | ||
|
||
let input = [Some(1), None, Some(3), Some(4)]; | ||
let array = input.into_iter().collect::<Int8Array<true, ArrowBuffer>>(); | ||
assert_eq!(array.null_count(), 1); | ||
let array = PrimitiveArray::<Int8Type>::from(array); | ||
assert_eq!(array.len(), 4); | ||
assert_eq!(array.null_count(), 1); | ||
} | ||
|
||
#[test] | ||
fn arrow_buffer() { | ||
let input = [1, 2, 3, 4]; | ||
let mut array = input.into_iter().collect::<Int8Array<false, ArrowBuffer>>(); | ||
assert_eq!(array.len(), 4); | ||
// Use arrow_buffer | ||
array.0.append_n(5, 5); | ||
assert_eq!(array.0.as_slice(), &[1, 2, 3, 4, 5, 5, 5, 5, 5]); | ||
|
||
let input = [Some(1), None, Some(3), Some(4)]; | ||
let array = input.into_iter().collect::<Int8Array<true, ArrowBuffer>>(); | ||
assert_eq!(array.len(), 4); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod fixed_size_primitive; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use super::buffer::ArrowBuffer; | ||
use crate::{bitmap::Bitmap, buffer::BufferType}; | ||
use arrow_buffer::BooleanBuffer; | ||
|
||
impl<Buffer: BufferType> From<Bitmap<Buffer>> for BooleanBuffer | ||
where | ||
<Buffer as BufferType>::Buffer<u8>: Into<<ArrowBuffer as BufferType>::Buffer<u8>>, | ||
{ | ||
fn from(value: Bitmap<Buffer>) -> Self { | ||
BooleanBuffer::new(value.buffer.into().finish(), value.offset, value.bits) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::Length; | ||
|
||
#[test] | ||
fn arrow_buffer() { | ||
let input = vec![true, false, true]; | ||
let bitmap = input.into_iter().collect::<Bitmap<ArrowBuffer>>(); | ||
assert_eq!(bitmap.len(), 3); | ||
|
||
let input = vec![true, false, true]; | ||
let bitmap = input.into_iter().collect::<Bitmap<ArrowBuffer>>(); | ||
assert_eq!(bitmap.len(), 3); | ||
assert_eq!(bitmap.into_iter().collect::<Vec<_>>(), [true, false, true]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use crate::buffer::{Buffer, BufferMut, BufferType}; | ||
use crate::FixedSize; | ||
use arrow_buffer::{ArrowNativeType, BufferBuilder, ScalarBuffer}; | ||
|
||
/// A [BufferType] implementation for [arrow_buffer::BufferBuilder]. | ||
pub struct ArrowBuffer; | ||
|
||
impl BufferType for ArrowBuffer { | ||
type Buffer<T: FixedSize> = BufferBuilder<T>; | ||
} | ||
|
||
impl<T: FixedSize + ArrowNativeType> Buffer<T> for BufferBuilder<T> { | ||
fn as_slice(&self) -> &[T] { | ||
BufferBuilder::as_slice(self) | ||
} | ||
} | ||
|
||
impl<T: FixedSize + ArrowNativeType> BufferMut<T> for BufferBuilder<T> { | ||
fn as_mut_slice(&mut self) -> &mut [T] { | ||
BufferBuilder::as_slice_mut(self) | ||
} | ||
} | ||
|
||
/// A [BufferType] implementation for [arrow_buffer::ScalarBuffer]. | ||
pub struct ArrowScalarBuffer; | ||
|
||
impl BufferType for ArrowScalarBuffer { | ||
type Buffer<T: FixedSize> = ScalarBuffer<T>; | ||
} | ||
|
||
impl<T: FixedSize + ArrowNativeType> Buffer<T> for ScalarBuffer<T> { | ||
fn as_slice(&self) -> &[T] { | ||
self | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn arrow() { | ||
let buffer = arrow_buffer::BufferBuilder::from_iter([1, 2, 3, 4]); | ||
assert_eq!(<_ as Buffer<u32>>::as_slice(&buffer), &[1, 2, 3, 4]); | ||
|
||
let mut buffer = arrow_buffer::BufferBuilder::from_iter([1u64, 2, 3, 4]); | ||
assert_eq!( | ||
<_ as BufferMut<u64>>::as_mut_slice(&mut buffer), | ||
&[1, 2, 3, 4] | ||
); | ||
<_ as BufferMut<u64>>::as_mut_slice(&mut buffer)[3] = 42; | ||
assert_eq!(<_ as Buffer<u64>>::as_slice(&buffer), &[1, 2, 3, 42]); | ||
|
||
let buffer = arrow_buffer::ScalarBuffer::from_iter([1, 2, 3, 4]); | ||
assert_eq!(<_ as Buffer<u32>>::as_slice(&buffer), &[1, 2, 3, 4]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use crate::Length; | ||
use arrow_buffer::{ArrowNativeType, BufferBuilder, ScalarBuffer}; | ||
|
||
impl<T: ArrowNativeType> Length for BufferBuilder<T> { | ||
fn len(&self) -> usize { | ||
BufferBuilder::len(self) | ||
} | ||
} | ||
|
||
impl<T: ArrowNativeType> Length for ScalarBuffer<T> { | ||
fn len(&self) -> usize { | ||
self.as_ref().len() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#[cfg(feature = "arrow-array")] | ||
pub mod array; | ||
|
||
#[cfg(feature = "arrow-buffer")] | ||
pub mod bitmap; | ||
|
||
#[cfg(feature = "arrow-buffer")] | ||
pub mod buffer; | ||
|
||
#[cfg(feature = "arrow-buffer")] | ||
pub mod length; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.