use std::{
fmt::{Display, Formatter},
sync::Arc,
};
use crate::{Bitmask, FloatArray, IntegerArray, MaskedArray, Vec64};
use crate::{BooleanArray, StringArray};
use crate::{
enums::{error::MinarrowError, shape_dim::ShapeDim},
traits::{concatenate::Concatenate, shape::Shape},
};
#[repr(C, align(64))]
#[derive(PartialEq, Clone, Debug, Default)]
pub enum NumericArray {
#[cfg(feature = "extended_numeric_types")]
Int8(Arc<IntegerArray<i8>>),
#[cfg(feature = "extended_numeric_types")]
Int16(Arc<IntegerArray<i16>>),
Int32(Arc<IntegerArray<i32>>),
Int64(Arc<IntegerArray<i64>>),
#[cfg(feature = "extended_numeric_types")]
UInt8(Arc<IntegerArray<u8>>),
#[cfg(feature = "extended_numeric_types")]
UInt16(Arc<IntegerArray<u16>>),
UInt32(Arc<IntegerArray<u32>>),
UInt64(Arc<IntegerArray<u64>>),
Float32(Arc<FloatArray<f32>>),
Float64(Arc<FloatArray<f64>>),
#[default]
Null, }
impl NumericArray {
#[inline]
pub fn len(&self) -> usize {
match self {
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int8(arr) => arr.len(),
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int16(arr) => arr.len(),
NumericArray::Int32(arr) => arr.len(),
NumericArray::Int64(arr) => arr.len(),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt8(arr) => arr.len(),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt16(arr) => arr.len(),
NumericArray::UInt32(arr) => arr.len(),
NumericArray::UInt64(arr) => arr.len(),
NumericArray::Float32(arr) => arr.len(),
NumericArray::Float64(arr) => arr.len(),
NumericArray::Null => 0,
}
}
#[inline]
pub fn null_mask(&self) -> Option<&Bitmask> {
match self {
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int8(arr) => arr.null_mask.as_ref(),
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int16(arr) => arr.null_mask.as_ref(),
NumericArray::Int32(arr) => arr.null_mask.as_ref(),
NumericArray::Int64(arr) => arr.null_mask.as_ref(),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt8(arr) => arr.null_mask.as_ref(),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt16(arr) => arr.null_mask.as_ref(),
NumericArray::UInt32(arr) => arr.null_mask.as_ref(),
NumericArray::UInt64(arr) => arr.null_mask.as_ref(),
NumericArray::Float32(arr) => arr.null_mask.as_ref(),
NumericArray::Float64(arr) => arr.null_mask.as_ref(),
NumericArray::Null => None,
}
}
pub fn append_array(&mut self, other: &Self) {
match (self, other) {
#[cfg(feature = "extended_numeric_types")]
(NumericArray::Int8(a), NumericArray::Int8(b)) => Arc::make_mut(a).append_array(b),
#[cfg(feature = "extended_numeric_types")]
(NumericArray::Int16(a), NumericArray::Int16(b)) => Arc::make_mut(a).append_array(b),
(NumericArray::Int32(a), NumericArray::Int32(b)) => Arc::make_mut(a).append_array(b),
(NumericArray::Int64(a), NumericArray::Int64(b)) => Arc::make_mut(a).append_array(b),
#[cfg(feature = "extended_numeric_types")]
(NumericArray::UInt8(a), NumericArray::UInt8(b)) => Arc::make_mut(a).append_array(b),
#[cfg(feature = "extended_numeric_types")]
(NumericArray::UInt16(a), NumericArray::UInt16(b)) => Arc::make_mut(a).append_array(b),
(NumericArray::UInt32(a), NumericArray::UInt32(b)) => Arc::make_mut(a).append_array(b),
(NumericArray::UInt64(a), NumericArray::UInt64(b)) => Arc::make_mut(a).append_array(b),
(NumericArray::Float32(a), NumericArray::Float32(b)) => {
Arc::make_mut(a).append_array(b)
}
(NumericArray::Float64(a), NumericArray::Float64(b)) => {
Arc::make_mut(a).append_array(b)
}
(NumericArray::Null, NumericArray::Null) => (),
(lhs, rhs) => panic!("Cannot append {:?} into {:?}", rhs, lhs),
}
}
pub fn append_range(&mut self, other: &Self, offset: usize, len: usize) -> Result<(), MinarrowError> {
match (self, other) {
#[cfg(feature = "extended_numeric_types")]
(NumericArray::Int8(a), NumericArray::Int8(b)) => Arc::make_mut(a).append_range(b, offset, len),
#[cfg(feature = "extended_numeric_types")]
(NumericArray::Int16(a), NumericArray::Int16(b)) => Arc::make_mut(a).append_range(b, offset, len),
(NumericArray::Int32(a), NumericArray::Int32(b)) => Arc::make_mut(a).append_range(b, offset, len),
(NumericArray::Int64(a), NumericArray::Int64(b)) => Arc::make_mut(a).append_range(b, offset, len),
#[cfg(feature = "extended_numeric_types")]
(NumericArray::UInt8(a), NumericArray::UInt8(b)) => Arc::make_mut(a).append_range(b, offset, len),
#[cfg(feature = "extended_numeric_types")]
(NumericArray::UInt16(a), NumericArray::UInt16(b)) => Arc::make_mut(a).append_range(b, offset, len),
(NumericArray::UInt32(a), NumericArray::UInt32(b)) => Arc::make_mut(a).append_range(b, offset, len),
(NumericArray::UInt64(a), NumericArray::UInt64(b)) => Arc::make_mut(a).append_range(b, offset, len),
(NumericArray::Float32(a), NumericArray::Float32(b)) => Arc::make_mut(a).append_range(b, offset, len),
(NumericArray::Float64(a), NumericArray::Float64(b)) => Arc::make_mut(a).append_range(b, offset, len),
(NumericArray::Null, NumericArray::Null) => Ok(()),
(lhs, rhs) => Err(MinarrowError::TypeError {
from: "NumericArray",
to: "NumericArray",
message: Some(format!("Cannot append_range {:?} into {:?}", rhs, lhs)),
}),
}
}
pub fn insert_rows(&mut self, index: usize, other: &Self) -> Result<(), MinarrowError> {
match (self, other) {
#[cfg(feature = "extended_numeric_types")]
(NumericArray::Int8(a), NumericArray::Int8(b)) => {
Arc::make_mut(a).insert_rows(index, b)
}
#[cfg(feature = "extended_numeric_types")]
(NumericArray::Int16(a), NumericArray::Int16(b)) => {
Arc::make_mut(a).insert_rows(index, b)
}
(NumericArray::Int32(a), NumericArray::Int32(b)) => {
Arc::make_mut(a).insert_rows(index, b)
}
(NumericArray::Int64(a), NumericArray::Int64(b)) => {
Arc::make_mut(a).insert_rows(index, b)
}
#[cfg(feature = "extended_numeric_types")]
(NumericArray::UInt8(a), NumericArray::UInt8(b)) => {
Arc::make_mut(a).insert_rows(index, b)
}
#[cfg(feature = "extended_numeric_types")]
(NumericArray::UInt16(a), NumericArray::UInt16(b)) => {
Arc::make_mut(a).insert_rows(index, b)
}
(NumericArray::UInt32(a), NumericArray::UInt32(b)) => {
Arc::make_mut(a).insert_rows(index, b)
}
(NumericArray::UInt64(a), NumericArray::UInt64(b)) => {
Arc::make_mut(a).insert_rows(index, b)
}
(NumericArray::Float32(a), NumericArray::Float32(b)) => {
Arc::make_mut(a).insert_rows(index, b)
}
(NumericArray::Float64(a), NumericArray::Float64(b)) => {
Arc::make_mut(a).insert_rows(index, b)
}
(NumericArray::Null, NumericArray::Null) => Ok(()),
(lhs, rhs) => Err(MinarrowError::TypeError {
from: "NumericArray",
to: "NumericArray",
message: Some(format!(
"Cannot insert {} into {}: incompatible types",
rhs, lhs
)),
}),
}
}
pub fn split(self, index: usize) -> Result<(Self, Self), MinarrowError> {
use std::sync::Arc;
match self {
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int8(a) => {
let (left, right) = Arc::try_unwrap(a)
.unwrap_or_else(|arc| (*arc).clone())
.split(index)?;
Ok((
NumericArray::Int8(Arc::new(left)),
NumericArray::Int8(Arc::new(right)),
))
}
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt8(a) => {
let (left, right) = Arc::try_unwrap(a)
.unwrap_or_else(|arc| (*arc).clone())
.split(index)?;
Ok((
NumericArray::UInt8(Arc::new(left)),
NumericArray::UInt8(Arc::new(right)),
))
}
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int16(a) => {
let (left, right) = Arc::try_unwrap(a)
.unwrap_or_else(|arc| (*arc).clone())
.split(index)?;
Ok((
NumericArray::Int16(Arc::new(left)),
NumericArray::Int16(Arc::new(right)),
))
}
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt16(a) => {
let (left, right) = Arc::try_unwrap(a)
.unwrap_or_else(|arc| (*arc).clone())
.split(index)?;
Ok((
NumericArray::UInt16(Arc::new(left)),
NumericArray::UInt16(Arc::new(right)),
))
}
NumericArray::Int32(a) => {
let (left, right) = Arc::try_unwrap(a)
.unwrap_or_else(|arc| (*arc).clone())
.split(index)?;
Ok((
NumericArray::Int32(Arc::new(left)),
NumericArray::Int32(Arc::new(right)),
))
}
NumericArray::Int64(a) => {
let (left, right) = Arc::try_unwrap(a)
.unwrap_or_else(|arc| (*arc).clone())
.split(index)?;
Ok((
NumericArray::Int64(Arc::new(left)),
NumericArray::Int64(Arc::new(right)),
))
}
NumericArray::UInt32(a) => {
let (left, right) = Arc::try_unwrap(a)
.unwrap_or_else(|arc| (*arc).clone())
.split(index)?;
Ok((
NumericArray::UInt32(Arc::new(left)),
NumericArray::UInt32(Arc::new(right)),
))
}
NumericArray::UInt64(a) => {
let (left, right) = Arc::try_unwrap(a)
.unwrap_or_else(|arc| (*arc).clone())
.split(index)?;
Ok((
NumericArray::UInt64(Arc::new(left)),
NumericArray::UInt64(Arc::new(right)),
))
}
NumericArray::Float32(a) => {
let (left, right) = Arc::try_unwrap(a)
.unwrap_or_else(|arc| (*arc).clone())
.split(index)?;
Ok((
NumericArray::Float32(Arc::new(left)),
NumericArray::Float32(Arc::new(right)),
))
}
NumericArray::Float64(a) => {
let (left, right) = Arc::try_unwrap(a)
.unwrap_or_else(|arc| (*arc).clone())
.split(index)?;
Ok((
NumericArray::Float64(Arc::new(left)),
NumericArray::Float64(Arc::new(right)),
))
}
NumericArray::Null => Err(MinarrowError::IndexError(
"Cannot split Null array".to_string(),
)),
}
}
pub fn i32_ref(&self) -> Result<&IntegerArray<i32>, MinarrowError> {
match self {
NumericArray::Int32(a) => Ok(a),
NumericArray::Null => Err(MinarrowError::NullError { message: None }),
_ => Err(MinarrowError::TypeError {
from: "NumericArray",
to: "IntegerArray<i32>",
message: None,
}),
}
}
pub fn i64_ref(&self) -> Result<&IntegerArray<i64>, MinarrowError> {
match self {
NumericArray::Int64(a) => Ok(a),
NumericArray::Null => Err(MinarrowError::NullError { message: None }),
_ => Err(MinarrowError::TypeError {
from: "NumericArray",
to: "IntegerArray<i64>",
message: None,
}),
}
}
pub fn u32_ref(&self) -> Result<&IntegerArray<u32>, MinarrowError> {
match self {
NumericArray::UInt32(a) => Ok(a),
NumericArray::Null => Err(MinarrowError::NullError { message: None }),
_ => Err(MinarrowError::TypeError {
from: "NumericArray",
to: "IntegerArray<u32>",
message: None,
}),
}
}
pub fn u64_ref(&self) -> Result<&IntegerArray<u64>, MinarrowError> {
match self {
NumericArray::UInt64(a) => Ok(a),
NumericArray::Null => Err(MinarrowError::NullError { message: None }),
_ => Err(MinarrowError::TypeError {
from: "NumericArray",
to: "IntegerArray<u64>",
message: None,
}),
}
}
pub fn f32_ref(&self) -> Result<&FloatArray<f32>, MinarrowError> {
match self {
NumericArray::Float32(a) => Ok(a),
NumericArray::Null => Err(MinarrowError::NullError { message: None }),
_ => Err(MinarrowError::TypeError {
from: "NumericArray",
to: "FloatArray<f32>",
message: None,
}),
}
}
pub fn f64_ref(&self) -> Result<&FloatArray<f64>, MinarrowError> {
match self {
NumericArray::Float64(a) => Ok(a),
NumericArray::Null => Err(MinarrowError::NullError { message: None }),
_ => Err(MinarrowError::TypeError {
from: "NumericArray",
to: "FloatArray<f64>",
message: None,
}),
}
}
pub fn i32(self) -> Result<IntegerArray<i32>, MinarrowError> {
match self {
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int8(a) => Ok(IntegerArray::<i32>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int16(a) => Ok(IntegerArray::<i32>::from(&*a)),
NumericArray::Int32(a) => match Arc::try_unwrap(a) {
Ok(inner) => Ok(inner),
Err(shared) => Ok((*shared).clone()),
},
NumericArray::Int64(a) => Ok(IntegerArray::<i32>::try_from(&*a)?),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt8(a) => Ok(IntegerArray::<i32>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt16(a) => Ok(IntegerArray::<i32>::from(&*a)),
NumericArray::UInt32(a) => Ok(IntegerArray::<i32>::try_from(&*a)?),
NumericArray::UInt64(a) => Ok(IntegerArray::<i32>::try_from(&*a)?),
NumericArray::Float32(a) => Ok(IntegerArray::<i32>::try_from(&*a)?),
NumericArray::Float64(a) => Ok(IntegerArray::<i32>::try_from(&*a)?),
NumericArray::Null => Err(MinarrowError::NullError { message: None }),
}
}
pub fn i64(self) -> Result<IntegerArray<i64>, MinarrowError> {
match self {
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int8(a) => Ok(IntegerArray::<i64>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int16(a) => Ok(IntegerArray::<i64>::from(&*a)),
NumericArray::Int32(a) => Ok(IntegerArray::<i64>::from(&*a)),
NumericArray::Int64(a) => match Arc::try_unwrap(a) {
Ok(inner) => Ok(inner),
Err(shared) => Ok((*shared).clone()),
},
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt8(a) => Ok(IntegerArray::<i64>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt16(a) => Ok(IntegerArray::<i64>::from(&*a)),
NumericArray::UInt32(a) => Ok(IntegerArray::<i64>::from(&*a)),
NumericArray::UInt64(a) => Ok(IntegerArray::<i64>::try_from(&*a)?),
NumericArray::Float32(a) => Ok(IntegerArray::<i64>::try_from(&*a)?),
NumericArray::Float64(a) => Ok(IntegerArray::<i64>::try_from(&*a)?),
NumericArray::Null => Err(MinarrowError::NullError { message: None }),
}
}
pub fn u32(self) -> Result<IntegerArray<u32>, MinarrowError> {
match self {
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int8(a) => Ok(IntegerArray::<u32>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int16(a) => Ok(IntegerArray::<u32>::from(&*a)),
NumericArray::Int32(a) => Ok(IntegerArray::<u32>::try_from(&*a)?),
NumericArray::Int64(a) => Ok(IntegerArray::<u32>::try_from(&*a)?),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt8(a) => Ok(IntegerArray::<u32>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt16(a) => Ok(IntegerArray::<u32>::from(&*a)),
NumericArray::UInt32(a) => match Arc::try_unwrap(a) {
Ok(inner) => Ok(inner),
Err(shared) => Ok((*shared).clone()),
},
NumericArray::UInt64(a) => Ok(IntegerArray::<u32>::try_from(&*a)?),
NumericArray::Float32(a) => Ok(IntegerArray::<u32>::try_from(&*a)?),
NumericArray::Float64(a) => Ok(IntegerArray::<u32>::try_from(&*a)?),
NumericArray::Null => Err(MinarrowError::NullError { message: None }),
}
}
pub fn u64(self) -> Result<IntegerArray<u64>, MinarrowError> {
match self {
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int8(a) => Ok(IntegerArray::<u64>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int16(a) => Ok(IntegerArray::<u64>::from(&*a)),
NumericArray::Int32(a) => Ok(IntegerArray::<u64>::from(&*a)),
NumericArray::Int64(a) => Ok(IntegerArray::<u64>::try_from(&*a)?),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt8(a) => Ok(IntegerArray::<u64>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt16(a) => Ok(IntegerArray::<u64>::from(&*a)),
NumericArray::UInt32(a) => Ok(IntegerArray::<u64>::from(&*a)),
NumericArray::UInt64(a) => match Arc::try_unwrap(a) {
Ok(inner) => Ok(inner),
Err(shared) => Ok((*shared).clone()),
},
NumericArray::Float32(a) => Ok(IntegerArray::<u64>::try_from(&*a)?),
NumericArray::Float64(a) => Ok(IntegerArray::<u64>::try_from(&*a)?),
NumericArray::Null => Err(MinarrowError::NullError { message: None }),
}
}
pub fn f32(self) -> Result<FloatArray<f32>, MinarrowError> {
match self {
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int8(a) => Ok(FloatArray::<f32>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int16(a) => Ok(FloatArray::<f32>::from(&*a)),
NumericArray::Int32(a) => Ok(FloatArray::<f32>::from(&*a)),
NumericArray::Int64(a) => Ok(FloatArray::<f32>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt8(a) => Ok(FloatArray::<f32>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt16(a) => Ok(FloatArray::<f32>::from(&*a)),
NumericArray::UInt32(a) => Ok(FloatArray::<f32>::from(&*a)),
NumericArray::UInt64(a) => Ok(FloatArray::<f32>::from(&*a)),
NumericArray::Float32(a) => match Arc::try_unwrap(a) {
Ok(inner) => Ok(inner),
Err(shared) => Ok((*shared).clone()),
},
NumericArray::Float64(a) => Ok(FloatArray::<f32>::from(&*a)),
NumericArray::Null => Err(MinarrowError::NullError { message: None }),
}
}
pub fn cow_into_f64(self) -> Self {
macro_rules! cast_arc {
($arc:expr) => {
match Arc::try_unwrap($arc) {
Ok(owned) => {
let data: Vec64<f64> =
owned.data.as_slice().iter().map(|&v| v as f64).collect();
NumericArray::Float64(Arc::new(FloatArray::new(data, owned.null_mask)))
}
Err(shared) => {
let data: Vec64<f64> =
shared.data.as_slice().iter().map(|&v| v as f64).collect();
NumericArray::Float64(Arc::new(FloatArray::new(
data,
shared.null_mask.clone(),
)))
}
}
};
}
match self {
NumericArray::Float64(_) => self,
NumericArray::Float32(arc) => cast_arc!(arc),
NumericArray::Int32(arc) => cast_arc!(arc),
NumericArray::Int64(arc) => cast_arc!(arc),
NumericArray::UInt32(arc) => cast_arc!(arc),
NumericArray::UInt64(arc) => cast_arc!(arc),
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int8(arc) => cast_arc!(arc),
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int16(arc) => cast_arc!(arc),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt8(arc) => cast_arc!(arc),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt16(arc) => cast_arc!(arc),
NumericArray::Null => {
NumericArray::Float64(Arc::new(FloatArray::new(Vec64::new(), None)))
}
}
}
pub fn f64(self) -> Result<FloatArray<f64>, MinarrowError> {
match self {
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int8(a) => Ok(FloatArray::<f64>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int16(a) => Ok(FloatArray::<f64>::from(&*a)),
NumericArray::Int32(a) => Ok(FloatArray::<f64>::from(&*a)),
NumericArray::Int64(a) => Ok(FloatArray::<f64>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt8(a) => Ok(FloatArray::<f64>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt16(a) => Ok(FloatArray::<f64>::from(&*a)),
NumericArray::UInt32(a) => Ok(FloatArray::<f64>::from(&*a)),
NumericArray::UInt64(a) => Ok(FloatArray::<f64>::from(&*a)),
NumericArray::Float32(a) => Ok(FloatArray::<f64>::from(&*a)),
NumericArray::Float64(a) => match Arc::try_unwrap(a) {
Ok(inner) => Ok(inner),
Err(shared) => Ok((*shared).clone()),
},
NumericArray::Null => Err(MinarrowError::NullError { message: None }),
}
}
pub fn bool(self) -> Result<BooleanArray<u8>, MinarrowError> {
match self {
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int8(a) => Ok(BooleanArray::<u8>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int16(a) => Ok(BooleanArray::<u8>::from(&*a)),
NumericArray::Int32(a) => Ok(BooleanArray::<u8>::from(&*a)),
NumericArray::Int64(a) => Ok(BooleanArray::<u8>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt8(a) => Ok(BooleanArray::<u8>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt16(a) => Ok(BooleanArray::<u8>::from(&*a)),
NumericArray::UInt32(a) => Ok(BooleanArray::<u8>::from(&*a)),
NumericArray::UInt64(a) => Ok(BooleanArray::<u8>::from(&*a)),
NumericArray::Float32(a) => Ok(BooleanArray::<u8>::from(&*a)),
NumericArray::Float64(a) => Ok(BooleanArray::<u8>::from(&*a)),
NumericArray::Null => Err(MinarrowError::NullError { message: None }),
}
}
pub fn str(self) -> Result<StringArray<u32>, MinarrowError> {
match self {
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int8(a) => Ok(StringArray::<u32>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int16(a) => Ok(StringArray::<u32>::from(&*a)),
NumericArray::Int32(a) => Ok(StringArray::<u32>::from(&*a)),
NumericArray::Int64(a) => Ok(StringArray::<u32>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt8(a) => Ok(StringArray::<u32>::from(&*a)),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt16(a) => Ok(StringArray::<u32>::from(&*a)),
NumericArray::UInt32(a) => Ok(StringArray::<u32>::from(&*a)),
NumericArray::UInt64(a) => Ok(StringArray::<u32>::from(&*a)),
NumericArray::Float32(a) => Ok(StringArray::<u32>::from(&*a)),
NumericArray::Float64(a) => Ok(StringArray::<u32>::from(&*a)),
NumericArray::Null => Err(MinarrowError::NullError { message: None }),
}
}
}
impl Display for NumericArray {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int8(arr) => write_numeric_array_with_header(f, "Int8", arr.as_ref()),
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int16(arr) => write_numeric_array_with_header(f, "Int16", arr.as_ref()),
NumericArray::Int32(arr) => write_numeric_array_with_header(f, "Int32", arr.as_ref()),
NumericArray::Int64(arr) => write_numeric_array_with_header(f, "Int64", arr.as_ref()),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt8(arr) => write_numeric_array_with_header(f, "UInt8", arr.as_ref()),
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt16(arr) => write_numeric_array_with_header(f, "UInt16", arr.as_ref()),
NumericArray::UInt32(arr) => write_numeric_array_with_header(f, "UInt32", arr.as_ref()),
NumericArray::UInt64(arr) => write_numeric_array_with_header(f, "UInt64", arr.as_ref()),
NumericArray::Float32(arr) => {
write_numeric_array_with_header(f, "Float32", arr.as_ref())
}
NumericArray::Float64(arr) => {
write_numeric_array_with_header(f, "Float64", arr.as_ref())
}
NumericArray::Null => writeln!(f, "NullNumericArray [0 values]"),
}
}
}
fn write_numeric_array_with_header<T>(
f: &mut Formatter<'_>,
dtype_name: &str,
arr: &(impl MaskedArray<CopyType = T> + Display + ?Sized),
) -> std::fmt::Result {
writeln!(
f,
"NumericArray [{dtype_name}] [{} values] (null count: {})",
arr.len(),
arr.null_count()
)?;
Display::fmt(arr, f)
}
impl Shape for NumericArray {
fn shape(&self) -> ShapeDim {
ShapeDim::Rank1(self.len())
}
}
impl Concatenate for NumericArray {
fn concat(self, other: Self) -> Result<Self, MinarrowError> {
match (self, other) {
#[cfg(feature = "extended_numeric_types")]
(NumericArray::Int8(a), NumericArray::Int8(b)) => {
let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
Ok(NumericArray::Int8(Arc::new(a.concat(b)?)))
}
#[cfg(feature = "extended_numeric_types")]
(NumericArray::Int16(a), NumericArray::Int16(b)) => {
let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
Ok(NumericArray::Int16(Arc::new(a.concat(b)?)))
}
(NumericArray::Int32(a), NumericArray::Int32(b)) => {
let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
Ok(NumericArray::Int32(Arc::new(a.concat(b)?)))
}
(NumericArray::Int64(a), NumericArray::Int64(b)) => {
let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
Ok(NumericArray::Int64(Arc::new(a.concat(b)?)))
}
#[cfg(feature = "extended_numeric_types")]
(NumericArray::UInt8(a), NumericArray::UInt8(b)) => {
let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
Ok(NumericArray::UInt8(Arc::new(a.concat(b)?)))
}
#[cfg(feature = "extended_numeric_types")]
(NumericArray::UInt16(a), NumericArray::UInt16(b)) => {
let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
Ok(NumericArray::UInt16(Arc::new(a.concat(b)?)))
}
(NumericArray::UInt32(a), NumericArray::UInt32(b)) => {
let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
Ok(NumericArray::UInt32(Arc::new(a.concat(b)?)))
}
(NumericArray::UInt64(a), NumericArray::UInt64(b)) => {
let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
Ok(NumericArray::UInt64(Arc::new(a.concat(b)?)))
}
(NumericArray::Float32(a), NumericArray::Float32(b)) => {
let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
Ok(NumericArray::Float32(Arc::new(a.concat(b)?)))
}
(NumericArray::Float64(a), NumericArray::Float64(b)) => {
let a = Arc::try_unwrap(a).unwrap_or_else(|arc| (*arc).clone());
let b = Arc::try_unwrap(b).unwrap_or_else(|arc| (*arc).clone());
Ok(NumericArray::Float64(Arc::new(a.concat(b)?)))
}
(NumericArray::Null, NumericArray::Null) => Ok(NumericArray::Null),
(lhs, rhs) => Err(MinarrowError::IncompatibleTypeError {
from: "NumericArray",
to: "NumericArray",
message: Some(format!(
"Cannot concatenate mismatched NumericArray variants: {:?} and {:?}",
variant_name(&lhs),
variant_name(&rhs)
)),
}),
}
}
}
fn variant_name(arr: &NumericArray) -> &'static str {
match arr {
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int8(_) => "Int8",
#[cfg(feature = "extended_numeric_types")]
NumericArray::Int16(_) => "Int16",
NumericArray::Int32(_) => "Int32",
NumericArray::Int64(_) => "Int64",
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt8(_) => "UInt8",
#[cfg(feature = "extended_numeric_types")]
NumericArray::UInt16(_) => "UInt16",
NumericArray::UInt32(_) => "UInt32",
NumericArray::UInt64(_) => "UInt64",
NumericArray::Float32(_) => "Float32",
NumericArray::Float64(_) => "Float64",
NumericArray::Null => "Null",
}
}