Skip to content

Commit cb88a79

Browse files
authored
fix: parsing timestamp with date format (#10476)
* fix parsing timestamp with date format * add test in dates.slt
1 parent 405a5f6 commit cb88a79

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

datafusion/functions/src/datetime/common.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ pub(crate) fn string_to_datetime_formatted<T: TimeZone>(
9393

9494
if let Err(e) = &dt {
9595
// no timezone or other failure, try without a timezone
96-
let ndt = parsed.to_naive_datetime_with_offset(0);
96+
let ndt = parsed
97+
.to_naive_datetime_with_offset(0)
98+
.or_else(|_| parsed.to_naive_date().map(|nd| nd.into()));
9799
if let Err(e) = &ndt {
98100
return Err(err(&e.to_string()));
99101
}

datafusion/functions/src/datetime/to_timestamp.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,10 @@ mod tests {
670670
parse_timestamp_formatted("09-08-2020 13/42/29", "%m-%d-%Y %H/%M/%S")
671671
.unwrap()
672672
);
673+
assert_eq!(
674+
1642896000000000000,
675+
parse_timestamp_formatted("2022-01-23", "%Y-%m-%d").unwrap()
676+
);
673677
}
674678

675679
fn parse_timestamp_formatted(s: &str, format: &str) -> Result<i64, DataFusionError> {

datafusion/sqllogictest/test_files/dates.slt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,11 @@ SELECT to_date(t.ts, '%Y-%m-%d %H/%M/%S%#z', '%s', '%q', '%d-%m-%Y %H:%M:%S%#z',
224224
query error function unsupported data type at index 1:
225225
SELECT to_date(t.ts, make_array('%Y-%m-%d %H/%M/%S%#z', '%s', '%q', '%d-%m-%Y %H:%M:%S%#z', '%+')) from ts_utf8_data as t
226226

227+
# verify to_date with format
228+
query D
229+
select to_date('2022-01-23', '%Y-%m-%d');
230+
----
231+
2022-01-23
232+
227233
statement ok
228234
drop table ts_utf8_data

0 commit comments

Comments
 (0)