Skip to content

Commit

Permalink
Introduce rustfmt.toml for consistent formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Swoorup committed Sep 30, 2024
1 parent e815499 commit 61506de
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 242 deletions.
31 changes: 9 additions & 22 deletions arrow_convert/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use arrow::{
buffer::{Buffer, ScalarBuffer},
};
use arrow_convert::{
deserialize::TryIntoCollection, serialize::TryIntoArrow, ArrowDeserialize, ArrowField,
ArrowSerialize,
deserialize::TryIntoCollection, serialize::TryIntoArrow, ArrowDeserialize, ArrowField, ArrowSerialize,
};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};

Expand Down Expand Up @@ -63,53 +62,41 @@ pub fn bench_buffer_deserialize(c: &mut Criterion) {
for size in [1, 10, 100, 1000, 10000].iter() {
group.throughput(Throughput::Elements(*size as u64));
group.bench_with_input(BenchmarkId::new("BufferU8", size), size, |b, &size| {
let data: ArrayRef = [BufU8Struct((0..size as u8).collect())]
.try_into_arrow()
.unwrap();
let data: ArrayRef = [BufU8Struct((0..size as u8).collect())].try_into_arrow().unwrap();
b.iter_batched(
|| data.clone(),
|data| {
let _: Vec<BufU8Struct> =
TryIntoCollection::try_into_collection(black_box(data)).unwrap();
let _: Vec<BufU8Struct> = TryIntoCollection::try_into_collection(black_box(data)).unwrap();
},
criterion::BatchSize::SmallInput,
)
});
group.bench_with_input(BenchmarkId::new("VecU8", size), size, |b, &size| {
let data: ArrayRef = [VecU8Struct((0..size as u8).collect())]
.try_into_arrow()
.unwrap();
let data: ArrayRef = [VecU8Struct((0..size as u8).collect())].try_into_arrow().unwrap();
b.iter_batched(
|| data.clone(),
|data| {
let _: Vec<VecU8Struct> =
TryIntoCollection::try_into_collection(black_box(data)).unwrap();
let _: Vec<VecU8Struct> = TryIntoCollection::try_into_collection(black_box(data)).unwrap();
},
criterion::BatchSize::SmallInput,
);
});
group.bench_with_input(BenchmarkId::new("BufferU32", size), size, |b, &size| {
let data: ArrayRef = [BufU32Struct((0..size as u32).collect())]
.try_into_arrow()
.unwrap();
let data: ArrayRef = [BufU32Struct((0..size as u32).collect())].try_into_arrow().unwrap();
b.iter_batched(
|| data.clone(),
|data| {
let _: Vec<BufU32Struct> =
TryIntoCollection::try_into_collection(black_box(data)).unwrap();
let _: Vec<BufU32Struct> = TryIntoCollection::try_into_collection(black_box(data)).unwrap();
},
criterion::BatchSize::SmallInput,
)
});
group.bench_with_input(BenchmarkId::new("VecU32", size), size, |b, &size| {
let data: ArrayRef = [VecU32Struct((0..size as u32).collect())]
.try_into_arrow()
.unwrap();
let data: ArrayRef = [VecU32Struct((0..size as u32).collect())].try_into_arrow().unwrap();
b.iter_batched(
|| data.clone(),
|data| {
let _: Vec<VecU32Struct> =
TryIntoCollection::try_into_collection(black_box(data)).unwrap();
let _: Vec<VecU32Struct> = TryIntoCollection::try_into_collection(black_box(data)).unwrap();
},
criterion::BatchSize::SmallInput,
);
Expand Down
55 changes: 14 additions & 41 deletions arrow_convert/src/deserialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ where
type ArrayType;

/// Deserialize this field from arrow
fn arrow_deserialize(
v: <Self::ArrayType as ArrowArrayIterable>::Item<'_>,
) -> Option<<Self as ArrowField>::Type>;
fn arrow_deserialize(v: <Self::ArrayType as ArrowArrayIterable>::Item<'_>) -> Option<<Self as ArrowField>::Type>;

#[inline]
#[doc(hidden)]
Expand All @@ -32,9 +30,7 @@ where
/// Ideally we would be able to capture the optional field of the iterator via
/// something like T::ArrayType: ArrowArrayIterable<Item=Option<E>>,
/// However, the E parameter seems to confuse the borrow checker if it's a reference.
fn arrow_deserialize_internal(
v: <Self::ArrayType as ArrowArrayIterable>::Item<'_>,
) -> <Self as ArrowField>::Type {
fn arrow_deserialize_internal(v: <Self::ArrayType as ArrowArrayIterable>::Item<'_>) -> <Self as ArrowField>::Type {
Self::arrow_deserialize(v).unwrap()
}
}
Expand Down Expand Up @@ -62,9 +58,7 @@ macro_rules! impl_arrow_deserialize_primitive {
type ArrayType = PrimitiveArray<$primitive_type>;

#[inline]
fn arrow_deserialize<'a>(
v: Option<<$primitive_type as ArrowPrimitiveType>::Native>,
) -> Option<Self> {
fn arrow_deserialize<'a>(v: Option<<$primitive_type as ArrowPrimitiveType>::Native>) -> Option<Self> {
v
}
}
Expand Down Expand Up @@ -97,16 +91,12 @@ where
type ArrayType = <T as ArrowDeserialize>::ArrayType;

#[inline]
fn arrow_deserialize(
v: <Self::ArrayType as ArrowArrayIterable>::Item<'_>,
) -> Option<<Self as ArrowField>::Type> {
fn arrow_deserialize(v: <Self::ArrayType as ArrowArrayIterable>::Item<'_>) -> Option<<Self as ArrowField>::Type> {
Self::arrow_deserialize_internal(v).map(Some)
}

#[inline]
fn arrow_deserialize_internal(
v: <Self::ArrayType as ArrowArrayIterable>::Item<'_>,
) -> <Self as ArrowField>::Type {
fn arrow_deserialize_internal(v: <Self::ArrayType as ArrowArrayIterable>::Item<'_>) -> <Self as ArrowField>::Type {
<T as ArrowDeserialize>::arrow_deserialize(v)
}
}
Expand Down Expand Up @@ -270,9 +260,7 @@ impl<const SIZE: usize> ArrowDeserialize for [u8; SIZE] {
}
}

pub(crate) fn arrow_deserialize_vec_helper<T>(
v: Option<ArrayRef>,
) -> Option<<Vec<T> as ArrowField>::Type>
pub(crate) fn arrow_deserialize_vec_helper<T>(v: Option<ArrayRef>) -> Option<<Vec<T> as ArrowField>::Type>
where
T: ArrowDeserialize + ArrowEnableVecForType + 'static,
T::ArrayType: ArrowArrayIterable,
Expand All @@ -294,16 +282,9 @@ where
type ArrayType = ListArray;

#[inline]
fn arrow_deserialize(
v: <Self::ArrayType as ArrowArrayIterable>::Item<'_>,
) -> Option<<Self as ArrowField>::Type> {
fn arrow_deserialize(v: <Self::ArrayType as ArrowArrayIterable>::Item<'_>) -> Option<<Self as ArrowField>::Type> {
let t = v?;
let array = t
.as_any()
.downcast_ref::<PrimitiveArray<K>>()
.unwrap()
.values()
.clone();
let array = t.as_any().downcast_ref::<PrimitiveArray<K>>().unwrap().values().clone();
Some(array)
}
}
Expand Down Expand Up @@ -403,9 +384,7 @@ where
}

/// Helper to return an iterator for elements from a [`arrow::array::Array`].
fn arrow_array_deserialize_iterator_internal<Element, Field>(
b: &dyn Array,
) -> impl Iterator<Item = Element> + '_
fn arrow_array_deserialize_iterator_internal<Element, Field>(b: &dyn Array) -> impl Iterator<Item = Element> + '_
where
Field: ArrowDeserialize + ArrowField<Type = Element> + 'static,
<Field as ArrowDeserialize>::ArrayType: ArrowArrayIterable,
Expand All @@ -432,17 +411,14 @@ where
arr.is_nullable()
)))
} else {
Ok(arrow_array_deserialize_iterator_internal::<
Element,
ArrowType,
>(arr))
Ok(arrow_array_deserialize_iterator_internal::<Element, ArrowType>(
arr,
))
}
}

/// Return an iterator that deserializes an [`Array`] to an element of type T
pub fn arrow_array_deserialize_iterator<T>(
arr: &dyn Array,
) -> arrow::error::Result<impl Iterator<Item = T> + '_>
pub fn arrow_array_deserialize_iterator<T>(arr: &dyn Array) -> arrow::error::Result<impl Iterator<Item = T> + '_>
where
T: ArrowDeserialize + ArrowField<Type = T> + 'static,
<T as ArrowDeserialize>::ArrayType: ArrowArrayIterable,
Expand All @@ -469,9 +445,6 @@ where
ArrowType: ArrowDeserialize + ArrowField<Type = Element> + 'static,
<ArrowType as ArrowDeserialize>::ArrayType: ArrowArrayIterable,
{
Ok(
arrow_array_deserialize_iterator_as_type::<Element, ArrowType>(self.borrow())?
.collect(),
)
Ok(arrow_array_deserialize_iterator_as_type::<Element, ArrowType>(self.borrow())?.collect())
}
}
10 changes: 2 additions & 8 deletions arrow_convert/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,6 @@ impl<T> ArrowEnableVecForType for Option<T> where T: ArrowField + ArrowEnableVec

// Blanket implementation for Vec<Vec<T>> and Vec<Buffer<T>> if vectors or buffers are enabled for T
impl<T> ArrowEnableVecForType for Vec<T> where T: ArrowField + ArrowEnableVecForType {}
impl<T> ArrowEnableVecForType for ScalarBuffer<T> where
T: ArrowField + ArrowEnableVecForType + ArrowNativeType
{
}
impl<T> ArrowEnableVecForType for ScalarBuffer<T> where T: ArrowField + ArrowEnableVecForType + ArrowNativeType {}
impl<T> ArrowEnableVecForType for LargeVec<T> where T: ArrowField + ArrowEnableVecForType {}
impl<T, const SIZE: i32> ArrowEnableVecForType for FixedSizeVec<T, SIZE> where
T: ArrowField + ArrowEnableVecForType
{
}
impl<T, const SIZE: i32> ArrowEnableVecForType for FixedSizeVec<T, SIZE> where T: ArrowField + ArrowEnableVecForType {}
59 changes: 12 additions & 47 deletions arrow_convert/src/serialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ pub trait ArrowSerialize: ArrowField {
fn new_array() -> Self::ArrayBuilderType;

/// Serialize this field to arrow
fn arrow_serialize(
v: &<Self as ArrowField>::Type,
array: &mut Self::ArrayBuilderType,
) -> arrow::error::Result<()>;
fn arrow_serialize(v: &<Self as ArrowField>::Type, array: &mut Self::ArrayBuilderType) -> arrow::error::Result<()>;
}

// Macro to facilitate implementation of serializable traits for numeric types and numeric mutable arrays.
Expand All @@ -44,10 +41,7 @@ macro_rules! impl_numeric_type {
}

#[inline]
fn arrow_serialize(
v: &Self,
array: &mut Self::ArrayBuilderType,
) -> arrow::error::Result<()> {
fn arrow_serialize(v: &Self, array: &mut Self::ArrayBuilderType) -> arrow::error::Result<()> {
array.append_option(Some(*v));
Ok(())
}
Expand All @@ -69,10 +63,7 @@ where
}

#[inline]
fn arrow_serialize(
v: &<Self as ArrowField>::Type,
array: &mut Self::ArrayBuilderType,
) -> arrow::error::Result<()> {
fn arrow_serialize(v: &<Self as ArrowField>::Type, array: &mut Self::ArrayBuilderType) -> arrow::error::Result<()> {
match v.as_ref() {
Some(t) => <T as ArrowSerialize>::arrow_serialize(t, array),
None => {
Expand Down Expand Up @@ -256,10 +247,7 @@ impl ArrowSerialize for LargeBinary {
}

#[inline]
fn arrow_serialize(
v: &Vec<u8>,
array: &mut Self::ArrayBuilderType,
) -> arrow::error::Result<()> {
fn arrow_serialize(v: &Vec<u8>, array: &mut Self::ArrayBuilderType) -> arrow::error::Result<()> {
array.append_option(Some(v));
Ok(())
}
Expand All @@ -274,10 +262,7 @@ impl<const SIZE: i32> ArrowSerialize for FixedSizeBinary<SIZE> {
}

#[inline]
fn arrow_serialize(
v: &Vec<u8>,
array: &mut Self::ArrayBuilderType,
) -> arrow::error::Result<()> {
fn arrow_serialize(v: &Vec<u8>, array: &mut Self::ArrayBuilderType) -> arrow::error::Result<()> {
array.append_value(v)
}
}
Expand Down Expand Up @@ -310,10 +295,7 @@ where
}

#[inline]
fn arrow_serialize(
v: &<Self as ArrowField>::Type,
array: &mut Self::ArrayBuilderType,
) -> arrow::error::Result<()> {
fn arrow_serialize(v: &<Self as ArrowField>::Type, array: &mut Self::ArrayBuilderType) -> arrow::error::Result<()> {
let values = array.values();
for i in v.iter() {
<T as ArrowSerialize>::arrow_serialize(i, values)?;
Expand All @@ -337,10 +319,7 @@ where
ListBuilder::new(<T as ArrowSerialize>::new_array()).with_field(field)
}

fn arrow_serialize(
v: &<Self as ArrowField>::Type,
array: &mut Self::ArrayBuilderType,
) -> arrow::error::Result<()> {
fn arrow_serialize(v: &<Self as ArrowField>::Type, array: &mut Self::ArrayBuilderType) -> arrow::error::Result<()> {
let values = array.values();
for i in v.iter() {
<T as ArrowSerialize>::arrow_serialize(i, values)?;
Expand All @@ -363,10 +342,7 @@ where
Self::ArrayBuilderType::new(<T as ArrowSerialize>::new_array()).with_field(field)
}

fn arrow_serialize(
v: &<Self as ArrowField>::Type,
array: &mut Self::ArrayBuilderType,
) -> arrow::error::Result<()> {
fn arrow_serialize(v: &<Self as ArrowField>::Type, array: &mut Self::ArrayBuilderType) -> arrow::error::Result<()> {
let values = array.values();
for i in v.iter() {
<T as ArrowSerialize>::arrow_serialize(i, values)?;
Expand All @@ -389,10 +365,7 @@ where
.with_field(<T as ArrowField>::field(DEFAULT_FIELD_NAME))
}

fn arrow_serialize(
v: &<Self as ArrowField>::Type,
array: &mut Self::ArrayBuilderType,
) -> arrow::error::Result<()> {
fn arrow_serialize(v: &<Self as ArrowField>::Type, array: &mut Self::ArrayBuilderType) -> arrow::error::Result<()> {
let values = array.values();
for i in v.iter() {
<T as ArrowSerialize>::arrow_serialize(i, values)?;
Expand All @@ -415,10 +388,7 @@ where
.with_field(<T as ArrowField>::field(DEFAULT_FIELD_NAME))
}

fn arrow_serialize(
v: &<Self as ArrowField>::Type,
array: &mut Self::ArrayBuilderType,
) -> arrow::error::Result<()> {
fn arrow_serialize(v: &<Self as ArrowField>::Type, array: &mut Self::ArrayBuilderType) -> arrow::error::Result<()> {
let values = array.values();
for i in v.iter() {
<T as ArrowSerialize>::arrow_serialize(i, values)?;
Expand Down Expand Up @@ -483,16 +453,11 @@ impl FlattenRecordBatch for RecordBatch {
let data_type = array.as_ref().data_type();
if !matches!(data_type, arrow::datatypes::DataType::Struct(_)) {
return Err(arrow::error::ArrowError::InvalidArgumentError(
"Array in RecordBatch must be of type arrow::datatypes::PhysicalType::Struct"
.to_string(),
"Array in RecordBatch must be of type arrow::datatypes::PhysicalType::Struct".to_string(),
));
}

let struct_array = array
.as_ref()
.as_any()
.downcast_ref::<StructArray>()
.unwrap();
let struct_array = array.as_ref().as_any().downcast_ref::<StructArray>().unwrap();
Ok(RecordBatch::from(struct_array))
}
}
Expand Down
4 changes: 2 additions & 2 deletions arrow_convert/src/serialize/push_null.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use arrow::{
array::{
ArrayBuilder, BinaryBuilder, BooleanBufferBuilder, BooleanBuilder, FixedSizeBinaryBuilder,
FixedSizeListBuilder, LargeBinaryBuilder, LargeListBuilder, LargeStringBuilder,
ListBuilder, PrimitiveBuilder, StringBuilder,
FixedSizeListBuilder, LargeBinaryBuilder, LargeListBuilder, LargeStringBuilder, ListBuilder, PrimitiveBuilder,
StringBuilder,
},
datatypes::ArrowPrimitiveType,
};
Expand Down
3 changes: 1 addition & 2 deletions arrow_convert/tests/simple_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::sync::Arc;
/// Simple example
use arrow::array::Array;
use arrow_convert::{
deserialize::TryIntoCollection, serialize::TryIntoArrow, ArrowDeserialize, ArrowField,
ArrowSerialize,
deserialize::TryIntoCollection, serialize::TryIntoArrow, ArrowDeserialize, ArrowField, ArrowSerialize,
};

#[derive(Debug, Clone, PartialEq, Eq, ArrowField, ArrowSerialize, ArrowDeserialize)]
Expand Down
Loading

0 comments on commit 61506de

Please sign in to comment.