Skip to content

Commit

Permalink
Extend string parsing support for Date32 to encompass the timestamp f…
Browse files Browse the repository at this point in the history
…ormat
  • Loading branch information
gruuya committed Jan 5, 2024
1 parent 8fda518 commit 81ea97a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
12 changes: 7 additions & 5 deletions arrow-cast/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7482,9 +7482,9 @@ mod tests {

let a = StringArray::from(vec![
"2000-01-01", // valid date with leading 0s
"2000-01-01T12:00:00", // valid datetime, will throw away the time part
"2000-2-2", // valid date without leading 0s
"2000-00-00", // invalid month and day
"2000-01-01T12:00:00", // date + time is invalid
"2000", // just a year is invalid
]);
let array = Arc::new(a) as ArrayRef;
Expand All @@ -7500,17 +7500,19 @@ mod tests {
assert!(c.is_valid(0)); // "2000-01-01"
assert_eq!(date_value, c.value(0));

assert!(c.is_valid(1)); // "2000-01-01"
assert_eq!(date_value, c.value(1));

let date_value = since(
NaiveDate::from_ymd_opt(2000, 2, 2).unwrap(),
from_ymd(1970, 1, 1).unwrap(),
)
.num_days() as i32;
assert!(c.is_valid(1)); // "2000-2-2"
assert_eq!(date_value, c.value(1));
assert!(c.is_valid(2)); // "2000-2-2"
assert_eq!(date_value, c.value(2));

// test invalid inputs
assert!(!c.is_valid(2)); // "2000-00-00"
assert!(!c.is_valid(3)); // "2000-01-01T12:00:00"
assert!(!c.is_valid(3)); // "2000-00-00"
assert!(!c.is_valid(4)); // "2000"
}

Expand Down
17 changes: 14 additions & 3 deletions arrow-cast/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,11 @@ const ERR_NANOSECONDS_NOT_SUPPORTED: &str = "The dates that can be represented a

fn parse_date(string: &str) -> Option<NaiveDate> {
if string.len() > 10 {
return None;
}
// Try to parse as datetime and return just the date part
return string_to_datetime(&Utc, string)
.map(|dt| dt.date_naive())
.ok();
};
let mut digits = [0; 10];
let mut mask = 0;

Expand Down Expand Up @@ -1488,10 +1491,13 @@ mod tests {
"2020-9-08",
"2020-12-1",
"1690-2-5",
"2020-09-08 01:02:03",
];
for case in cases {
let v = date32_to_datetime(Date32Type::parse(case).unwrap()).unwrap();
let expected: NaiveDate = case.parse().unwrap();
let expected = NaiveDate::parse_from_str(case, "%Y-%m-%d")
.or(NaiveDate::parse_from_str(case, "%Y-%m-%d %H:%M:%S"))
.unwrap();
assert_eq!(v.date(), expected);
}

Expand All @@ -1503,6 +1509,11 @@ mod tests {
"2020-09-08-03",
"2020--04-03",
"2020--",
"2020-09-08 01",
"2020-09-08 01:02",
"2020-09-08 01-02-03",
"2020-9-8 01:02:03",
"2020-09-08 1:2:3",
];
for case in err_cases {
assert_eq!(Date32Type::parse(case), None);
Expand Down

0 comments on commit 81ea97a

Please sign in to comment.