diff --git a/src/query/src/promql/planner.rs b/src/query/src/promql/planner.rs index 6cb0d671196..dcb4e8fe4e4 100644 --- a/src/query/src/promql/planner.rs +++ b/src/query/src/promql/planner.rs @@ -3021,6 +3021,14 @@ impl PromPlanner { let (_, upper) = bounds(end); return Ok(filter(lower, upper)); } + // Overlapping or touching windows form one continuous range. Use the last + // evaluation point, since the requested end need not lie on the step grid. + if window >= self.ctx.interval { + let (lower, _) = bounds(start); + let last = i128::from(start) + num_points * i128::from(self.ctx.interval); + let upper = last - i128::from(offset_duration); + return Ok(filter(lower, upper)); + } let mut filters = Vec::new(); for timestamp in (start..=end).step_by(self.ctx.interval as usize) { let (lower, upper) = bounds(timestamp); @@ -3031,7 +3039,20 @@ impl PromPlanner { }; filters.push(filter); } - Ok(filters.into_iter().reduce(DfExpr::or)) + // Pair adjacent windows to keep generated expression depth logarithmic. + // A left fold can exceed the expression-depth guard for ordinary queries. + while filters.len() > 1 { + let mut next = Vec::with_capacity(filters.len().div_ceil(2)); + let mut iter = filters.into_iter(); + while let Some(left) = iter.next() { + next.push(match iter.next() { + Some(right) => left.or(right), + None => left, + }); + } + filters = next; + } + Ok(filters.pop()) } /// Create a table scan plan and a filter plan with given filter. diff --git a/src/query/src/promql/planner/test.rs b/src/query/src/promql/planner/test.rs index be3140a6a52..7588390dc9e 100644 --- a/src/query/src/promql/planner/test.rs +++ b/src/query/src/promql/planner/test.rs @@ -6414,6 +6414,29 @@ async fn only_equals_is_supported_for_special_matcher() { } } +#[tokio::test] +async fn scatter_time_filter_many_evaluation_points() { + for function in ["increase", "sum_over_time"] { + for window in ["1h", "2h", "3h"] { + let query = format!("sum({function}(some_metric[{window}]))"); + let eval_stmt = EvalStmt { + expr: parser::parse(&query).unwrap(), + start: UNIX_EPOCH, + end: UNIX_EPOCH + Duration::from_secs(30 * 24 * 3600 + 1800), + interval: Duration::from_secs(2 * 3600), + lookback_delta: Duration::from_secs(300), + }; + let provider = build_test_table_provider( + &[(DEFAULT_SCHEMA_NAME.to_string(), "some_metric".to_string())], + 1, + 1, + ) + .await; + build_optimized_promql_plan(provider, &eval_stmt).await; + } + } +} + #[tokio::test] async fn native_scan_bounds_preserve_zero_lookback_and_overflow() { let table_provider = build_test_table_provider( diff --git a/tests/cases/standalone/common/promql/scatter_time_filter.result b/tests/cases/standalone/common/promql/scatter_time_filter.result new file mode 100644 index 00000000000..82242fe7ae2 --- /dev/null +++ b/tests/cases/standalone/common/promql/scatter_time_filter.result @@ -0,0 +1,57 @@ +-- More than 128 evaluation points must survive the expression-depth guard. +-- Sparse samples exercise excluded lower bounds, included upper bounds and gaps. +CREATE TABLE scatter_time_filter ( + ts TIMESTAMP TIME INDEX, + val DOUBLE +); + +Affected Rows: 0 + +INSERT INTO scatter_time_filter VALUES + (3600000, 1), (3600001, 2), (7199999, 3), (7200000, 4), + (7200001, 5), (10800000, 6), (2592000000, 7), + (2592000001, 8); + +Affected Rows: 8 + +TQL EVAL (0, 2593800, '2h') sum(sum_over_time(scatter_time_filter[1h])); + ++---------------------+---------------------------------------+ +| ts | sum(prom_sum_over_time(ts_range,val)) | ++---------------------+---------------------------------------+ +| 1970-01-01T02:00:00 | 9.0 | +| 1970-01-31T00:00:00 | 7.0 | ++---------------------+---------------------------------------+ + +TQL EVAL (0, 2593800, '2h') sum(increase(scatter_time_filter[1h])); + ++---------------------+----------------------------------------------------+ +| ts | sum(prom_increase(ts_range,val,ts,Int64(3600000))) | ++---------------------+----------------------------------------------------+ +| 1970-01-01T02:00:00 | 2.00000055555571 | ++---------------------+----------------------------------------------------+ + +TQL EVAL (0, 2593800, '2h') sum(sum_over_time(scatter_time_filter[2h])); + ++---------------------+---------------------------------------+ +| ts | sum(prom_sum_over_time(ts_range,val)) | ++---------------------+---------------------------------------+ +| 1970-01-01T02:00:00 | 10.0 | +| 1970-01-01T04:00:00 | 11.0 | +| 1970-01-31T00:00:00 | 7.0 | ++---------------------+---------------------------------------+ + +TQL EVAL (0, 2593800, '2h') sum(sum_over_time(scatter_time_filter[3h])); + ++---------------------+---------------------------------------+ +| ts | sum(prom_sum_over_time(ts_range,val)) | ++---------------------+---------------------------------------+ +| 1970-01-01T02:00:00 | 10.0 | +| 1970-01-01T04:00:00 | 20.0 | +| 1970-01-31T00:00:00 | 7.0 | ++---------------------+---------------------------------------+ + +DROP TABLE scatter_time_filter; + +Affected Rows: 0 + diff --git a/tests/cases/standalone/common/promql/scatter_time_filter.sql b/tests/cases/standalone/common/promql/scatter_time_filter.sql new file mode 100644 index 00000000000..56fedc44210 --- /dev/null +++ b/tests/cases/standalone/common/promql/scatter_time_filter.sql @@ -0,0 +1,18 @@ +-- More than 128 evaluation points must survive the expression-depth guard. +-- Sparse samples exercise excluded lower bounds, included upper bounds and gaps. +CREATE TABLE scatter_time_filter ( + ts TIMESTAMP TIME INDEX, + val DOUBLE +); + +INSERT INTO scatter_time_filter VALUES + (3600000, 1), (3600001, 2), (7199999, 3), (7200000, 4), + (7200001, 5), (10800000, 6), (2592000000, 7), + (2592000001, 8); + +TQL EVAL (0, 2593800, '2h') sum(sum_over_time(scatter_time_filter[1h])); +TQL EVAL (0, 2593800, '2h') sum(increase(scatter_time_filter[1h])); +TQL EVAL (0, 2593800, '2h') sum(sum_over_time(scatter_time_filter[2h])); +TQL EVAL (0, 2593800, '2h') sum(sum_over_time(scatter_time_filter[3h])); + +DROP TABLE scatter_time_filter;