Skip to content

Commit

Permalink
Optimize to_timestamp (with format) (apache#9090) (apache#9833)
Browse files Browse the repository at this point in the history
Eliminate duplicate parsing of the input and format strings in some cases

Co-authored-by: Vojtech Toman <[email protected]>
  • Loading branch information
2 people authored and Lordworms committed Apr 1, 2024
1 parent f182c45 commit b213763
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions datafusion/functions/src/datetime/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ use arrow::array::{
};
use arrow::compute::kernels::cast_utils::string_to_timestamp_nanos;
use arrow::datatypes::DataType;
use chrono::format::{parse, Parsed, StrftimeItems};
use chrono::LocalResult::Single;
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
use chrono::{DateTime, TimeZone, Utc};
use itertools::Either;

use datafusion_common::cast::as_generic_string_array;
Expand Down Expand Up @@ -84,12 +85,15 @@ pub(crate) fn string_to_datetime_formatted<T: TimeZone>(
))
};

let mut parsed = Parsed::new();
parse(&mut parsed, s, StrftimeItems::new(format)).map_err(|e| err(&e.to_string()))?;

// attempt to parse the string assuming it has a timezone
let dt = DateTime::parse_from_str(s, format);
let dt = parsed.to_datetime();

if let Err(e) = &dt {
// no timezone or other failure, try without a timezone
let ndt = NaiveDateTime::parse_from_str(s, format);
let ndt = parsed.to_naive_datetime_with_offset(0);
if let Err(e) = &ndt {
return Err(err(&e.to_string()));
}
Expand Down

0 comments on commit b213763

Please sign in to comment.