use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_ensure;
use vortex_error::vortex_err;
use crate::DType;
use crate::ExtDType;
use crate::Nullability;
use crate::PType;
use crate::datetime::TimeUnit;
use crate::extension::ExtDTypeVTable;
use crate::extension::ExtID;
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct Date;
impl Date {
pub fn try_new(time_unit: TimeUnit, nullability: Nullability) -> VortexResult<ExtDType<Self>> {
let ptype = date_ptype(&time_unit)
.ok_or_else(|| vortex_err!("Date type does not support time unit {}", time_unit))?;
ExtDType::try_new(time_unit, DType::Primitive(ptype, nullability))
}
pub fn new(time_unit: TimeUnit, nullability: Nullability) -> ExtDType<Self> {
Self::try_new(time_unit, nullability).vortex_expect("failed to create date dtype")
}
}
impl ExtDTypeVTable for Date {
type Metadata = TimeUnit;
fn id(&self) -> ExtID {
ExtID::new_ref("vortex.date")
}
fn serialize(&self, metadata: &Self::Metadata) -> VortexResult<Vec<u8>> {
Ok(vec![u8::from(*metadata)])
}
fn deserialize(&self, metadata: &[u8]) -> VortexResult<Self::Metadata> {
let tag = metadata[0];
TimeUnit::try_from(tag)
}
fn validate_dtype(&self, metadata: &Self::Metadata, storage_dtype: &DType) -> VortexResult<()> {
let ptype = date_ptype(metadata)
.ok_or_else(|| vortex_err!("Date type does not support time unit {}", metadata))?;
vortex_ensure!(
storage_dtype.as_ptype() == ptype,
"Date storage dtype for {} must be {}",
metadata,
ptype
);
Ok(())
}
}
fn date_ptype(time_unit: &TimeUnit) -> Option<PType> {
match time_unit {
TimeUnit::Nanoseconds => None,
TimeUnit::Microseconds => None,
TimeUnit::Milliseconds => Some(PType::I64),
TimeUnit::Seconds => None,
TimeUnit::Days => Some(PType::I32),
}
}