Skip to content

Commit

Permalink
refactor: Simplified type signatures related to ArrayType::Array (#269
Browse files Browse the repository at this point in the history
)
  • Loading branch information
vultix authored Nov 1, 2024
1 parent 1a52e3a commit b2ae31c
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 384 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@1.70.0
- uses: dtolnay/rust-toolchain@1.79.0
id: rust-toolchain
- uses: dtolnay/install@master
with:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace.package]
authors = ["Matthijs Brobbel <m1brobbel@gmail.com>"]
edition = "2021"
rust-version = "1.70.0"
rust-version = "1.79.0"
description = "An implementation of Apache Arrow"
readme = "README.md"
repository = "https://github.com/mbrobbel/narrow"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ The crate supports the following optional features:

# Minimum supported Rust version

The minimum supported Rust version for this crate is Rust 1.70.0.
The minimum supported Rust version for this crate is Rust 1.79.0.

# License

Expand Down
136 changes: 39 additions & 97 deletions src/array/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Sequences of values with known length all having the same type.
use crate::{
buffer::BufferType,
buffer::{BufferType, VecBuffer},
logical::{LogicalArray, LogicalArrayType},
offset::{self, OffsetElement},
validity::Nullability,
Length,
};
use std::{collections::VecDeque, marker::PhantomData};
Expand Down Expand Up @@ -67,6 +68,27 @@ pub trait ArrayType<T: ?Sized> {
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType>: Array;
}

/// A helper type that allows extracting the [`ArrayType::Array`] type for any `ArrayType<T> for T`
pub type ArrayTypeOf<T, Buffer = VecBuffer, OffsetItem = offset::NA, UnionLayout = union::NA> =
<T as ArrayType<T>>::Array<Buffer, OffsetItem, UnionLayout>;

/// A helper type that allows extracting the [`ArrayType::Array`] type for any `ArrayType<Option<T>> for T`
pub type OptionArrayTypeOf<
T,
Buffer = VecBuffer,
OffsetItem = offset::NA,
UnionLayout = union::NA,
> = <Option<T> as ArrayType<T>>::Array<Buffer, OffsetItem, UnionLayout>;

/// A helper type that allows extracting the [`ArrayType::Array`] type for any `ArrayType<T>::Item for <T as Nullability<NULLABLE>`
pub type NullableArrayTypeOf<
const NULLABLE: bool,
T,
Buffer,
OffsetItem = offset::NA,
UnionLayout = union::NA,
> = <<T as Nullability<NULLABLE>>::Item as ArrayType<T>>::Array<Buffer, OffsetItem, UnionLayout>;

impl<T: ArrayType<U> + ?Sized, U> ArrayType<U> for &T {
type Array<Buffer: BufferType, OfsetItem: OffsetElement, UnionLayout: UnionType> =
<T as ArrayType<U>>::Array<Buffer, OfsetItem, UnionLayout>;
Expand Down Expand Up @@ -222,45 +244,25 @@ impl IntoIterator for VariableSizeBinary {

impl<T: ArrayType<T>, const N: usize> ArrayType<[T; N]> for [T; N] {
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
FixedSizeListArray<
N,
<T as ArrayType<T>>::Array<Buffer, OffsetItem, UnionLayout>,
false,
Buffer,
>;
FixedSizeListArray<N, ArrayTypeOf<T, Buffer, OffsetItem, UnionLayout>, false, Buffer>;
}
impl<T: ArrayType<T>, const N: usize> ArrayType<[T; N]> for Option<[T; N]> {
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
FixedSizeListArray<
N,
<T as ArrayType<T>>::Array<Buffer, OffsetItem, UnionLayout>,
true,
Buffer,
>;
FixedSizeListArray<N, ArrayTypeOf<T, Buffer, OffsetItem, UnionLayout>, true, Buffer>;
}
impl<T, const N: usize> ArrayType<[Option<T>; N]> for [Option<T>; N]
where
Option<T>: ArrayType<T>,
{
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
FixedSizeListArray<
N,
<Option<T> as ArrayType<T>>::Array<Buffer, OffsetItem, UnionLayout>,
false,
Buffer,
>;
FixedSizeListArray<N, OptionArrayTypeOf<T, Buffer, OffsetItem, UnionLayout>, false, Buffer>;
}
impl<T, const N: usize> ArrayType<[Option<T>; N]> for Option<[Option<T>; N]>
where
Option<T>: ArrayType<T>,
{
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
FixedSizeListArray<
N,
<Option<T> as ArrayType<T>>::Array<Buffer, OffsetItem, UnionLayout>,
true,
Buffer,
>;
FixedSizeListArray<N, OptionArrayTypeOf<T, Buffer, OffsetItem, UnionLayout>, true, Buffer>;
}
impl ArrayType<str> for str {
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
Expand All @@ -285,130 +287,70 @@ impl ArrayType<String> for Option<String> {

impl<'a, T: ArrayType<T>> ArrayType<&'a [T]> for &'a [T] {
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
VariableSizeListArray<
<T as ArrayType<T>>::Array<Buffer, offset::NA, union::NA>,
false,
OffsetItem,
Buffer,
>;
VariableSizeListArray<ArrayTypeOf<T, Buffer>, false, OffsetItem, Buffer>;
}
impl<'a, T: ArrayType<T>> ArrayType<&'a [T]> for Option<&'a [T]> {
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
VariableSizeListArray<
<T as ArrayType<T>>::Array<Buffer, offset::NA, union::NA>,
true,
OffsetItem,
Buffer,
>;
VariableSizeListArray<ArrayTypeOf<T, Buffer>, true, OffsetItem, Buffer>;
}
impl<'a, T> ArrayType<&'a [Option<T>]> for &'a [Option<T>]
where
Option<T>: ArrayType<T>,
{
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
VariableSizeListArray<
<Option<T> as ArrayType<T>>::Array<Buffer, offset::NA, union::NA>,
false,
OffsetItem,
Buffer,
>;
VariableSizeListArray<OptionArrayTypeOf<T, Buffer>, false, OffsetItem, Buffer>;
}
impl<'a, T> ArrayType<&'a [Option<T>]> for Option<&'a [Option<T>]>
where
Option<T>: ArrayType<T>,
{
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
VariableSizeListArray<
<Option<T> as ArrayType<T>>::Array<Buffer, offset::NA, union::NA>,
true,
OffsetItem,
Buffer,
>;
VariableSizeListArray<OptionArrayTypeOf<T, Buffer>, true, OffsetItem, Buffer>;
}
impl<T: ArrayType<T>> ArrayType<Vec<T>> for Vec<T> {
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
VariableSizeListArray<
<T as ArrayType<T>>::Array<Buffer, offset::NA, union::NA>,
false,
OffsetItem,
Buffer,
>;
VariableSizeListArray<ArrayTypeOf<T, Buffer>, false, OffsetItem, Buffer>;
}
impl<T: ArrayType<T>> ArrayType<Vec<T>> for Option<Vec<T>> {
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
VariableSizeListArray<
<T as ArrayType<T>>::Array<Buffer, offset::NA, union::NA>,
true,
OffsetItem,
Buffer,
>;
VariableSizeListArray<ArrayTypeOf<T, Buffer>, true, OffsetItem, Buffer>;
}
impl<T> ArrayType<Vec<Option<T>>> for Vec<Option<T>>
where
Option<T>: ArrayType<T>,
{
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
VariableSizeListArray<
<Option<T> as ArrayType<T>>::Array<Buffer, offset::NA, union::NA>,
false,
OffsetItem,
Buffer,
>;
VariableSizeListArray<OptionArrayTypeOf<T, Buffer>, false, OffsetItem, Buffer>;
}
impl<T> ArrayType<Vec<Option<T>>> for Option<Vec<Option<T>>>
where
Option<T>: ArrayType<T>,
{
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
VariableSizeListArray<
<Option<T> as ArrayType<T>>::Array<Buffer, offset::NA, union::NA>,
true,
OffsetItem,
Buffer,
>;
VariableSizeListArray<OptionArrayTypeOf<T, Buffer>, true, OffsetItem, Buffer>;
}

impl<T: ArrayType<T>> ArrayType<VecDeque<T>> for VecDeque<T> {
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
VariableSizeListArray<
<T as ArrayType<T>>::Array<Buffer, offset::NA, union::NA>,
false,
OffsetItem,
Buffer,
>;
VariableSizeListArray<ArrayTypeOf<T, Buffer>, false, OffsetItem, Buffer>;
}
impl<T: ArrayType<T>> ArrayType<VecDeque<T>> for Option<VecDeque<T>> {
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
VariableSizeListArray<
<T as ArrayType<T>>::Array<Buffer, offset::NA, union::NA>,
true,
OffsetItem,
Buffer,
>;
VariableSizeListArray<ArrayTypeOf<T, Buffer>, true, OffsetItem, Buffer>;
}
impl<T> ArrayType<VecDeque<Option<T>>> for VecDeque<Option<T>>
where
Option<T>: ArrayType<T>,
{
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
VariableSizeListArray<
<Option<T> as ArrayType<T>>::Array<Buffer, offset::NA, union::NA>,
false,
OffsetItem,
Buffer,
>;
VariableSizeListArray<OptionArrayTypeOf<T, Buffer>, false, OffsetItem, Buffer>;
}
impl<T> ArrayType<VecDeque<Option<T>>> for Option<VecDeque<Option<T>>>
where
Option<T>: ArrayType<T>,
{
type Array<Buffer: BufferType, OffsetItem: OffsetElement, UnionLayout: UnionType> =
VariableSizeListArray<
<Option<T> as ArrayType<T>>::Array<Buffer, offset::NA, union::NA>,
true,
OffsetItem,
Buffer,
>;
VariableSizeListArray<OptionArrayTypeOf<T, Buffer>, true, OffsetItem, Buffer>;
}

impl<T> ArrayType<PhantomData<T>> for PhantomData<T> {
Expand Down
9 changes: 2 additions & 7 deletions src/array/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,17 +337,12 @@ impl<OffsetItem: OffsetElement, Buffer: BufferType> ValidityBitmap
#[cfg(test)]
mod tests {
use super::*;
use crate::{
array::{union, ArrayType},
buffer::BufferRef,
};
use crate::{array::ArrayTypeOf, buffer::BufferRef};

#[test]
fn from_iter() {
let input = ["1", "23", "456", "7890"];
let array = input
.into_iter()
.collect::<<String as ArrayType<String>>::Array<VecBuffer, i64, union::NA>>();
let array = input.into_iter().collect::<ArrayTypeOf<String>>();
assert_eq!(array.len(), 4);
assert_eq!(array.0 .0.data.0, b"1234567890");

Expand Down
Loading

1 comment on commit b2ae31c

@mbrobbel
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'narrow'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: b2ae31c Previous: 1a52e3a Ratio
FixedSizePrimitiveArray::<arrow_array::types::UInt8Type, false>::from_iter/narrow/65536 21584 ns/iter (± 64) 1362 ns/iter (± 3) 15.85

This comment was automatically generated by workflow using github-action-benchmark.

CC: @mbrobbel

Please sign in to comment.