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

Commit

Permalink
Simplified traits.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Dec 21, 2021
1 parent 28aad3e commit f56d15b
Show file tree
Hide file tree
Showing 38 changed files with 643 additions and 704 deletions.
5 changes: 3 additions & 2 deletions src/array/binary/mod.rs
@@ -1,8 +1,9 @@
use crate::{bitmap::Bitmap, buffer::Buffer, datatypes::DataType};

use super::{
display_fmt, display_helper, specification::check_offsets,
specification::check_offsets_minimal, specification::Offset, Array, GenericBinaryArray,
display_fmt, display_helper,
specification::{check_offsets, check_offsets_minimal},
Array, GenericBinaryArray, Offset,
};

mod ffi;
Expand Down
3 changes: 2 additions & 1 deletion src/array/dictionary/ffi.rs
Expand Up @@ -30,7 +30,8 @@ impl<K: DictionaryKey, A: ffi::ArrowArrayRef> FromFfi<A> for DictionaryArray<K>
let validity = unsafe { array.validity() }?;
let values = unsafe { array.buffer::<K>(1) }?;

let keys = PrimitiveArray::<K>::from_data(K::DATA_TYPE, values, validity);
let data_type = K::PRIMITIVE.into();
let keys = PrimitiveArray::<K>::from_data(data_type, values, validity);
let values = array.dictionary()?.unwrap();
let values = ffi::try_from(values)?.into();

Expand Down
6 changes: 4 additions & 2 deletions src/array/dictionary/mod.rs
Expand Up @@ -62,15 +62,17 @@ impl<K: DictionaryKey> DictionaryArray<K> {
pub fn new_empty(data_type: DataType) -> Self {
let values = Self::get_child(&data_type);
let values = new_empty_array(values.clone()).into();
Self::from_data(PrimitiveArray::<K>::new_empty(K::DATA_TYPE), values)
let data_type = K::PRIMITIVE.into();
Self::from_data(PrimitiveArray::<K>::new_empty(data_type), values)
}

/// Returns an [`DictionaryArray`] whose all elements are null
#[inline]
pub fn new_null(data_type: DataType, length: usize) -> Self {
let values = Self::get_child(&data_type);
let data_type = K::PRIMITIVE.into();
Self::from_data(
PrimitiveArray::<K>::new_null(K::DATA_TYPE, length),
PrimitiveArray::<K>::new_null(data_type, length),
new_empty_array(values.clone()).into(),
)
}
Expand Down
6 changes: 4 additions & 2 deletions src/array/growable/dictionary.rs
Expand Up @@ -84,7 +84,8 @@ impl<'a, T: DictionaryKey> GrowableDictionary<'a, T> {
let validity = std::mem::take(&mut self.key_validity);
let values = std::mem::take(&mut self.key_values);

let keys = PrimitiveArray::<T>::from_data(T::DATA_TYPE, values.into(), validity.into());
let data_type = T::PRIMITIVE.into();
let keys = PrimitiveArray::<T>::from_data(data_type, values.into(), validity.into());

DictionaryArray::<T>::from_data(keys, self.values.clone())
}
Expand Down Expand Up @@ -125,8 +126,9 @@ impl<'a, T: DictionaryKey> Growable<'a> for GrowableDictionary<'a, T> {
impl<'a, T: DictionaryKey> From<GrowableDictionary<'a, T>> for DictionaryArray<T> {
#[inline]
fn from(val: GrowableDictionary<'a, T>) -> Self {
let data_type = T::PRIMITIVE.into();
let keys = PrimitiveArray::<T>::from_data(
T::DATA_TYPE,
data_type,
val.key_values.into(),
val.key_validity.into(),
);
Expand Down
2 changes: 1 addition & 1 deletion src/array/list/ffi.rs
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use crate::{array::FromFfi, bitmap::align, error::Result, ffi};

use super::super::{ffi::ToFfi, specification::Offset, Array};
use super::super::{ffi::ToFfi, Array, Offset};
use super::ListArray;

unsafe impl<O: Offset> ToFfi for ListArray<O> {
Expand Down
6 changes: 1 addition & 5 deletions src/array/list/mod.rs
Expand Up @@ -6,11 +6,7 @@ use crate::{
datatypes::{DataType, Field},
};

use super::{
display_fmt, new_empty_array,
specification::{check_offsets, Offset},
Array,
};
use super::{display_fmt, new_empty_array, specification::check_offsets, Array, Offset};

mod ffi;
mod iterator;
Expand Down
2 changes: 1 addition & 1 deletion src/array/mod.rs
Expand Up @@ -371,7 +371,7 @@ pub use list::{ListArray, MutableListArray};
pub use map::MapArray;
pub use null::NullArray;
pub use primitive::*;
pub use specification::Offset;
pub use crate::types::Offset;
pub use struct_::StructArray;
pub use union::UnionArray;
pub use utf8::{MutableUtf8Array, Utf8Array, Utf8ValuesIter};
Expand Down
16 changes: 7 additions & 9 deletions src/array/primitive/from_natural.rs
Expand Up @@ -3,32 +3,30 @@ use std::iter::FromIterator;
use crate::{
buffer::{Buffer, MutableBuffer},
trusted_len::TrustedLen,
types::{NativeType, NaturalDataType},
types::NativeType,
};

use super::{MutablePrimitiveArray, PrimitiveArray};

impl<T: NativeType + NaturalDataType, P: AsRef<[Option<T>]>> From<P> for PrimitiveArray<T> {
impl<T: NativeType, P: AsRef<[Option<T>]>> From<P> for PrimitiveArray<T> {
fn from(slice: P) -> Self {
MutablePrimitiveArray::<T>::from(slice).into()
}
}

impl<T: NativeType + NaturalDataType, Ptr: std::borrow::Borrow<Option<T>>> FromIterator<Ptr>
for PrimitiveArray<T>
{
impl<T: NativeType, Ptr: std::borrow::Borrow<Option<T>>> FromIterator<Ptr> for PrimitiveArray<T> {
fn from_iter<I: IntoIterator<Item = Ptr>>(iter: I) -> Self {
MutablePrimitiveArray::<T>::from_iter(iter).into()
}
}

impl<T: NativeType + NaturalDataType> PrimitiveArray<T> {
impl<T: NativeType> PrimitiveArray<T> {
/// Creates a (non-null) [`PrimitiveArray`] from an iterator of values.
/// # Implementation
/// This does not assume that the iterator has a known length.
pub fn from_values<I: IntoIterator<Item = T>>(iter: I) -> Self {
Self::from_data(
T::DATA_TYPE,
T::PRIMITIVE.into(),
MutableBuffer::<T>::from_iter(iter).into(),
None,
)
Expand All @@ -38,11 +36,11 @@ impl<T: NativeType + NaturalDataType> PrimitiveArray<T> {
/// # Implementation
/// This is essentially a memcopy and is the fastest way to create a [`PrimitiveArray`].
pub fn from_slice<P: AsRef<[T]>>(slice: P) -> Self {
Self::from_data(T::DATA_TYPE, Buffer::<T>::from(slice), None)
Self::from_data(T::PRIMITIVE.into(), Buffer::<T>::from(slice), None)
}
}

impl<T: NativeType + NaturalDataType> PrimitiveArray<T> {
impl<T: NativeType> PrimitiveArray<T> {
/// Creates a (non-null) [`PrimitiveArray`] from a [`TrustedLen`] of values.
/// # Implementation
/// This does not assume that the iterator has a known length.
Expand Down
122 changes: 61 additions & 61 deletions src/array/primitive/mod.rs
Expand Up @@ -38,43 +38,39 @@ pub struct PrimitiveArray<T: NativeType> {
}

impl<T: NativeType> PrimitiveArray<T> {
/// Returns a new empty [`PrimitiveArray`].
pub fn new_empty(data_type: DataType) -> Self {
Self::from_data(data_type, Buffer::new(), None)
/// Returns the length of this array
#[inline]
pub fn len(&self) -> usize {
self.values.len()
}

/// Returns a new [`PrimitiveArray`] whose all slots are null / `None`.
/// The optional validity.
#[inline]
pub fn new_null(data_type: DataType, length: usize) -> Self {
Self::from_data(
data_type,
Buffer::new_zeroed(length),
Some(Bitmap::new_zeroed(length)),
)
pub fn validity(&self) -> Option<&Bitmap> {
self.validity.as_ref()
}

/// The canonical method to create a [`PrimitiveArray`] out of low-end APIs.
/// # Panics
/// This function panics iff:
/// * `data_type` is not supported by the physical type
/// * The validity is not `None` and its length is different from the `values`'s length
pub fn from_data(data_type: DataType, values: Buffer<T>, validity: Option<Bitmap>) -> Self {
if !T::is_valid(&data_type) {
Err(ArrowError::InvalidArgumentError(format!(
"Type {} does not support logical type {}",
std::any::type_name::<T>(),
data_type
)))
.unwrap()
}
if let Some(ref validity) = validity {
assert_eq!(values.len(), validity.len());
}
Self {
data_type,
values,
validity,
}
/// The values [`Buffer`].
/// Values on null slots are undetermined (they can be anything).
#[inline]
pub fn values(&self) -> &Buffer<T> {
&self.values
}

/// Returns the value at slot `i`. Equivalent to `self.values()[i]`.
/// The value on null slots is undetermined (it can be anything).
#[inline]
pub fn value(&self, i: usize) -> T {
self.values()[i]
}

/// Returns the element at index `i` as `T`.
/// The value on null slots is undetermined (it can be anything).
/// # Safety
/// Caller must be sure that `i < self.len()`
#[inline]
pub unsafe fn value_unchecked(&self, i: usize) -> T {
*self.values.get_unchecked(i)
}

/// Returns a slice of this [`PrimitiveArray`].
Expand Down Expand Up @@ -123,39 +119,43 @@ impl<T: NativeType> PrimitiveArray<T> {
}

impl<T: NativeType> PrimitiveArray<T> {
/// Returns the length of this array
#[inline]
pub fn len(&self) -> usize {
self.values.len()
}

/// The optional validity.
#[inline]
pub fn validity(&self) -> Option<&Bitmap> {
self.validity.as_ref()
}

/// The values [`Buffer`].
/// Values on null slots are undetermined (they can be anything).
#[inline]
pub fn values(&self) -> &Buffer<T> {
&self.values
/// Returns a new empty [`PrimitiveArray`].
pub fn new_empty(data_type: DataType) -> Self {
Self::from_data(data_type, Buffer::new(), None)
}

/// Returns the value at slot `i`. Equivalent to `self.values()[i]`.
/// The value on null slots is undetermined (it can be anything).
/// Returns a new [`PrimitiveArray`] whose all slots are null / `None`.
#[inline]
pub fn value(&self, i: usize) -> T {
self.values()[i]
pub fn new_null(data_type: DataType, length: usize) -> Self {
Self::from_data(
data_type,
Buffer::new_zeroed(length),
Some(Bitmap::new_zeroed(length)),
)
}

/// Returns the element at index `i` as `T`.
/// The value on null slots is undetermined (it can be anything).
/// # Safety
/// Caller must be sure that `i < self.len()`
#[inline]
pub unsafe fn value_unchecked(&self, i: usize) -> T {
*self.values.get_unchecked(i)
/// The canonical method to create a [`PrimitiveArray`] out of low-end APIs.
/// # Panics
/// This function panics iff:
/// * `data_type` is not supported by the physical type
/// * The validity is not `None` and its length is different from the `values`'s length
pub fn from_data(data_type: DataType, values: Buffer<T>, validity: Option<Bitmap>) -> Self {
if !data_type.to_physical_type().eq_primitive(T::PRIMITIVE) {
Err(ArrowError::InvalidArgumentError(format!(
"Type {} does not support logical type {}",
std::any::type_name::<T>(),
data_type
)))
.unwrap()
}
if let Some(ref validity) = validity {
assert_eq!(values.len(), validity.len());
}
Self {
data_type,
values,
validity,
}
}

/// Returns a new [`PrimitiveArray`] with a different logical type.
Expand All @@ -164,7 +164,7 @@ impl<T: NativeType> PrimitiveArray<T> {
/// Panics iff the data_type is not supported for the physical type.
#[inline]
pub fn to(self, data_type: DataType) -> Self {
if !T::is_valid(&data_type) {
if !data_type.to_physical_type().eq_primitive(T::PRIMITIVE) {
Err(ArrowError::InvalidArgumentError(format!(
"Type {} does not support logical type {}",
std::any::type_name::<T>(),
Expand Down

0 comments on commit f56d15b

Please sign in to comment.