Skip to content

Commit

Permalink
Bugfix: expressing date in rfc3339. unit test + integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
fulmicoton committed Aug 2, 2024
1 parent b418ee7 commit c22c245
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
22 changes: 10 additions & 12 deletions quickwit/quickwit-datetime/src/date_time_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,6 @@ fn build_zone_offset(_: &str) -> Option<OwnedFormatItem> {
Some(OwnedFormatItem::Compound(items))
}

fn build_day_item(ptn: &str) -> Option<OwnedFormatItem> {
let mut day = Day::default();
if ptn.len() == 2 {
day.padding = Padding::Zero;
} else {
day.padding = Padding::None;
};
Some(OwnedFormatItem::Component(Component::Day(day)))
}

fn build_year_item(ptn: &str) -> Option<OwnedFormatItem> {
let year_repr = if ptn.len() == 4 {
YearRepr::Full
Expand All @@ -95,6 +85,16 @@ fn build_month_item(ptn: &str) -> Option<OwnedFormatItem> {
Some(OwnedFormatItem::Component(Component::Month(month)))
}

fn build_day_item(ptn: &str) -> Option<OwnedFormatItem> {
let mut day = Day::default();
if ptn.len() == 2 {
day.padding = Padding::Zero;
} else {
day.padding = Padding::None;
};
Some(OwnedFormatItem::Component(Component::Day(day)))
}

fn build_hour_item(ptn: &str) -> Option<OwnedFormatItem> {
let mut hour = Hour::default();
if ptn.len() == 2 {
Expand Down Expand Up @@ -167,8 +167,6 @@ fn resolve_java_datetime_format_alias(java_datetime_format: &str) -> &str {
let java_datetime_format_map = JAVA_DATE_FORMAT_ALIASES.get_or_init(|| {
let mut m = HashMap::new();
m.insert("date_optional_time", "yyyy-MM-dd['T'HH:mm:ss.SSSZ]");
// m.insert("date_optional_time", "yyyy-MM-dd['T'HH:]");
// m.insert("date_optional_time", "yyyy-MM-dd");
m.insert("strict_date_optional_time", "yyyy-MM-dd'T['HH:mm:ss.SSSZ]");
m.insert(
"strict_date_optional_time_nanos",
Expand Down
1 change: 1 addition & 0 deletions quickwit/quickwit-query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ serde = { workspace = true }
serde_json = { workspace = true }
serde_with = { workspace = true }
tantivy = { workspace = true }
time = { workspace = true }
thiserror = { workspace = true }
whichlang = { workspace = true, optional = true }

Expand Down
40 changes: 39 additions & 1 deletion quickwit/quickwit-query/src/elastic_query_dsl/range_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::ops::Bound;

use quickwit_datetime::StrptimeParser;
use serde::Deserialize;
use time::format_description::well_known::Rfc3339;

use crate::elastic_query_dsl::one_field_map::OneFieldMap;
use crate::elastic_query_dsl::ConvertableToQueryAst;
Expand Down Expand Up @@ -100,8 +101,45 @@ fn parse_and_convert(literal: JsonLiteral, parser: &StrptimeParser) -> anyhow::R
let parsed_date_time = parser
.parse_date_time(&date_time_str)
.map_err(|reason| anyhow::anyhow!("Failed to parse date time: {}", reason))?;
Ok(JsonLiteral::String(parsed_date_time.to_string()))
let parsed_date_time_rfc3339 = parsed_date_time.format(&Rfc3339)?;
Ok(JsonLiteral::String(parsed_date_time_rfc3339))
} else {
Ok(literal)
}
}

#[cfg(test)]
mod tests {
use std::ops::Bound;

use super::{RangeQuery as ElasticRangeQuery, RangeQueryParams as ElasticRangeQueryParams};
use crate::elastic_query_dsl::ConvertableToQueryAst;
use crate::query_ast::{QueryAst, RangeQuery};
use crate::JsonLiteral;

#[test]
fn test_date_range_query_with_format() {
let range_query_params = ElasticRangeQueryParams {
gt: Some(JsonLiteral::String("2021-01-03T13:32:43".to_string())),
gte: None,
lt: None,
lte: None,
boost: None,
format: JsonLiteral::String("yyyy-MM-dd['T'HH:mm:ss]".to_string()).into(),
};
let range_query = ElasticRangeQuery {
field: "date".to_string(),
value: range_query_params,
};
let range_query_ast = range_query.convert_to_query_ast().unwrap();
assert!(matches!(
range_query_ast,
QueryAst::Range(RangeQuery {
field,
lower_bound: Bound::Excluded(lower_bound),
upper_bound: Bound::Unbounded,
})
if field == "date" && lower_bound == JsonLiteral::String("2021-01-03T13:32:43Z".to_string())
));
}
}

0 comments on commit c22c245

Please sign in to comment.