Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse datetimes and timestamps with leading and/or trailing whitespace #5544

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 14 additions & 12 deletions quickwit/quickwit-datetime/src/date_time_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,24 @@ pub fn parse_date_time_str(
date_time_str: &str,
date_time_formats: &[DateTimeInputFormat],
) -> Result<TantivyDateTime, String> {
let trimmed_date_time_str = date_time_str.trim();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let trimmed_date_time_str = date_time_str.trim();
let trimmed_date_time_str = date_time_str.trim_ascii();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is probably minor, but triming unicode is considerably more expensive than trimming ascii.
(It requires decoding utf-8, and check if each individual char is a whitespace or not. )

I'd feel safer if we restricted ourselves to ascii. It will just prevent us from trimming weird whitespace like the japanese " ".


for date_time_format in date_time_formats {
let date_time_opt = match date_time_format {
DateTimeInputFormat::Iso8601 => parse_iso8601(date_time_str)
DateTimeInputFormat::Iso8601 => parse_iso8601(trimmed_date_time_str)
.map(TantivyDateTime::from_utc)
.ok(),
DateTimeInputFormat::Rfc2822 => parse_rfc2822(date_time_str)
DateTimeInputFormat::Rfc2822 => parse_rfc2822(trimmed_date_time_str)
.map(TantivyDateTime::from_utc)
.ok(),
DateTimeInputFormat::Rfc3339 => parse_rfc3339(date_time_str)
DateTimeInputFormat::Rfc3339 => parse_rfc3339(trimmed_date_time_str)
.map(TantivyDateTime::from_utc)
.ok(),
DateTimeInputFormat::Strptime(parser) => parser
.parse_date_time(date_time_str)
.parse_date_time(trimmed_date_time_str)
.map(TantivyDateTime::from_utc)
.ok(),
DateTimeInputFormat::Timestamp => parse_timestamp_str(date_time_str),
DateTimeInputFormat::Timestamp => parse_timestamp_str(trimmed_date_time_str),
};
if let Some(date_time) = date_time_opt {
return Ok(date_time);
Expand Down Expand Up @@ -80,7 +82,7 @@ pub fn parse_timestamp_float(
));
}
let duration_since_epoch = Duration::try_from_secs_f64(timestamp)
.map_err(|error| format!("Failed to parse datetime `{timestamp}`: {error}"))?;
.map_err(|error| format!("failed to parse datetime `{timestamp}`: {error}"))?;
let timestamp_nanos = duration_since_epoch.as_nanos() as i64;
Ok(TantivyDateTime::from_timestamp_nanos(timestamp_nanos))
}
Expand Down Expand Up @@ -291,14 +293,14 @@ mod tests {
#[test]
fn test_parse_date_time_str() {
for date_time_str in [
"20120521T120914Z",
"Mon, 21 May 2012 12:09:14 GMT",
"2012-05-21T12:09:14-00:00",
"20120521T120914Z ",
" Mon, 21 May 2012 12:09:14 GMT",
" 2012-05-21T12:09:14-00:00 ",
"2012-05-21 12:09:14",
"2012/05/21 12:09:14",
" 2012/05/21 12:09:14",
"2012/05/21 12:09:14 +00:00",
"1337602154",
"1337602154.0",
"1337602154 ",
" 1337602154.0 ",
] {
let date_time = parse_date_time_str(
date_time_str,
Expand Down
Loading