Skip to content

Commit d5271f9

Browse files
authored
Display dates beyond jiff's range without panicking (#11187)
## Summary - Tracking Issue: #11114 `Date` accepts every `i32` day count and every `i64` millisecond count, but jiff only holds the years -9999 to 9999, so formatting a valid `Date` outside that range panicked. `Display` now prints the raw value and unit instead (e.g., `10000000days`), like the `Timestamp` fallback from #10953. ## Changes Tests cover the first date after 9999-12-31, day and millisecond counts that a jiff `Span` can't hold, and an ordinary date whose output doesn't change. Signed-off-by: Connor Tsui <[email protected]>
1 parent 93ab9aa commit d5271f9

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

  • vortex-array/src/extension/datetime

‎vortex-array/src/extension/datetime/date.rs‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
use std::fmt;
55

6-
use jiff::Span;
76
use vortex_error::VortexExpect;
87
use vortex_error::VortexResult;
98
use vortex_error::vortex_bail;
@@ -68,11 +67,20 @@ pub enum DateValue {
6867

6968
impl fmt::Display for DateValue {
7069
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71-
let date = match self {
72-
DateValue::Days(days) => EPOCH + Span::new().days(*days),
73-
DateValue::Milliseconds(ms) => EPOCH + Span::new().milliseconds(*ms),
70+
let (unit, value) = match *self {
71+
DateValue::Days(days) => (TimeUnit::Days, i64::from(days)),
72+
DateValue::Milliseconds(ms) => (TimeUnit::Milliseconds, ms),
7473
};
75-
write!(f, "{}", date)
74+
75+
// jiff cannot hold every storage value, such as any date after the year 9999.
76+
let Ok(date) = unit
77+
.to_jiff_span(value)
78+
.and_then(|span| Ok(EPOCH.checked_add(span)?))
79+
else {
80+
return write!(f, "{value}{unit}");
81+
};
82+
83+
write!(f, "{date}")
7684
}
7785
}
7886

@@ -128,12 +136,14 @@ impl ExtVTable for Date {
128136

129137
#[cfg(test)]
130138
mod tests {
139+
use rstest::rstest;
131140
use vortex_error::VortexResult;
132141

133142
use crate::dtype::DType;
134143
use crate::dtype::Nullability::Nullable;
135144
use crate::dtype::extension::ExtDType;
136145
use crate::extension::datetime::Date;
146+
use crate::extension::datetime::DateValue;
137147
use crate::extension::datetime::TimeUnit;
138148
use crate::scalar::PValue;
139149
use crate::scalar::Scalar;
@@ -177,6 +187,15 @@ mod tests {
177187
assert_eq!(format!("{}", scalar.as_extension()), "1971-01-01");
178188
}
179189

190+
#[rstest]
191+
#[case::ordinary(DateValue::Milliseconds(86_400_000), "1970-01-02")]
192+
#[case::after_year_9999(DateValue::Days(2_932_897), "2932897days")]
193+
#[case::beyond_jiff_span(DateValue::Days(10_000_000), "10000000days")]
194+
#[case::max_milliseconds(DateValue::Milliseconds(i64::MAX), "9223372036854775807ms")]
195+
fn display_date_value(#[case] value: DateValue, #[case] expected: &str) {
196+
assert_eq!(value.to_string(), expected);
197+
}
198+
180199
#[test]
181200
fn deserialize_empty_metadata_returns_error() {
182201
use crate::dtype::extension::ExtVTable;

0 commit comments

Comments
 (0)