Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/diskgraph/diskgraph-rowpack/src/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2699,7 +2699,12 @@ impl<'p> RowAccess<'p> {
// If a format hint is set (e.g., "%Y%m%d"), convert the integer to
// a string and parse with the format. This handles date-encoded
// integers like YYYYMMDD (20240301) that are NOT epoch values.
// Valid eight-digit "%Y%m%d" dates skip the text round-trip.
if let Some(fmt) = format {
if let Some(secs) = fast.and_then(|parser| parser.parse_compact_ymd_int(value))
{
return Ok(Some(in_clock(floor_parts::<U>(secs, || 0))?));
}
let text = value.to_string();
if let Some((secs, nanos)) = parse_timestamp_text_parts(&text, Some(fmt)) {
return Ok(Some(in_clock(floor_parts::<U>(secs, || nanos))?));
Expand Down
16 changes: 16 additions & 0 deletions packages/diskgraph/diskgraph-rowpack/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,22 @@ impl TimestampFormatParser {
}
}

/// Epoch seconds of an eight-digit integer that `%Y%m%d` reads as a valid
/// date, without formatting it as text.
pub(crate) fn parse_compact_ymd_int(&self, value: i64) -> Option<i64> {
if !matches!(self.kind, TimestampFormatKind::CompactYmd)
|| !(10_000_000..=99_999_999).contains(&value)
{
return None;
}
let date = NaiveDate::from_ymd_opt(
(value / 10_000) as i32,
(value / 100 % 100) as u32,
(value % 100) as u32,
)?;
Some(date.and_hms_opt(0, 0, 0)?.and_utc().timestamp())
}

pub(crate) fn parse_date(&self, value: &str) -> Option<i32> {
let secs = self.parse_timestamp(value)?;
Some(days_from_secs(secs))
Expand Down
33 changes: 33 additions & 0 deletions packages/diskgraph/diskgraph-rowpack/src/tests_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1832,6 +1832,39 @@ fn int32_yyyymmdd_format_takes_priority_over_unit() {
assert_eq!(secs, expected);
}

#[test]
fn integer_yyyymmdd_matches_text_parse() {
let mut values = vec![
i64::MIN,
-20240301,
0,
2024031,
9_999_999,
100_000_000,
i64::MAX,
];
for year in [1000, 1969, 2000, 2023, 2024, 9999] {
for month in 0..=13 {
for day in 0..=32 {
values.push(year * 10_000 + month * 100 + day);
}
}
}
for value in values {
let array = || Arc::new(arrow_array::Int64Array::from(vec![value])) as ArrayRef;
let expected = match parse_timestamp_text(&value.to_string(), Some("%Y%m%d")) {
Some(secs) => Ok(Some(secs)),
None => get_timestamp_from_array(array(), None, None),
};
let actual = get_timestamp_from_array(array(), Some("%Y%m%d"), None);
assert_eq!(
actual.map_err(|error| error.to_string()),
expected.map_err(|error| error.to_string()),
"{value}"
);
}
}

// ---- detect_encoded_int_format tests ----

#[test]
Expand Down
Loading