Skip to content

Commit aac0907

Browse files
MazterQyoumcheshkov
authored andcommitted
feat: Parse timestamp strings as Date32
1 parent fb96263 commit aac0907

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

arrow/src/compute/kernels/cast.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,11 +1703,20 @@ fn parse_naive_date(
17031703
}
17041704
}
17051705

1706-
if let Some(d) = delimiter {
1707-
chrono::NaiveDate::parse_from_str(v, &format!("%Y{}%m{}%d", d, d))
1708-
} else {
1709-
chrono::NaiveDate::parse_from_str(v, "%Y%m%d")
1710-
}
1706+
if let Ok(result) = {
1707+
if let Some(d) = delimiter {
1708+
chrono::NaiveDate::parse_from_str(v, &format!("%Y{}%m{}%d", d, d))
1709+
} else {
1710+
chrono::NaiveDate::parse_from_str(v, "%Y%m%d")
1711+
}
1712+
} {
1713+
return Ok(result);
1714+
};
1715+
1716+
// Try to parse full timestamp, this is valid in Postgres
1717+
chrono::NaiveDate::parse_from_str(v, "%Y-%m-%dT%H:%M:%S%.fZ")
1718+
.or_else(|_| chrono::NaiveDate::parse_from_str(v, "%Y-%m-%d %H:%M:%S%.f"))
1719+
.or_else(|_| chrono::NaiveDate::parse_from_str(v, "%Y-%m-%d %H:%M:%S"))
17111720
}
17121721

17131722
/// Casts generic string arrays to Date32Array
@@ -3263,20 +3272,23 @@ mod tests {
32633272
fn test_cast_string_to_date32() {
32643273
let a1 = Arc::new(StringArray::from(vec![
32653274
Some("2018-12-25"),
3275+
Some("2018-12-26 00:00:00"),
32663276
Some("Not a valid date"),
32673277
None,
32683278
])) as ArrayRef;
32693279
let a2 = Arc::new(LargeStringArray::from(vec![
32703280
Some("2018-12-25"),
3281+
Some("2018-12-26 00:00:00"),
32713282
Some("Not a valid date"),
32723283
None,
32733284
])) as ArrayRef;
32743285
for array in &[a1, a2] {
32753286
let b = cast(array, &DataType::Date32).unwrap();
32763287
let c = b.as_any().downcast_ref::<Date32Array>().unwrap();
32773288
assert_eq!(17890, c.value(0));
3278-
assert!(c.is_null(1));
3289+
assert_eq!(17891, c.value(1));
32793290
assert!(c.is_null(2));
3291+
assert!(c.is_null(3));
32803292
}
32813293
}
32823294

0 commit comments

Comments
 (0)