Skip to content

Commit

Permalink
chore: implement parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
etolbakov authored Jul 10, 2024
1 parent a415f30 commit 6887b47
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions quickwit/quickwit-query/src/elastic_query_dsl/range_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use std::ops::Bound;
use std::str::FromStr;

use quickwit_datetime::StrptimeParser;
use serde::Deserialize;
use quickwit_datetime::{DateTimeInputFormat, parse_date_time_str, StrptimeParser};

use crate::elastic_query_dsl::one_field_map::OneFieldMap;
use crate::elastic_query_dsl::ConvertableToQueryAst;
Expand Down Expand Up @@ -59,13 +59,15 @@ impl ConvertableToQueryAst for RangeQuery {
boost,
format,
} = self.value;
let (gt, gte, lt, lte) =
if let Some(JsonLiteral::String(fmt)) = format {
let (gt, gte, lt, lte) = if let Some(JsonLiteral::String(fmt)) = format {
let parser = StrptimeParser::from_str(&fmt).map_err(|reason| {
anyhow::anyhow!("failed to create parser from : {}; reason: {}", fmt, reason)
})?;
(
gt.map(|v| parse_and_convert(v, &fmt)).transpose()?,
gte.map(|v| parse_and_convert(v, &fmt)).transpose()?,
lt.map(|v| parse_and_convert(v, &fmt)).transpose()?,
lte.map(|v| parse_and_convert(v, &fmt)).transpose()?,
gt.map(|v| parse_and_convert(v, &parser)).transpose()?,
gte.map(|v| parse_and_convert(v, &parser)).transpose()?,
lt.map(|v| parse_and_convert(v, &parser)).transpose()?,
lte.map(|v| parse_and_convert(v, &parser)).transpose()?,
)
} else {
(gt, gte, lt, lte)
Expand Down Expand Up @@ -95,14 +97,13 @@ impl ConvertableToQueryAst for RangeQuery {
}
}


fn parse_and_convert(value: JsonLiteral, fmt: &str) -> anyhow::Result<JsonLiteral> {
if let JsonLiteral::String(s) = value {
let date_format = DateTimeInputFormat::from_str(fmt).unwrap();
let date_time = parse_date_time_str(&s, &[date_format]).unwrap();
Ok(JsonLiteral::String(date_time.to_string())) // TODO no `to_string` method
fn parse_and_convert(literal: JsonLiteral, parser: &StrptimeParser) -> anyhow::Result<JsonLiteral> {
if let JsonLiteral::String(date_time_str) = literal {
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()))
} else {
dbg!(value.clone());
Ok(value)
Ok(literal)
}
}

0 comments on commit 6887b47

Please sign in to comment.