diff --git a/src/query/src/promql/native_compile.rs b/src/query/src/promql/native_compile.rs index 43e7233708c..08917cd43cd 100644 --- a/src/query/src/promql/native_compile.rs +++ b/src/query/src/promql/native_compile.rs @@ -513,8 +513,7 @@ fn negate(context: &Context, operand: &PromExpr) -> Result { /// Compiles a binary operation, preserving written operand order and vector matching modifiers. /// /// A scalar carries no labels, so broadcasting it over a vector is unambiguous. Vector operands -/// retain their own input bindings and matching; a `group_right` operation with fill is refused -/// until the kernel supports that combination. +/// retain their own input bindings and matching, including a fill on either side. fn binary_operation(context: &Context, binary: &BinaryExpr) -> Result { // A set operator combines two vectors' series rather than their values, and the tree has no node // for it at all. Its name is reported before the operands are looked at, so a set operation @@ -651,8 +650,8 @@ fn scalar_matching(op: BinaryOp, return_bool: bool) -> BinaryMatching { /// /// Each part is read from the modifier the query wrote, and a modifier it did not write is the /// default PromQL applies: every label but `__name__` matches (`ignoring` nothing), and the -/// cardinality is one-to-one. Fill fields retain the parser's left/right values; one-to-many -/// matching with fill is refused until the native kernel supports that combination. +/// cardinality is one-to-one. Fill fields retain the parser's left/right values, which name the +/// sides as written: the kernel swaps them together with the operands under `group_right`. /// /// `ManyToMany` is the cardinality of a set operator, which [`binary_operation`] refuses before /// asking for a matching, so reaching it means the two disagree; it is reported as a shape with no @@ -684,18 +683,11 @@ fn vector_matching(binary: &BinaryExpr, op: BinaryOp) -> Result ))); } }; - if matches!(&cardinality, Cardinality::OneToMany(_)) - && modifier.is_some_and(|modifier| { - modifier.fill_values.lhs.is_some() || modifier.fill_values.rhs.is_some() - }) - { - return Err(not_implemented(&format!( - "the one-to-many vector matching with fill in `{binary}`, which the native kernel does not support" - ))); - } // The fields keep the parser's operand names: `fill_values.lhs` is `fill_left`, and - // `fill_values.rhs` is `fill_right`. One-to-many matching with either field set was refused - // above; the remaining supported cardinalities preserve these parser-side values for the kernel. + // `fill_values.rhs` is `fill_right`. They name the side written in the query, which is what the + // kernel fills under every cardinality: since prometheus/prometheus#19939 the values swap with + // the operands, so `group_right` fills the query's left and right sides rather than the + // matching's one and many sides. let fill = modifier.map_or_else(FillValues::default, |modifier| FillValues { lhs: modifier.fill_values.lhs, rhs: modifier.fill_values.rhs, diff --git a/src/query/tests/native_multi_selector.rs b/src/query/tests/native_multi_selector.rs index 973f2ae10b6..a3f181a6ed5 100644 --- a/src/query/tests/native_multi_selector.rs +++ b/src/query/tests/native_multi_selector.rs @@ -37,7 +37,7 @@ use datatypes::arrow::array::Float64Array; use datatypes::arrow::buffer::{Buffer, ScalarBuffer}; use datatypes::series::raw::{RawSeriesChunk, RawTimestamps}; use datatypes::series::samples::{FieldSpec, SampleRef, Samples, SeriesFields, SeriesSchema}; -use datatypes::series::{Label, SeriesLabels, SeriesOrdering}; +use datatypes::series::{Label, MetricName, SeriesOrdering}; use promql::evaluated::EvaluatedChunk; use promql::native::execute::{NativeExecutionState, NativeExecutor}; use promql::native::plan::NativeExpr; @@ -74,7 +74,7 @@ fn compile(query: &str) -> CompiledTree { } /// The metric `name` with `labels`, as a source series of the samples a scan would read. -fn metric(name: &str, labels: &[(&str, &str)]) -> SeriesLabels { +fn metric(name: &str, labels: &[(&str, &str)]) -> MetricName { let tags = labels .iter() .map(|(name, value)| Label { @@ -82,7 +82,7 @@ fn metric(name: &str, labels: &[(&str, &str)]) -> SeriesLabels { value: (*value).into(), }) .collect(); - SeriesLabels::try_new(Some(name.into()), tags).expect("distinct label names") + MetricName::try_new(Some(name.into()), tags).expect("distinct label names") } /// One raw chunk of `series`, holding `rows` at their source timestamps, closing the series. @@ -92,7 +92,7 @@ fn metric(name: &str, labels: &[(&str, &str)]) -> SeriesLabels { fn raw_chunk( schema: &SeriesSchema, key: &str, - series: SeriesLabels, + series: MetricName, rows: &[(i64, f64)], ) -> RawSeriesChunk { let timestamps: Vec = rows.iter().map(|&(time, _)| time).collect(); @@ -530,12 +530,11 @@ fn a_two_input_tree_refuses_a_chunk_that_names_no_input() { } /// `group_right(env)` makes the right operand the many side and copies `env` from the left one, so -/// one left series answers two right series. Group-right matching without fill executes; fill on -/// this one-to-many shape is refused until the native kernel supports its semantics. +/// one left series answers two right series. #[test] -fn group_right_executes_without_fill_and_declines_fill() { - let pushes = |compiled: &CompiledTree, with_unmatched: bool| { - let mut pushes = vec![ +fn group_right_answers_each_right_series_and_copies_its_label() { + let pushes = |compiled: &CompiledTree| { + vec![ ( 0, raw_chunk( @@ -563,24 +562,11 @@ fn group_right_executes_without_fill_and_declines_fill() { &[(0, 20.0)], ), ), - ]; - if with_unmatched { - pushes.push(( - 1, - raw_chunk( - compiled.inputs[1].schema.as_ref(), - "b-job=y-instance=i3", - metric("b", &[("instance", "i3"), ("job", "y")]), - &[(0, 5.0)], - ), - )); - } - pushes + ] }; - // One left series, two right ones: each right series is answered and gains `env` from the left. - let unfilled = compile("a{job=\"x\"} / on(job) group_right(env) b"); - let output = results(&run(&unfilled, pushes(&unfilled, true))); + let compiled = compile("a{job=\"x\"} / on(job) group_right(env) b"); + let output = results(&run(&compiled, pushes(&compiled))); assert_eq!( output.len(), 2, @@ -596,22 +582,73 @@ fn group_right_executes_without_fill_and_declines_fill() { Some(&every_point(0.1)), "the second right series is not `2 / 20`: {output:?}" ); +} - // The kernel currently applies the wrong fill side for one-to-many matching; refuse this shape - // until its matching/fill semantics are corrected. - for fill in ["fill_right(83)", "fill(41)"] { - let query = format!("a{{job=\"x\"}} / on(job) group_right(env) {fill} b"); - let error = compile_selectors( - &eval_stmt(&query), - &|_| Ok(raw_schema()), - SeriesOrdering::KeyThenTime, - ) - .expect_err("one-to-many matching with fill must fall back"); - assert!( - matches!(error, datafusion::error::DataFusionError::NotImplemented(_)), - "`{query}` was refused for the wrong reason: {error}" - ); - } +/// `fill_left` and `fill_right` fill the side written in the query, also under `group_right`, where +/// the many side is the right one: prometheus/prometheus#19939. The two values differ so a swap is +/// visible. `fill_left(5)` answers a right series with no left counterpart as `5 - 4`, and +/// `fill_right(7)` answers a left series with no right counterpart as `2 - 7`; the pre-fix kernel +/// had the two trade places, answering `7 - 4` and `2 - 5`. +#[test] +fn group_right_fills_the_side_the_query_wrote() { + let compiled = compile("a{job=\"x\"} - on(job) group_right() fill_left(5) fill_right(7) b"); + let pushes = vec![ + ( + 0, + raw_chunk( + compiled.inputs[0].schema.as_ref(), + "a-job=x", + metric("a", &[("job", "x")]), + &[(0, 2.0)], + ), + ), + // This left series has no right counterpart at all, so `fill_right` answers every point. + ( + 0, + raw_chunk( + compiled.inputs[0].schema.as_ref(), + "a-job=z", + metric("a", &[("job", "z")]), + &[(0, 2.0)], + ), + ), + ( + 1, + raw_chunk( + compiled.inputs[1].schema.as_ref(), + "b-job=x-instance=i1", + metric("b", &[("instance", "i1"), ("job", "x")]), + &[(0, 10.0)], + ), + ), + // This right series has no left counterpart, so `fill_left` answers every point. + ( + 1, + raw_chunk( + compiled.inputs[1].schema.as_ref(), + "b-job=y-instance=i2", + metric("b", &[("instance", "i2"), ("job", "y")]), + &[(0, 4.0)], + ), + ), + ]; + let output = results(&run(&compiled, pushes)); + assert_eq!(output.len(), 3, "every series is answered: {output:?}"); + assert_eq!( + output.get("{instance=\"i1\", job=\"x\"}"), + Some(&every_point(-8.0)), + "the matched series is not `2 - 10`: {output:?}" + ); + assert_eq!( + output.get("{job=\"z\"}"), + Some(&every_point(-5.0)), + "`fill_right(7)` did not fill the right side the query wrote: {output:?}" + ); + assert_eq!( + output.get("{instance=\"i2\", job=\"y\"}"), + Some(&every_point(1.0)), + "`fill_left(5)` did not fill the left side the query wrote: {output:?}" + ); } #[test]