Skip to content

Commit

Permalink
fix(cubesql): Fix time dimension range filter chaining with OR oper…
Browse files Browse the repository at this point in the history
…ator
  • Loading branch information
MazterQyou committed May 24, 2024
1 parent 96c1549 commit 757c4c5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
68 changes: 68 additions & 0 deletions rust/cubesql/cubesql/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22794,4 +22794,72 @@ LIMIT {{ limit }}{% endif %}"#.to_string(),
}
);
}

#[tokio::test]
async fn test_time_dimension_range_filter_chain_or() {
init_logger();

let logical_plan = convert_select_to_query_plan(
r#"
SELECT
"customer_gender",
date_trunc('day', "order_date") AS "order_date"
FROM "KibanaSampleDataEcommerce"
WHERE
("order_date" >= '2019-01-01 00:00:00.0' AND "order_date" < '2020-01-01 00:00:00.0')
OR ("order_date" >= '2021-01-01 00:00:00.0' AND "order_date" < '2022-01-01 00:00:00.0')
GROUP BY 1, 2
"#
.to_string(),
DatabaseProtocol::PostgreSQL,
)
.await
.as_logical_plan();

assert_eq!(
logical_plan.find_cube_scan().request,
V1LoadRequestQuery {
measures: Some(vec![]),
dimensions: Some(vec!["KibanaSampleDataEcommerce.customer_gender".to_string()]),
segments: Some(vec![]),
time_dimensions: Some(vec![V1LoadRequestQueryTimeDimension {
dimension: "KibanaSampleDataEcommerce.order_date".to_owned(),
granularity: Some("day".to_owned()),
date_range: None
}]),
order: None,
limit: None,
offset: None,
filters: Some(vec![V1LoadRequestQueryFilterItem {
member: None,
operator: None,
values: None,
or: Some(vec![
json!(V1LoadRequestQueryFilterItem {
member: Some("KibanaSampleDataEcommerce.order_date".to_string()),
operator: Some("inDateRange".to_string()),
values: Some(vec![
"2019-01-01 00:00:00.0".to_string(),
"2020-01-01 00:00:00.0".to_string(),
]),
or: None,
and: None,
}),
json!(V1LoadRequestQueryFilterItem {
member: Some("KibanaSampleDataEcommerce.order_date".to_string()),
operator: Some("inDateRange".to_string()),
values: Some(vec![
"2021-01-01 00:00:00.0".to_string(),
"2022-01-01 00:00:00.0".to_string(),
]),
or: None,
and: None,
}),
]),
and: None
}]),
ungrouped: None,
}
)
}
}
13 changes: 9 additions & 4 deletions rust/cubesql/cubesql/src/compile/rewrite/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,7 @@ impl LanguageToLogicalPlanConverter {
query_time_dimensions: &mut Vec<V1LoadRequestQueryTimeDimension>,
filters: Vec<LogicalPlanLanguage>,
node_by_id: &impl Index<Id, Output = LogicalPlanLanguage>,
is_in_and_or: bool,
) -> Result<
(
Vec<V1LoadRequestQueryFilterItem>,
Expand All @@ -1757,8 +1758,12 @@ impl LanguageToLogicalPlanConverter {
);
let op =
match_data_node!(node_by_id, params[1], FilterOpOp);
let (filters, segments, change_user) =
to_filter(query_time_dimensions, filters, node_by_id)?;
let (filters, segments, change_user) = to_filter(
query_time_dimensions,
filters,
node_by_id,
true,
)?;
match op.as_str() {
"and" => {
result.push(V1LoadRequestQueryFilterItem {
Expand Down Expand Up @@ -1822,7 +1827,7 @@ impl LanguageToLogicalPlanConverter {
params[2],
FilterMemberValues
);
if op == "inDateRange" {
if !is_in_and_or && op == "inDateRange" {
let existing_time_dimension =
query_time_dimensions.iter_mut().find_map(|td| {
if td.dimension == member
Expand Down Expand Up @@ -1886,7 +1891,7 @@ impl LanguageToLogicalPlanConverter {
}

let (filters, segments, change_user) =
to_filter(&mut query_time_dimensions, filters, node_by_id)?;
to_filter(&mut query_time_dimensions, filters, node_by_id, false)?;

query.filters = if filters.len() > 0 {
Some(filters)
Expand Down

0 comments on commit 757c4c5

Please sign in to comment.