vortex-array 0.94.0

Vortex in memory columnar data format
Documentation
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

use std::fmt;

use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure;
use vortex_session::registry::CachedId;

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;

/// The Unix epoch date (1970-01-01).
const EPOCH: jiff::civil::Date = jiff::civil::Date::constant(1970, 1, 1);

/// Date DType.
#[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 {
    /// Creates a new Date extension dtype with the given time unit and nullability.
    ///
    /// Note that only Milliseconds and Days time units are supported for Date.
    pub fn try_new(time_unit: TimeUnit, nullability: Nullability) -> VortexResult<ExtDType<Self>> {
        ExtDType::try_new(time_unit, nullability)
    }

    /// Creates a new Date extension dtype with the given time unit and nullability.
    ///
    /// # Panics
    ///
    /// Panics if the `time_unit` is not supported by date types.
    pub fn new(time_unit: TimeUnit, nullability: Nullability) -> ExtDType<Self> {
        Self::try_new(time_unit, nullability).vortex_expect("failed to create date dtype")
    }
}

/// Unpacked value of a [`Date`] extension scalar.
pub enum DateValue {
    /// Days since the Unix epoch.
    Days(i32),
    /// Milliseconds since the Unix epoch.
    Milliseconds(i64),
}

impl fmt::Display for DateValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let (unit, value) = match *self {
            DateValue::Days(days) => (TimeUnit::Days, i64::from(days)),
            DateValue::Milliseconds(ms) => (TimeUnit::Milliseconds, ms),
        };

        // jiff cannot hold every storage value, such as any date after the year 9999.
        let Ok(date) = unit
            .to_jiff_span(value)
            .and_then(|span| Ok(EPOCH.checked_add(span)?))
        else {
            return write!(f, "{value}{unit}");
        };

        write!(f, "{date}")
    }
}

impl ExtVTable for Date {
    type Metadata = TimeUnit;
    type StoredMetadata = Self::Metadata;
    type NativeValue<'a> = DateValue;

    fn id(&self) -> ExtId {
        static ID: CachedId = CachedId::new("vortex.date");
        *ID
    }

    fn serialize_metadata(&self, metadata: &Self::Metadata) -> VortexResult<Vec<u8>> {
        Ok(vec![u8::from(*metadata)])
    }

    fn deserialize_metadata(&self, metadata: &[u8]) -> VortexResult<Self::StoredMetadata> {
        vortex_ensure!(!metadata.is_empty(), "Date metadata must not be empty");
        let tag = metadata[0];
        TimeUnit::try_from(tag)
    }

    fn metadata_from_storage(
        &self,
        stored_metadata: Self::StoredMetadata,
        _storage_dtype: &DType,
    ) -> VortexResult<Self::Metadata> {
        Ok(stored_metadata)
    }

    fn storage_dtype(metadata: &Self::Metadata) -> DType {
        let ptype = date_ptype(metadata)
            .vortex_expect("Date::validate_metadata accepts only days and milliseconds");
        DType::Primitive(ptype, Nullability::NonNullable)
    }

    fn validate_metadata(metadata: &Self::Metadata) -> VortexResult<()> {
        vortex_ensure!(
            date_ptype(metadata).is_some(),
            "Date time unit must be days or milliseconds, got {metadata}"
        );

        Ok(())
    }

    fn unpack_native<'a>(
        ext_dtype: &'a ExtDType<Self>,
        storage_value: &'a ScalarValue,
    ) -> VortexResult<Self::NativeValue<'a>> {
        let metadata = ext_dtype.metadata();
        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 rstest::rstest;
    use vortex_error::VortexResult;

    use crate::dtype::DType;
    use crate::dtype::Nullability::Nullable;
    use crate::dtype::extension::ExtDTypePlugin;
    use crate::extension::datetime::Date;
    use crate::extension::datetime::DateValue;
    use crate::extension::datetime::TimeUnit;
    use crate::scalar::PValue;
    use crate::scalar::Scalar;
    use crate::scalar::ScalarValue;

    #[test]
    fn test_read_rejects_non_primitive_storage() {
        let result = Date.deserialize(&[u8::from(TimeUnit::Days)], DType::Utf8(Nullable));
        assert!(result.is_err_and(|error| {
            error
                .to_string()
                .contains("vortex.date storage must be i32?, got utf8?")
        }));
    }

    #[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() {
        // Days storage is `I32`, so an `I64` value that overflows `i32` should fail the cast.
        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");
    }

    #[rstest]
    #[case::ordinary(DateValue::Milliseconds(86_400_000), "1970-01-02")]
    #[case::after_year_9999(DateValue::Days(2_932_897), "2932897days")]
    #[case::beyond_jiff_span(DateValue::Days(10_000_000), "10000000days")]
    #[case::max_milliseconds(DateValue::Milliseconds(i64::MAX), "9223372036854775807ms")]
    fn display_date_value(#[case] value: DateValue, #[case] expected: &str) {
        assert_eq!(value.to_string(), expected);
    }

    #[test]
    fn deserialize_empty_metadata_returns_error() {
        use crate::dtype::extension::ExtVTable;

        let vtable = Date;
        assert!(vtable.deserialize_metadata(&[]).is_err());
    }

    #[test]
    fn deserialize_invalid_tag_returns_error() {
        use crate::dtype::extension::ExtVTable;

        let vtable = Date;
        // 0xFF is not a valid TimeUnit tag.
        assert!(vtable.deserialize_metadata(&[0xFF]).is_err());
    }
}