Repository navigation
Push PromQL label matchers across binary operators - #122972
alexey-milovidov merged 5 commits into
Conversation
A binary operator only pairs series whose matching labels are equal, so a
matcher on such a label from one side also holds for the series used from
the other side. Copying it into the selectors of the other side lets
`rate(a{instance="x"}[5m]) / on(instance) rate(b[5m])` read only
b{instance="x"} instead of every series of b. The matchers go only through
selectors, offsets, subqueries, range functions, operators with a scalar and
aggregations grouping by the label; never through `or` or label functions,
and only from the left side to the right side of `unless`.
The pass removes duplicate matchers and skips expressions with more than 20
binary operators, because its work grows fast with their number. The setting
`promql_push_down_label_matchers` turns it off: a copied matcher turns a
whole-metric selector into a filtered one, which loses the whole-metric
primary-key range of a metric-clustered id and can be slower when the matcher
keeps most series of the metric.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [0d89935] Summary: ✅
AI ReviewSummaryThis PR adds a PromQL AST pass that copies label matchers across binary operators and wires it behind the new Findings
Final Verdict
LLVM Coverage ReportMeasured on commit 0d89935.
Changed lines: Changed C/C++ lines covered: 153/166 (92.17%) · Uncovered code |
| const auto & function_name = static_cast<const PrometheusQueryTree::Function *>(node)->function_name; | ||
| bool is_function_over_range = isFunctionOverRange(function_name) || isFunctionQuantileOverTime(function_name) | ||
| || isFunctionPredictLinear(function_name); | ||
| if (!is_function_over_range) |
There was a problem hiding this comment.
This helper currently stops at any non-range Function node and has no UnaryOperator case, so common label-preserving wrappers like abs(req{job="api"}) / on(job) lim or -req{job="api"} / on(job) lim still read all of lim. That is a real gap for the feature as described here, because the PR text only calls out or, label functions, count_values, and absent as exclusions. Please either traverse unary / simple instant-vector wrappers too, or narrow the supported surface and add coverage for the intended limits.
…ushdown The pushdown filtered out series that Prometheus still checks, so some queries that fail in Prometheus returned a result instead. Prometheus reports duplicate series on the "one" side of a vector match in every match group (engine.go VectorBinop), and after a function or operator drops the metric name (mergeSeriesWithSameLabelset), whether or not the other side has that group. Matchers are now copied into the "one" side only when it is an aggregation by labels that are all matched on, which can't have duplicates, and through a function or operator only when its input is one metric selected by name. The many side is still filtered, which is safe: Prometheus checks it only in matched groups. The ratio of two sums by the same labels and the fused shapes keep the pushdown; a plain selector on the right no longer gets it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes10 object files changed (+55.68 KiB total), 1 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units17 translation units recompiled, 57 s compile time in total, 16 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.23 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
A matcher copied into a side of a binary operator went on into any
vector operator nested in that side. There it removed groups the inner
operator matches, so `(lim / on(instance) req) / on(instance)
req{instance="i1"}` returned a result, while Prometheus fails on the
duplicate `lim` series of the inner match group `instance="i3"`. Now
only `and` and `unless`, which have no duplicate checks, pass matchers
inward. 05271 checks this query fails.
05271 also failed in CI for three test-only reasons:
- It counted every EXPLAIN line containing "Join", and one of them is
there only with `enable_join_runtime_filters`, which CI randomizes.
It now counts the Join steps.
- Under TSan, the 20-operator query nested its SQL too deep for the
stack limit. The operator-cap queries are now balanced `and` trees,
which keep the 20 and 21 operator counts.
- It runs about 70 PromQL queries and took over 180s in the flaky
check, so it is tagged `long`.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Under TSan a thread may use only 5% of its stack, and analyzing the SQL of the 20-operator cap query exceeds that even with a balanced operator tree (TOO_DEEP_RECURSION in the TSan targeted and flaky checks). The ASan, MSan, debug and release flaky checks pass the test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A comparison with a scalar and without `bool` keeps the metric name, so its
series can't become duplicates, and the single-metric guard isn't needed there.
Before this, `({__name__=~"req|opt"} > 0) and on(job) lim{job="api"}` read all
of `req` and `opt`; now it reads only their `job="api"` series. A comparison with
`bool` drops the metric name and keeps the guard.
The setting description now also says that only functions over a range and
operators with a scalar pass the matchers, while other functions and the unary
minus stop them, as the code and the 05271 selector lines already do.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
PromQL Compliance ReportBaseline: S3 master
Extended support (upstream engine tests): native-histogram exclusions: 149; excluded assertions: 248; upstream Note: Prometheus compliance score is unchanged vs baseline. |
…cache Master's PromQL label matcher pushdown (ClickHouse#122972) added a setting next to this branch's two settings and a setting declaration in PrometheusHTTPProtocolAPI.cpp. Both sides are kept; the chunks copy the evaluation settings, so they push down matchers too. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve conflicts with ClickHouse#122972 (promql_push_down_label_matchers) in Settings.cpp, StoragePrometheusQuery.cpp and PrometheusHTTPProtocolAPI.cpp by keeping both sides. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve conflicts with ClickHouse#122972 (promql_push_down_label_matchers) in Settings.cpp, PrometheusHTTPProtocolAPI.cpp and PrometheusQueryEvaluationSettings.h by keeping both settings. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test timed out at the 600 s per-test limit in 3 of the 6 master runs of `Stateless tests (amd_msan, parallel, 2/3)` since it was added in ClickHouse#122972 (ed166bd, 28bdac1, db91952). The no-random-settings diagnosis reruns timed out as well, so it is not a randomized setting. The failing jobs' query_log shows where the time goes: each of the 37 comparison queries spends 15-31 s of single-threaded CPU in analysis and plan building under MSan, while reading 473 rows. The whole test takes at most 189 s under ASan+UBSan and 29 s in release builds. Splitting the file would keep the total MSan time, and each part would still be far above the 60 s MSan cutoff of ClickHouse#115050. Tag it `no-msan` like `01721_join_implicit_cast_long` (0d02623). The test still runs on debug, release and ASan+UBSan builds, and other PromQL tests keep the pushdown covered under MSan. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…out arguments The PromQL parser builds an aggregation with any number of arguments, including none, and the converter checks the arity only in `visitNode`. Since ClickHouse#122972, `Converter::getSQL` runs `pushDownLabelMatchers` before `visitNode`, and `getCommonMatchers` / `addMatchers` took `aggregation.children.back()` unchecked. A query such as `sum() / on(job) up{job="a"}` therefore called `back()` on an empty vector, and the libc++ hardening assertion aborted the server (every build type enables it: EXTENSIVE in Debug, FAST otherwise). Add `getAggregatedArgument`, which returns nullptr for an aggregation without arguments (and, as before, for `count_values`), and use it at both sites. Such an aggregation now neither contributes nor receives matchers, the tree is left as it is, and `visitNode` reports the existing `CANNOT_EXECUTE_PROMQL_QUERY` arity error, exactly as with `promql_push_down_label_matchers = 0`. The test covers five routes to the empty `back()`: the left operand, a `topk by` on the right, `quantile` under `and`, an aggregation nested in an enclosing aggregation under `unless`, and a range function over a subquery. Closes ClickHouse#125254 Co-Authored-By: Claude Opus 5.5 <[email protected]>
This PR copies the label matchers of one side of a PromQL binary operator into the selectors of the other side, for the labels the operator matches series by. For example,
rate(b[5m]) / on(instance) rate(a{instance="x"}[5m])now reads onlyb{instance="x"}. Matchers are not copied throughor, label functions,count_values,absent, unary minus or functions without a range argument, and only from left to right forunless. When both sides become the same, the existing fused aggregation reads them once. Expressions with more than 20 binary operators are left as they are. The new settingpromql_push_down_label_matchers(on by default) turns it off: a copied matcher that keeps most series of a whole-metric selector can be slower on a table with very many marks (theindex_granularity = 64rows). To keep Prometheus' errors, a matcher is not copied into the "one" side of the operator (the right side, or the left side withgroup_right) unless that side is an aggregation by labels that are all matched on, not into a nested operator between two vectors other thanandandunless, and not through a function or operator unless its input is one metric selected by name, except a comparison withoutbool. The only change in errors matches Prometheus: duplicate left series of a one-to-one match in a group the right side does not have no longer fail the query.sum by (instance) (rate(node_cpu_seconds_total{instance="host-001:9100",mode!="idle"}[5m])) / on(instance) sum by (instance) (rate(node_cpu_seconds_total[5m]))sum by (job) (rate(http_requests_total{job="node-api"}[5m])) / sum by (job) (rate(http_requests_total[5m]))rate(node_cpu_seconds_total[5m]) / on(instance, region) group_left node_load1{region!="ap-south-1"}rate(http_requests_total[5m]) / on(instance, job) group_left up{job=~"node-api|node-db|node-web"}rate(node_cpu_seconds_total[5m]) / on(instance, job) group_left up{job=~"node-.*"}sum by (job) (rate(http_requests_total{code="500"}[5m])) / sum by (job) (rate(http_requests_total[5m]))bar / on(host) baz{host="h1"}bar / on(env) group_left foo{env="prod"}bar / on(env) group_left foo{env=~"prod|dev"}index_granularity = 64(281k marks)bar / on(host) baz{host="h1"}bar / on(env) group_left foo{env="prod"}req{a="0"} + req{a="1"} + ...((req{a="0"} + req{a="1"}) + ...)req{a0="1"} + req{a1="1"} + ...limseries only in a group the left side lacksreq{instance="i1"} / on(instance) limlim / on(instance) req{instance="i1"}Timings are medians of 21 runs (3 interleaved rounds of 7) with
clickhouse localon a laptop under load 2 to 11; "master" is the same build withpromql_push_down_label_matchers = 0, and a master build gave the same times within noise. A second run of the 90,000-series rows under higher load gave the same picture (keeps 50%: 349 -> 283 ms and 1055 -> 1258 ms; keeps 100%: 437 -> 458 ms).Related: #121107
Related: #114131
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
PromQL: label matchers are copied across binary operators for the labels the operator matches series by, so
b / on(job) a{job="x"}reads only the series ofbwithjob="x". As in Prometheus, duplicate left series of a one-to-one match in a group the right side does not have no longer fail the query. The new settingpromql_push_down_label_matchersturns this off.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/122972]
Version info
26.10.1.2074-master(included in26.10and later)