use std::fmt;
use jiff::Span;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure;
use vortex_error::vortex_err;
use crate::dtype::DType;
use crate::dtype::Nullability;
use crate::dtype::PType;
use crate::dtype::extension::ExtDType;
use crate::dtype::extension::ExtId;
use crate::dtype::extension::ExtVTable;
use crate::extension::datetime::TimeUnit;
use crate::scalar::ScalarValue;
const EPOCH: jiff::civil::Date = jiff::civil::Date::constant(1970, 1, 1);
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct Date;
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),
}
}
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")
}
}
pub enum DateValue {
Days(i32),
Milliseconds(i64),
}
impl fmt::Display for DateValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let date = match self {
DateValue::Days(days) => EPOCH + Span::new().days(*days),
DateValue::Milliseconds(ms) => EPOCH + Span::new().milliseconds(*ms),
};
write!(f, "{}", date)
}
}
impl ExtVTable for Date {
type Metadata = TimeUnit;
type NativeValue<'a> = DateValue;
fn id(&self) -> ExtId {
ExtId::new_ref("vortex.date")
}
fn serialize_metadata(&self, metadata: &Self::Metadata) -> VortexResult<Vec<u8>> {
Ok(vec![u8::from(*metadata)])
}
fn deserialize_metadata(&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 unpack_native(
&self,
metadata: &Self::Metadata,
_storage_dtype: &DType,
storage_value: &ScalarValue,
) -> VortexResult<Self::NativeValue<'_>> {
match metadata {
TimeUnit::Milliseconds => Ok(DateValue::Milliseconds(
storage_value.as_primitive().cast::<i64>()?,
)),
TimeUnit::Days => Ok(DateValue::Days(storage_value.as_primitive().cast::<i32>()?)),
_ => vortex_bail!("Date type does not support time unit {}", metadata),
}
}
}
#[cfg(test)]
mod tests {
use vortex_error::VortexResult;
use crate::dtype::DType;
use crate::dtype::Nullability::Nullable;
use crate::extension::datetime::Date;
use crate::extension::datetime::TimeUnit;
use crate::scalar::PValue;
use crate::scalar::Scalar;
use crate::scalar::ScalarValue;
#[test]
fn validate_date_scalar() -> VortexResult<()> {
let days_dtype = DType::Extension(Date::new(TimeUnit::Days, Nullable).erased());
Scalar::try_new(days_dtype, Some(ScalarValue::Primitive(PValue::I32(0))))?;
let ms_dtype = DType::Extension(Date::new(TimeUnit::Milliseconds, Nullable).erased());
Scalar::try_new(
ms_dtype,
Some(ScalarValue::Primitive(PValue::I64(86_400_000))),
)?;
Ok(())
}
#[test]
fn reject_date_with_overflowing_value() {
let dtype = DType::Extension(Date::new(TimeUnit::Days, Nullable).erased());
let result = Scalar::try_new(dtype, Some(ScalarValue::Primitive(PValue::I64(i64::MAX))));
assert!(result.is_err());
}
#[test]
fn display_date_scalar() {
let dtype = DType::Extension(Date::new(TimeUnit::Days, Nullable).erased());
let scalar = Scalar::new(dtype.clone(), Some(ScalarValue::Primitive(PValue::I32(0))));
assert_eq!(format!("{}", scalar.as_extension()), "1970-01-01");
let scalar = Scalar::new(dtype, Some(ScalarValue::Primitive(PValue::I32(365))));
assert_eq!(format!("{}", scalar.as_extension()), "1971-01-01");
}
}