Jimexist commented on a change in pull request #1589:
URL: https://github.com/apache/arrow-datafusion/pull/1589#discussion_r785977125
##########
File path: datafusion/src/from_slice.rs
##########
@@ -19,27 +19,119 @@
//!
//! This file essentially exists to ease the transition onto arrow2
-use arrow::array::{ArrayData, PrimitiveArray};
-use arrow::buffer::Buffer;
-use arrow::datatypes::ArrowPrimitiveType;
+use arrow::array::{
+ ArrayData, BinaryOffsetSizeTrait, BooleanArray, GenericBinaryArray,
+ GenericStringArray, PrimitiveArray, StringOffsetSizeTrait,
+};
+use arrow::buffer::{Buffer, MutableBuffer};
+use arrow::datatypes::{ArrowPrimitiveType, DataType};
+use arrow::util::bit_util;
/// A trait to define from_slice functions for arrow primitive array types
-pub trait FromSlice<T>
+pub trait FromSlice<S, E>
where
- T: ArrowPrimitiveType,
+ S: AsRef<[E]>,
{
/// convert a slice of native types into a primitive array (without nulls)
- fn from_slice(slice: &[T::Native]) -> PrimitiveArray<T>;
+ fn from_slice(slice: S) -> Self;
}
-/// default implementation for primitive types
-// #[cfg(test)]
-impl<T: ArrowPrimitiveType> FromSlice<T> for PrimitiveArray<T> {
- fn from_slice(slice: &[T::Native]) -> PrimitiveArray<T> {
+/// default implementation for primitive array types, adapted from
`From<Vec<_>>`
+impl<S, T> FromSlice<S, T::Native> for PrimitiveArray<T>
+where
+ T: ArrowPrimitiveType,
+ S: AsRef<[T::Native]>,
+{
+ fn from_slice(slice: S) -> Self {
+ let slice = slice.as_ref();
let array_data = ArrayData::builder(T::DATA_TYPE)
.len(slice.len())
.add_buffer(Buffer::from_slice_ref(&slice));
let array_data = unsafe { array_data.build_unchecked() };
- PrimitiveArray::<T>::from(array_data)
+ Self::from(array_data)
+ }
+}
+
+/// default implementation for binary array types, adapted from `From<Vec<_>>`
+impl<S, I, OffsetSize> FromSlice<S, I> for GenericBinaryArray<OffsetSize>
+where
+ OffsetSize: BinaryOffsetSizeTrait,
+ S: AsRef<[I]>,
+ I: AsRef<[u8]>,
+{
+ fn from_slice(slice: S) -> Self {
+ let slice = slice.as_ref();
Review comment:
i guess this change is necessary:
- https://github.com/apache/arrow-rs/pull/1188
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]