use std::any::TypeId;
use std::fmt::{Display, Formatter, Result as FmtResult};
#[cfg(feature = "datetime")]
use crate::DatetimeArray;
#[cfg(feature = "datetime")]
use crate::enums::time_units::{IntervalUnit, TimeUnit};
use crate::{BooleanArray, CategoricalArray, Float, FloatArray, Integer, StringArray};
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub enum ArrowType {
Null,
Boolean,
#[cfg(feature = "extended_numeric_types")]
Int8,
#[cfg(feature = "extended_numeric_types")]
Int16,
Int32,
Int64,
#[cfg(feature = "extended_numeric_types")]
UInt8,
#[cfg(feature = "extended_numeric_types")]
UInt16,
UInt32,
UInt64,
Float32,
Float64,
#[cfg(feature = "datetime")]
Date32,
#[cfg(feature = "datetime")]
Date64,
#[cfg(feature = "datetime")]
Time32(TimeUnit),
#[cfg(feature = "datetime")]
Time64(TimeUnit),
#[cfg(feature = "datetime")]
Duration32(TimeUnit),
#[cfg(feature = "datetime")]
Duration64(TimeUnit),
#[cfg(feature = "datetime")]
Timestamp(TimeUnit, Option<String>), #[cfg(feature = "datetime")]
Interval(IntervalUnit),
String,
#[cfg(feature = "large_string")]
LargeString,
Utf8View,
Dictionary(CategoricalIndexType),
}
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub enum CategoricalIndexType {
#[cfg(feature = "default_categorical_8")]
UInt8,
#[cfg(feature = "extended_categorical")]
UInt16,
#[cfg(any(not(feature = "default_categorical_8"), feature = "extended_categorical"))]
UInt32,
#[cfg(feature = "extended_categorical")]
UInt64,
}
impl BooleanArray<()> {
pub fn arrow_type() -> ArrowType {
ArrowType::Boolean
}
}
impl<T: Integer> CategoricalArray<T> {
pub fn arrow_type() -> ArrowType {
let t = TypeId::of::<T>();
#[cfg(feature = "default_categorical_8")]
if t == TypeId::of::<u8>() {
return ArrowType::Dictionary(CategoricalIndexType::UInt8);
}
#[cfg(feature = "extended_categorical")]
if t == TypeId::of::<u16>() {
return ArrowType::Dictionary(CategoricalIndexType::UInt16);
}
#[cfg(any(not(feature = "default_categorical_8"), feature = "extended_categorical"))]
if t == TypeId::of::<u32>() {
return ArrowType::Dictionary(CategoricalIndexType::UInt32);
}
#[cfg(feature = "extended_categorical")]
if t == TypeId::of::<u64>() {
return ArrowType::Dictionary(CategoricalIndexType::UInt64);
}
unsafe { std::hint::unreachable_unchecked() }
}
}
impl<T: Float> FloatArray<T> {
pub fn arrow_type() -> ArrowType {
let t = TypeId::of::<T>();
if t == TypeId::of::<f32>() {
ArrowType::Float32
} else if t == TypeId::of::<f64>() {
ArrowType::Float64
} else {
unsafe { std::hint::unreachable_unchecked() }
}
}
}
impl<T: Integer> StringArray<T> {
pub fn arrow_type() -> ArrowType {
let t = TypeId::of::<T>();
if t == TypeId::of::<u32>() {
return ArrowType::String;
}
#[cfg(feature = "large_string")]
if t == TypeId::of::<u64>() {
return ArrowType::LargeString;
}
unsafe { std::hint::unreachable_unchecked() }
}
}
#[cfg(feature = "datetime")]
impl<T: Integer> DatetimeArray<T> {
pub fn arrow_type() -> ArrowType {
ArrowType::Null
}
}
impl Display for ArrowType {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
ArrowType::Null => f.write_str("Null"),
ArrowType::Boolean => f.write_str("Boolean"),
#[cfg(feature = "extended_numeric_types")]
ArrowType::Int8 => f.write_str("Int8"),
#[cfg(feature = "extended_numeric_types")]
ArrowType::Int16 => f.write_str("Int16"),
ArrowType::Int32 => f.write_str("Int32"),
ArrowType::Int64 => f.write_str("Int64"),
#[cfg(feature = "extended_numeric_types")]
ArrowType::UInt8 => f.write_str("UInt8"),
#[cfg(feature = "extended_numeric_types")]
ArrowType::UInt16 => f.write_str("UInt16"),
ArrowType::UInt32 => f.write_str("UInt32"),
ArrowType::UInt64 => f.write_str("UInt64"),
ArrowType::Float32 => f.write_str("Float32"),
ArrowType::Float64 => f.write_str("Float64"),
#[cfg(feature = "datetime")]
ArrowType::Date32 => f.write_str("Date32"),
#[cfg(feature = "datetime")]
ArrowType::Date64 => f.write_str("Date64"),
#[cfg(feature = "datetime")]
ArrowType::Time32(unit) => write!(f, "Time32({unit})"),
#[cfg(feature = "datetime")]
ArrowType::Time64(unit) => write!(f, "Time64({unit})"),
#[cfg(feature = "datetime")]
ArrowType::Duration32(unit) => write!(f, "Duration32({unit})"),
#[cfg(feature = "datetime")]
ArrowType::Duration64(unit) => write!(f, "Duration64({unit})"),
#[cfg(feature = "datetime")]
ArrowType::Timestamp(unit, tz) => {
if let Some(tz_str) = tz {
write!(f, "Timestamp({unit}, {})", tz_str)
} else {
write!(f, "Timestamp({unit})")
}
}
#[cfg(feature = "datetime")]
ArrowType::Interval(interval) => write!(f, "Interval({interval})"),
ArrowType::String => f.write_str("String"),
#[cfg(feature = "large_string")]
ArrowType::LargeString => f.write_str("LargeString"),
ArrowType::Utf8View => f.write_str("Utf8View"),
ArrowType::Dictionary(key_type) => write!(f, "Dictionary({key_type})"),
}
}
}
impl Display for CategoricalIndexType {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
#[cfg(feature = "default_categorical_8")]
CategoricalIndexType::UInt8 => f.write_str("UInt8"),
#[cfg(feature = "extended_categorical")]
CategoricalIndexType::UInt16 => f.write_str("UInt16"),
#[cfg(any(not(feature = "default_categorical_8"), feature = "extended_categorical"))]
CategoricalIndexType::UInt32 => f.write_str("UInt32"),
#[cfg(feature = "extended_categorical")]
CategoricalIndexType::UInt64 => f.write_str("UInt64"),
}
}
}