Skip to content

PromQL: Add functions label_replace and label_join - #104564

Merged
nikitamikhaylov merged 2 commits into
ClickHouse:masterfrom
vitlibar:promql-label-replace
May 19, 2026
Merged

nikitamikhaylov merged 2 commits into
ClickHouse:masterfrom
vitlibar:promql-label-replace

Conversation

@vitlibar

@vitlibar vitlibar commented May 11, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

PromQL: Add functions label_replace and label_join

Version info

  • Merged into: 26.5.1.817

@vitlibar vitlibar added the comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... label May 11, 2026
@vitlibar
vitlibar requested a review from Copilot May 11, 2026 08:08
@clickhouse-gh

clickhouse-gh Bot commented May 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [43c2d3a]

Summary: ✅


AI Review

Summary

This PR adds PromQL label_replace / label_join support in the converter, updates replacement parsing to match Prometheus ExpandString rules, and adds broad integration/stateless coverage. One correctness issue is still live: the metric_name_dropped flag is forced from destination label name rather than from actual resulting labels, so it can become inconsistent with produced groups when __name__ is rewritten to an empty value.

Findings

❌ Blockers

  • [src/Storages/TimeSeries/PrometheusQueryToSQL/applyLabelManipulationFunction.cpp:279] res.metric_name_dropped is forced to false whenever dest_label == "__name__", but timeSeriesJoinTags / timeSeriesReplaceTag remove destination tags when computed value is empty. Example: label_join(up, "__name__", "_") removes __name__, yet the flag is set as if metric name is present. This violates the converter’s own metric-name state invariant and can mislead downstream group/name propagation logic.
  • Suggested fix: do not force metric_name_dropped = false based only on destination label name. Either (a) conservatively keep the previous value unless non-empty __name__ is guaranteed, or (b) make this path compute/propagate whether __name__ can be empty and update the flag accordingly. The same issue exists in the scalar branch at [src/Storages/TimeSeries/PrometheusQueryToSQL/applyLabelManipulationFunction.cpp:207].
Tests
  • ⚠️ Add a focused regression covering label_join(..., "__name__", ...) / label_replace(..., "__name__", ...) where replacement can be empty, and assert both resulting labels and downstream behavior in a follow-up operation (e.g. binary/aggregation path that relies on metric-name state).
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
  1. Fix metric_name_dropped updates in applyLabelManipulationFunction so they match actual post-transform labelsets when writing __name__.
  2. Add a regression test for empty-result __name__ rewrites.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label May 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds PromQL label manipulation support by implementing label_replace() and label_join() in the PrometheusQueryToSQL converter, backed by existing ClickHouse time-series tag functions, and extends the replacement-pattern handling to better match Prometheus behavior.

Changes:

  • Implement PromQL label_replace / label_join conversion to SQL via timeSeriesReplaceTag / timeSeriesJoinTags.
  • Extend replacement-pattern parsing for timeSeriesReplaceTag (multi-digit refs, braced refs, literal $, etc.) and allow empty src_tag to match PromQL semantics.
  • Add/extend stateless and integration tests covering label manipulation and replacement edge cases.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/queries/0_stateless/03779_time_series_tags_functions.sql Adds stateless SQL coverage for replacement-pattern edge cases.
tests/queries/0_stateless/03779_time_series_tags_functions.reference Updates expected output for the new stateless test block.
tests/integration/test_prometheus_protocols/test_evaluation.py Adds integration tests for PromQL label_replace and label_join behavior and errors.
src/Storages/TimeSeries/PrometheusQueryToSQL/applyLabelManipulationFunction.h Declares helpers for recognizing/applying label manipulation functions.
src/Storages/TimeSeries/PrometheusQueryToSQL/applyLabelManipulationFunction.cpp Implements label_replace / label_join conversion and duplicate-labelset protection.
src/Storages/TimeSeries/PrometheusQueryToSQL/applyFunctionScalar.cpp Adjusts scalar aggregation behavior for instant (start_time == end_time) evaluation.
src/Storages/TimeSeries/PrometheusQueryToSQL/applyFunction.cpp Wires label manipulation functions into the function dispatch path.
src/Interpreters/ContextTimeSeriesTagsCollector.cpp Updates replacement-pattern parsing logic to more closely follow Prometheus semantics.
src/Functions/TimeSeries/timeSeriesReplaceTag.cpp Allows empty src_tag to support PromQL tricks like constant label insertion.

@vitlibar
vitlibar marked this pull request as draft May 11, 2026 08:26
@vitlibar
vitlibar force-pushed the promql-label-replace branch from a7dc5ca to 6cf9805 Compare May 11, 2026 10:35
@vitlibar
vitlibar requested a review from Copilot May 11, 2026 10:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

@vitlibar
vitlibar force-pushed the promql-label-replace branch from 6cf9805 to b43963b Compare May 11, 2026 11:03
@nikitamikhaylov nikitamikhaylov self-assigned this May 11, 2026
@vitlibar
vitlibar force-pushed the promql-label-replace branch from b43963b to fc1c72d Compare May 11, 2026 11:20
@vitlibar

Copy link
Copy Markdown
Member Author

@groeneai Investigate CI failures

@vitlibar
vitlibar marked this pull request as ready for review May 11, 2026 16:19
@groeneai

Copy link
Copy Markdown
Collaborator

@vitlibar — investigated CI failures on fc1c72d (Unit tests ran at 11:51 UTC). All chronic / infra, none PR-caused — detail below.

1. Unit tests (asan_ubsan, function_prop_fuzzer) — FunctionsStress (Logical error: prev.thread_id == curr.thread_id) — CHRONIC, FIX MERGED

This is the exact bug fixed by your own PR #103982 (merged 2026-05-11 14:55:17 UTC). PR #104564's CI run was 11:51 UTC, ~3h before the fix. 30-day CIDB: 56 hits / 26 distinct PRs / 7 master hits — all today, all on commits predating the fix.

2. Unit tests (amd_llvm_coverage) — FileCacheTest.SLRUFreeSpaceKeepingProtectedOnly — CHRONIC

30-day CIDB: 21 hits today / 20 unrelated PRs + chronic since 2026-05-08 (introduced by PR #104313 on 2026-05-08 randomizing keep_free_space_elements_ratio; the test races with FileCache async eviction). Currently being fixed in PR #104418 (@groeneai, in CI) with keep_free_space_elements_ratio=0 to bypass the race; failure expected message is Expected equality: cache->getFileSegmentsNum() = 3, expected 0, which matches.

3. Finish Workflow — Post Hooks/check_report_messages.py — CIDB INFRASTRUCTURE

Read timed out. (read timeout=20) writing to play.clickhouse.com across many sub-jobs (Stateless tests, Build, Fast test, etc). Same global CIDB throughput issue affecting PR #104563's performance comparison shards today.

Recommendation

Rebase against current master (post #103982) and re-run CI; FunctionsStress should clear immediately. FileCacheTest may still flake until #104418 lands; LLVM Coverage Unit tests is a known chronic until then.

@alexey-milovidov

Copy link
Copy Markdown
Member

The FileCacheTest.SLRUFreeSpaceKeepingProtectedOnly failure on this PR is a known flaky test that was fixed in #104418. Could you please update the branch with the latest master to pick up the fix?

@vitlibar
vitlibar force-pushed the promql-label-replace branch from fc1c72d to 6e023ff Compare May 15, 2026 11:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

src/Storages/TimeSeries/PrometheusQueryToSQL/applyLabelManipulationFunction.cpp:279

  • This unconditionally clears metric_name_dropped when writing to __name__, but the tag functions drop dest_tag if the computed destination value is empty. Cases like joining only missing source labels into __name__ therefore leave the actual group without __name__ while downstream code treats it as present; avoid forcing the flag to false unless the produced value cannot be empty.
            if (dest_label == kMetricName)
                res.metric_name_dropped = false;

@clickhouse-gh

clickhouse-gh Bot commented May 15, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.20% +0.10%
Functions 90.80% 90.80% +0.00%
Branches 76.50% 76.70% +0.20%

Changed lines: 71.30% (164/230) | lost baseline coverage: 13 line(s) · Uncovered code

Full report · Diff report

@nikitamikhaylov
nikitamikhaylov added this pull request to the merge queue May 19, 2026
Merged via the queue into ClickHouse:master with commit ba4ced7 May 19, 2026
330 of 334 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants