Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Allow to create primitive array by vec without extra memcopy (#710)
Browse files Browse the repository at this point in the history
  • Loading branch information
sundy-li authored Dec 25, 2021
1 parent dfa6370 commit 87a08f4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
6 changes: 3 additions & 3 deletions examples/growable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use arrow2::array::PrimitiveArray;

fn main() {
// say we have two sorted arrays
let array0 = PrimitiveArray::<i64>::from_slice(&[1, 2, 5]);
let array1 = PrimitiveArray::<i64>::from_slice(&[3, 4, 6]);
let array0 = PrimitiveArray::<i64>::from_vec(vec![1, 2, 5]);
let array1 = PrimitiveArray::<i64>::from_vec(vec![3, 4, 6]);

// and we found a way to compute the slices that sort them:
// (array_index, start of the slice, length of the slice)
Expand Down Expand Up @@ -33,6 +33,6 @@ fn main() {
// finally, convert it to the array (this is `O(1)`)
let result: PrimitiveArray<i64> = growable.into();

let expected = PrimitiveArray::<i64>::from_slice(&[1, 2, 3, 4, 5, 6]);
let expected = PrimitiveArray::<i64>::from_vec(vec![1, 2, 3, 4, 5, 6]);
assert_eq!(result, expected);
}
8 changes: 7 additions & 1 deletion src/array/primitive/from_natural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ impl<T: NativeType> PrimitiveArray<T> {

/// Creates a (non-null) [`PrimitiveArray`] from a slice of values.
/// # Implementation
/// This is essentially a memcopy and is the fastest way to create a [`PrimitiveArray`].
/// This is essentially a memcopy
pub fn from_slice<P: AsRef<[T]>>(slice: P) -> Self {
Self::from_data(
T::PRIMITIVE.into(),
Vec::<T>::from(slice.as_ref()).into(),
None,
)
}

/// Creates a (non-null) [`PrimitiveArray`] from a vector of values.
/// This does not have memcopy and is the fastest way to create a [`PrimitiveArray`].
pub fn from_vec(array: Vec<T>) -> Self {
Self::from_data(T::PRIMITIVE.into(), array.into(), None)
}
}

impl<T: NativeType> PrimitiveArray<T> {
Expand Down
5 changes: 2 additions & 3 deletions src/util/bench_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use rand::distributions::{Alphanumeric, Distribution, Standard};
use rand::{rngs::StdRng, Rng, SeedableRng};

use crate::types::NaturalDataType;
use crate::{array::*, types::NativeType};

/// Returns fixed seedable RNG
Expand All @@ -14,7 +13,7 @@ pub fn seedable_rng() -> StdRng {
/// Creates an random (but fixed-seeded) array of a given size and null density
pub fn create_primitive_array<T>(size: usize, null_density: f32) -> PrimitiveArray<T>
where
T: NativeType + NaturalDataType,
T: NativeType,
Standard: Distribution<T>,
{
let mut rng = seedable_rng();
Expand All @@ -37,7 +36,7 @@ pub fn create_primitive_array_with_seed<T>(
seed: u64,
) -> PrimitiveArray<T>
where
T: NativeType + NaturalDataType,
T: NativeType,
Standard: Distribution<T>,
{
let mut rng = StdRng::seed_from_u64(seed);
Expand Down
15 changes: 9 additions & 6 deletions tests/it/io/csv/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn data() -> RecordBatch {
let c4 = BooleanArray::from(&[Some(true), Some(false), None]);
let c5 = PrimitiveArray::<i64>::from([None, Some(1555584887378), Some(1555555555555)])
.to(DataType::Timestamp(TimeUnit::Millisecond, None));
let c6 = PrimitiveArray::<i32>::from_slice(&[1234, 24680, 85563])
let c6 = PrimitiveArray::<i32>::from_vec(vec![1234, 24680, 85563])
.to(DataType::Time32(TimeUnit::Second));
let keys = UInt32Array::from_slice(&[2, 0, 1]);
let c7 = DictionaryArray::from_data(keys, Arc::new(c1.clone()));
Expand Down Expand Up @@ -122,24 +122,27 @@ fn data_array(column: usize) -> (RecordBatch, Vec<&'static str>) {
vec!["3", "2", "1"],
),
9 => {
let array = PrimitiveArray::<i32>::from_slice(&[1_234_001, 24_680_001, 85_563_001])
let array = PrimitiveArray::<i32>::from_vec(vec![1_234_001, 24_680_001, 85_563_001])
.to(DataType::Time32(TimeUnit::Millisecond));
(
Arc::new(array) as Arc<dyn Array>,
vec!["00:20:34.001", "06:51:20.001", "23:46:03.001"],
)
}
10 => {
let array =
PrimitiveArray::<i64>::from_slice(&[1_234_000_001, 24_680_000_001, 85_563_000_001])
.to(DataType::Time64(TimeUnit::Microsecond));
let array = PrimitiveArray::<i64>::from_vec(vec![
1_234_000_001,
24_680_000_001,
85_563_000_001,
])
.to(DataType::Time64(TimeUnit::Microsecond));
(
Arc::new(array) as Arc<dyn Array>,
vec!["00:20:34.000001", "06:51:20.000001", "23:46:03.000001"],
)
}
11 => {
let array = PrimitiveArray::<i64>::from_slice(&[
let array = PrimitiveArray::<i64>::from_vec(vec![
1_234_000_000_001,
24_680_000_000_001,
85_563_000_000_001,
Expand Down

0 comments on commit 87a08f4

Please sign in to comment.