Skip to content

Commit 095c288

Browse files
authored
fix(promql): honor @ modifier on subqueries (#9446)
* fix(promql): honor @ modifier on subqueries `prom_subquery_expr_to_plan` never read the `at` field of a subquery, so `sum_over_time(m[20s:10s] @ 60)` was evaluated like `m[20s:10s]` and followed the outer evaluation grid. Handle `@` the way Prometheus does: fold the anchor into the subquery offset (`offset + (eval_start - anchor)`, as `setOffsetForAtModifier` does), evaluate the subquery once at the start of the evaluation, and replay the folded window over the outer grid, reusing the anchored range selector machinery. A call whose range argument is an anchored subquery is promoted as step-invariant, like one over an anchored range selector. `@ start()`/`@ end()` and `offset` combine as for selectors, and an unrepresentable anchor is rejected with the existing error. Closes #9411 Signed-off-by: Aarav <[email protected]> * test(promql): add sqlness cases for @ on subqueries Cover the #9411 reproducer end to end: `@ <ts>`, `@ start()`/`@ end()`, `@` combined with `offset` in either order, an instant query away from the anchor, `*_over_time` functions and `rate` over an anchored subquery, a computed inner expression, and an aggregation above the anchored call. Two series per table so a dropped or merged series shows up as a wrong row. Refs #9411 Signed-off-by: Aarav <[email protected]> --------- Signed-off-by: Aarav <[email protected]>
1 parent e68e8c4 commit 095c288

5 files changed

Lines changed: 494 additions & 13 deletions

File tree

‎src/query/src/promql/planner.rs‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ impl PromPlanner {
577577
range,
578578
step,
579579
offset,
580-
..
580+
at,
581581
} = subquery_expr;
582582

583583
// Shift the child window back; `RangeManipulate` restores the outer timeline.
@@ -603,10 +603,18 @@ impl PromPlanner {
603603
end: current_end,
604604
}
605605
);
606+
// `@` makes the subquery step-invariant: like Prometheus (`setOffsetForAtModifier`), fold
607+
// the anchor into the offset and evaluate once at `current_start`, then replay the result
608+
// over the outer grid. See [`Self::at_modifier_offset`].
609+
let at_offset = self.at_modifier_offset(at, offset)?;
610+
let (eval_end, offset_ms) = match at_offset {
611+
Some(at_offset) => (current_start, at_offset),
612+
None => (current_end, offset_ms),
613+
};
606614
// An empty child needs only its schema; plan it over the valid caller window.
607615
let child_window = Self::subquery_child_window(
608616
current_start,
609-
current_end,
617+
eval_end,
610618
current_interval,
611619
offset_ms,
612620
range.as_millis() as Millisecond,
@@ -707,23 +715,31 @@ impl PromPlanner {
707715
};
708716

709717
let manipulate = RangeManipulate::new(
710-
self.ctx.start,
711-
self.ctx.end,
718+
current_start,
719+
eval_end,
712720
self.ctx.interval,
713721
offset_ms,
714722
range_ms,
715-
time_index_column,
723+
time_index_column.clone(),
716724
self.ctx.field_columns.clone(),
717725
divide_plan,
718726
)
719727
.context(DataFusionPlanningSnafu)?;
720-
// The payload timestamps are shifted by the subquery offset; see
721-
// [`Self::create_range_eval_ts_expr`].
728+
// The payload timestamps are shifted by the subquery offset (the rewritten one under `@`);
729+
// see [`Self::create_range_eval_ts_expr`].
722730
self.ctx.range_fold_offset = Some(offset_ms);
723-
724-
Ok(LogicalPlan::Extension(Extension {
731+
let manipulate = LogicalPlan::Extension(Extension {
725732
node: Arc::new(manipulate),
726-
}))
733+
});
734+
735+
// An anchored subquery was folded once, at `current_start`; report that window at every
736+
// step of the outer grid, like an anchored range selector.
737+
Ok(match at_offset {
738+
Some(_) => {
739+
self.replay_over_grid(manipulate, current_start, current_end, time_index_column)
740+
}
741+
None => manipulate,
742+
})
727743
}
728744

729745
async fn prom_aggr_expr_to_plan(

‎src/query/src/promql/planner/at_modifier.rs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use datafusion::logical_expr::{Extension, LogicalPlan, LogicalPlanBuilder};
2121
use datafusion::prelude::{Column, Expr as DfExpr};
2222
use promql::extension_plan::{InstantManipulate, Millisecond, SeriesDivide};
2323
use promql_parser::parser::{
24-
AtModifier, Call, Expr as PromExpr, MatrixSelector, Offset, ParenExpr,
24+
AtModifier, Call, Expr as PromExpr, MatrixSelector, Offset, ParenExpr, SubqueryExpr,
25+
VectorSelector,
2526
};
2627
use snafu::{OptionExt, ResultExt, ensure};
2728

@@ -138,8 +139,14 @@ impl PromPlanner {
138139
arg = expr;
139140
}
140141
match arg {
141-
// The window is pinned by `@`, so every step folds the same samples.
142-
PromExpr::MatrixSelector(MatrixSelector { vs, .. }) if vs.at.is_some() => {
142+
// The window is pinned by `@`, so every step folds the same samples. That holds
143+
// for a subquery with `@` too, which Prometheus treats as step-invariant whatever
144+
// its inner expression is.
145+
PromExpr::MatrixSelector(MatrixSelector {
146+
vs: VectorSelector { at: Some(_), .. },
147+
..
148+
})
149+
| PromExpr::Subquery(SubqueryExpr { at: Some(_), .. }) => {
143150
if anchored_range {
144151
return false;
145152
}

‎src/query/src/promql/planner/test.rs‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5754,6 +5754,143 @@ async fn subquery_rejects_a_child_window_out_of_the_representable_range() {
57545754
assert_eq!(err.status_code(), StatusCode::InvalidArguments, "{err}");
57555755
}
57565756

5757+
/// `@` on a subquery pins its evaluation instant, like `@` on a range selector: Prometheus folds
5758+
/// the anchor into the subquery offset (`setOffsetForAtModifier`) and treats the subquery as
5759+
/// step-invariant, so every step reports the window `(anchor - offset - range, anchor - offset]`.
5760+
/// See <https://github.com/GreptimeTeam/greptimedb/issues/9411>.
5761+
///
5762+
/// The probe samples are at 0s..60s every 10s with values 1..7, on the 10s subquery grid.
5763+
#[tokio::test]
5764+
async fn subquery_at_modifier_pins_the_evaluation_instant() {
5765+
// The issue reproducer: `@ 60` folds `(40s, 60s]` -- the points 50s and 60s, 6 + 7 -- at every
5766+
// step, the same as the range selector form `m[20s] @ 60`.
5767+
for query in [
5768+
"sum_over_time(subquery_offset_probe[20s] @ 60)",
5769+
"sum_over_time(subquery_offset_probe[20s:10s] @ 60)",
5770+
"sum_over_time(subquery_offset_probe[20s:10s] @ end())",
5771+
] {
5772+
assert_eq!(
5773+
run_subquery_offset_probe(query, 30, 60, 30).await,
5774+
vec![(30_000, 13.0), (60_000, 13.0)],
5775+
"{query}"
5776+
);
5777+
}
5778+
// `@ start()` folds `(10s, 30s]` -- 3 + 4 -- at every step.
5779+
assert_eq!(
5780+
run_subquery_offset_probe(
5781+
"sum_over_time(subquery_offset_probe[20s:10s] @ start())",
5782+
30,
5783+
60,
5784+
30
5785+
)
5786+
.await,
5787+
vec![(30_000, 7.0), (60_000, 7.0)],
5788+
);
5789+
// `offset` is applied relative to the anchor: `@ 60 offset 30s` folds `(10s, 30s]`, in either
5790+
// order of the modifiers.
5791+
for query in [
5792+
"sum_over_time(subquery_offset_probe[20s:10s] @ 60 offset 30s)",
5793+
"sum_over_time(subquery_offset_probe[20s:10s] offset 30s @ 60)",
5794+
] {
5795+
assert_eq!(
5796+
run_subquery_offset_probe(query, 30, 60, 30).await,
5797+
vec![(30_000, 7.0), (60_000, 7.0)],
5798+
"{query}"
5799+
);
5800+
}
5801+
// An instant query evaluated away from the anchor still folds the anchored window.
5802+
assert_eq!(
5803+
run_subquery_offset_probe(
5804+
"sum_over_time(subquery_offset_probe[20s:10s] @ 60)",
5805+
0,
5806+
0,
5807+
1
5808+
)
5809+
.await,
5810+
vec![(0, 13.0)],
5811+
);
5812+
// An anchor whose window starts before the first sample: `@ 0` folds `(-20s, 0s]`, where only
5813+
// the point 0s has a sample.
5814+
assert_eq!(
5815+
run_subquery_offset_probe(
5816+
"sum_over_time(subquery_offset_probe[20s:10s] @ 0)",
5817+
30,
5818+
60,
5819+
30
5820+
)
5821+
.await,
5822+
vec![(30_000, 1.0), (60_000, 1.0)],
5823+
);
5824+
// `predict_linear` is not promoted as a whole, so it consumes the window replayed by the
5825+
// subquery itself; that window holds the same points as the range selector form.
5826+
let anchored = run_subquery_offset_probe(
5827+
"predict_linear(subquery_offset_probe[20s:10s] @ 60, 0)",
5828+
30,
5829+
60,
5830+
30,
5831+
)
5832+
.await;
5833+
assert_eq!(anchored.len(), 2, "{anchored:?}");
5834+
assert_eq!(
5835+
anchored,
5836+
run_subquery_offset_probe(
5837+
"predict_linear(subquery_offset_probe[20s] @ 60, 0)",
5838+
30,
5839+
60,
5840+
30
5841+
)
5842+
.await,
5843+
);
5844+
}
5845+
5846+
/// An anchored subquery evaluates its child over the anchored window only, not over the whole
5847+
/// outer evaluation range, and an anchor that cannot be represented in milliseconds is rejected.
5848+
#[tokio::test]
5849+
async fn subquery_at_modifier_scans_only_the_anchored_window() {
5850+
let plan = plan_subquery_offset_probe(
5851+
"sum_over_time(subquery_offset_probe[20s:10s] @ 60)",
5852+
0,
5853+
100_000,
5854+
30,
5855+
)
5856+
.await
5857+
.unwrap();
5858+
let plan_str = plan.display_indent_schema().to_string();
5859+
// The child grid is the points 50s and 60s of `(40s, 60s]`.
5860+
assert!(
5861+
plan_str.contains("PromInstantManipulate: range=[50000..60000]"),
5862+
"{plan_str}"
5863+
);
5864+
// The subquery is folded once, at the start of the evaluation, and only the outer replay spans
5865+
// the evaluation range.
5866+
assert!(
5867+
plan_str.contains("PromRangeManipulate: req range=[0..0]"),
5868+
"{plan_str}"
5869+
);
5870+
assert!(
5871+
plan_str.contains("subquery_offset_probe.timestamp <= TimestampMillisecond(60000, None)"),
5872+
"the child must not be evaluated over the outer range:\n{plan_str}"
5873+
);
5874+
5875+
#[cfg(not(windows))]
5876+
{
5877+
let err = plan_subquery_offset_probe(
5878+
"sum_over_time(subquery_offset_probe[20s:10s] @ 1e16)",
5879+
0,
5880+
60,
5881+
30,
5882+
)
5883+
.await
5884+
.unwrap_err();
5885+
assert!(
5886+
err.to_string()
5887+
.contains("Timestamp out of range for the `@` modifier"),
5888+
"{err}"
5889+
);
5890+
assert_eq!(err.status_code(), StatusCode::InvalidArguments);
5891+
}
5892+
}
5893+
57575894
#[tokio::test]
57585895
async fn test_hash_join() {
57595896
let mut eval_stmt = EvalStmt {

0 commit comments

Comments
 (0)