MetricsQL optimizer pushes down label filters from the left operand of `group_left` even when the label is overwritten by `group_left(...)`, silently producing empty results
Disclaimer: this issue was investigated and drafted with the help of Claude (Fable 5). The repro outputs below were generated and verified by actually running
metricsql.Optimizelocally against metricsql v0.87.3, and the query-time behaviour was confirmed against a live VictoriaMetrics cluster and a live Prometheus.
Describe the bug
The MetricsQL optimizer treats the label filters of the left operand of a group_left join as common label filters of the whole join expression. That is correct for most labels (the "many" side's labels survive the join), but it is wrong for any label listed in group_left(...): those labels are overwritten by the right operand's values, so the left operand's filter on such a label says nothing about the join result.
When the join is then combined with another selector via and on(<that label>) (or or/unless, or any outer expression the filter can propagate through), the stale filter gets pushed into that selector and the query silently returns empty or incomplete results. The same query returns correct results on Prometheus.
We hit this in production alerting: rules of this shape evaluated by vmalert returned nothing, so the alerts could never fire. No error is surfaced anywhere — the only symptom is an empty result.
To Reproduce
The bug is visible statically via metricsql.Optimize (no data needed), e.g. with metricsql v0.87.3:
e, _ := metricsql.Parse(`(foo{a="x"} * on(instance) group_left(a) bar) and on(a) baz`)
fmt.Println(string(metricsql.Optimize(e).AppendString(nil)))Output:
(foo{a="x"} * on(instance) group_left(a) bar) and on(a) baz{a="x"}baz must not be constrained to a="x": the join result's a label comes from bar (via group_left(a)), not from foo, so the result's a values are unrelated to "x".
This variant makes the wrongness self-evident — the right operand guarantees the result's a is y or z, yet the optimizer still constrains baz to a="x", making the and unsatisfiable by construction:
input: (foo{a="x"} * on(instance) group_left(a) bar{a=~"y|z"}) and on(a) baz
optimized: (foo{a="x"} * on(instance) group_left(a) bar{a=~"y|z"}) and on(a) baz{a="x"}The same rewrite is observable on a running VictoriaMetrics via /expand-with-exprs, and at query time: with series such that the join produces a="y" results and baz{a="y"} exists, VictoriaMetrics returns an empty result for the query above while Prometheus returns the expected series. The related mutual pushdown between the * operands can even merge contradictory filters (e.g. foo{a="x"} ... bar{a=~"y|z"} in real queries with a shared filtered label), after which vmselect fetches zero series for the whole query (seriesFetched: 0).
Root cause
getCommonLabelFilters in metricsql optimizer.go:
case "group_left":
// {f1} * group_left() {f2} -> {f1, f2}
...
lfsRight = TrimFiltersByGroupModifier(lfsRight, t)
return unionLabelFilters(lfsLeft, lfsRight)lfsLeft is returned untrimmed. Any filter in lfsLeft whose label appears in the join modifier args (group_left(...)) refers to a label that the join overwrites with right-side values, so it must be dropped from the expression's common filters. group_right has the mirrored flaw (lfsRight untrimmed while group_right(...) labels are copied from the left).
Suggested fix: in the group_left case, remove from lfsLeft all filters on labels listed in t.JoinModifier.Args before the union (and symmetrically for group_right / lfsRight).
Version
Observed on VictoriaMetrics cluster v1.136.9; reproduced with metricsql v0.87.3 (current latest, and current master of optimizer.go).
Workaround
Wrapping the left operand so the filtered label is aggregated away before the join, e.g.:
(group without (a) (foo{a="x"}) * on(instance) group_left(a) bar) and on(a) bazThis stops the filter propagating and restores Prometheus-equivalent results.
Source: VictoriaMetrics/VictoriaMetrics