Skip to content

Commit 803998f

Browse files
authored
fix(cubesql): Do not merge time dimension ranges in "or" filter into date range (#9609)
Signed-off-by: Alex Qyoun-ae <[email protected]>
1 parent 4e322ae commit 803998f

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

rust/cubesql/cubesql/src/compile/mod.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16739,4 +16739,95 @@ LIMIT {{ limit }}{% endif %}"#.to_string(),
1673916739
displayable(physical_plan.as_ref()).indent()
1674016740
);
1674116741
}
16742+
16743+
#[tokio::test]
16744+
async fn test_date_filter_with_or_and() {
16745+
init_testing_logger();
16746+
16747+
let logical_plan = convert_select_to_query_plan(
16748+
r#"
16749+
SELECT
16750+
DATE_TRUNC('year', "order_date") AS "y",
16751+
SUM("KibanaSampleDataEcommerce"."sumPrice") AS "m1"
16752+
FROM "KibanaSampleDataEcommerce" AS "KibanaSampleDataEcommerce"
16753+
WHERE
16754+
DATE_TRUNC('year', "order_date") = '2024-01-01T00:00:00Z'::timestamptz
16755+
OR (
16756+
DATE_TRUNC('year', "order_date") = '2025-01-01T00:00:00Z'::timestamptz
16757+
AND DATE_TRUNC('month', "order_date") = '2025-01-01T00:00:00Z'::timestamptz
16758+
)
16759+
GROUP BY 1
16760+
"#
16761+
.to_string(),
16762+
DatabaseProtocol::PostgreSQL,
16763+
)
16764+
.await
16765+
.as_logical_plan();
16766+
16767+
assert_eq!(
16768+
logical_plan.find_cube_scan().request,
16769+
V1LoadRequestQuery {
16770+
measures: Some(vec!["KibanaSampleDataEcommerce.sumPrice".to_string()]),
16771+
dimensions: Some(vec![]),
16772+
segments: Some(vec![]),
16773+
time_dimensions: Some(vec![V1LoadRequestQueryTimeDimension {
16774+
dimension: "KibanaSampleDataEcommerce.order_date".to_string(),
16775+
granularity: Some("year".to_string()),
16776+
date_range: None
16777+
}]),
16778+
order: Some(vec![]),
16779+
filters: Some(vec![V1LoadRequestQueryFilterItem {
16780+
member: None,
16781+
operator: None,
16782+
values: None,
16783+
or: Some(vec![
16784+
json!(V1LoadRequestQueryFilterItem {
16785+
member: Some("KibanaSampleDataEcommerce.order_date".to_string()),
16786+
operator: Some("inDateRange".to_string()),
16787+
values: Some(vec![
16788+
"2024-01-01T00:00:00.000Z".to_string(),
16789+
"2024-12-31T23:59:59.999Z".to_string(),
16790+
]),
16791+
or: None,
16792+
and: None,
16793+
}),
16794+
json!(V1LoadRequestQueryFilterItem {
16795+
member: None,
16796+
operator: None,
16797+
values: None,
16798+
or: None,
16799+
and: Some(vec![
16800+
json!(V1LoadRequestQueryFilterItem {
16801+
member: Some(
16802+
"KibanaSampleDataEcommerce.order_date".to_string()
16803+
),
16804+
operator: Some("inDateRange".to_string()),
16805+
values: Some(vec![
16806+
"2025-01-01T00:00:00.000Z".to_string(),
16807+
"2025-12-31T23:59:59.999Z".to_string(),
16808+
]),
16809+
or: None,
16810+
and: None,
16811+
}),
16812+
json!(V1LoadRequestQueryFilterItem {
16813+
member: Some(
16814+
"KibanaSampleDataEcommerce.order_date".to_string()
16815+
),
16816+
operator: Some("inDateRange".to_string()),
16817+
values: Some(vec![
16818+
"2025-01-01T00:00:00.000Z".to_string(),
16819+
"2025-01-31T23:59:59.999Z".to_string(),
16820+
]),
16821+
or: None,
16822+
and: None,
16823+
})
16824+
]),
16825+
}),
16826+
]),
16827+
and: None
16828+
}]),
16829+
..Default::default()
16830+
}
16831+
)
16832+
}
1674216833
}

rust/cubesql/cubesql/src/compile/rewrite/converter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,7 @@ impl LanguageToLogicalPlanConverter {
17831783
query_time_dimensions,
17841784
filters,
17851785
node_by_id,
1786-
!is_in_or || !is_and_op,
1786+
is_in_or || !is_and_op,
17871787
)?;
17881788
match op.as_str() {
17891789
"and" => {

0 commit comments

Comments
 (0)