Support PromQL label transformation functions by BadLiveware · Pull Request #104491 · ClickHouse/ClickHouse · GitHub
Skip to content

Support PromQL label transformation functions#104491

Draft
BadLiveware wants to merge 46 commits into
ClickHouse:masterfrom
BadLiveware:split/promql-label-functions
Draft

Support PromQL label transformation functions#104491
BadLiveware wants to merge 46 commits into
ClickHouse:masterfrom
BadLiveware:split/promql-label-functions

Conversation

@BadLiveware

@BadLiveware BadLiveware commented May 9, 2026

Copy link
Copy Markdown

Adds native lowering for PromQL label transformation functions over ClickHouse TimeSeries data.

The implementation supports label_replace and label_join, including empty source-label behavior and invalid duplicate output label sets when transformations collapse multiple input series.

Split out from the integration draft #104271; shared base #104484.

Changelog category (leave one):

  • Experimental Feature

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

Added native PromQL-to-SQL lowering for label_replace and label_join over ClickHouse TimeSeries data.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

BadLiveware added 13 commits May 9, 2026 13:52
Add the common PromQL compliance reporting and developer notes used by the follow-up TimeSeries PromQL lowering slices. Keep the parser compliance corpus expectation explicit for selectors Prometheus rejects.
Add focused Prometheus protocol tests for user-facing native lowering errors: unsupported-but-parseable functions and invalid date-function arity. These are semantic guardrails for clear errors, not broad coverage padding.
Lower label_replace and label_join through the existing time-series tag helpers so native PromQL can transform labelsets without introducing a new IR path. The lowering preserves Prometheus UTF-8 label validation, label_replace's empty source-label behavior, metric-name restoration when __name__ is written, and duplicate-labelset merging with overlap detection.

Also extends focused Prometheus protocol tests and compliance cases for label replacement, joins, empty label names, UTF-8 label names, regex errors, and replacement syntax.

Validation: PYTHONPYCACHEPREFIX=/tmp/ch_pycache_pr7 python3 -m py_compile tests/integration/test_prometheus_protocols/test_evaluation.py tests/integration/test_prometheus_protocols/test_compliance.py; git diff --check; python3 -m ci.praktika run --workflow PR "Build (amd_debug)"; python3 -m ci.praktika run --workflow PR integration --test test_prometheus_protocols/test_evaluation.py::test_label_functions; python3 -m ci.praktika run --workflow PR integration --test test_prometheus_protocols/test_compliance.py::test_promql_compliance.
Avoid fork-local wording in the PromQL review base. These are ClickHouse PromQL regression cases and evidence requirements, not fork-specific artifacts.
Avoid fork-local wording in the PromQL review base. These are ClickHouse PromQL regression cases and evidence requirements, not fork-specific artifacts.
Remove the standalone native histogram discovery Markdown note from the PromQL split branches.
Remove the standalone native histogram discovery Markdown note from the PromQL split branches.
Remove downstream promshim-specific evidence and non-goal language from the native PromQL lowering README so the note stays focused on ClickHouse review guidance.
Remove downstream promshim-specific evidence and non-goal language from the native PromQL lowering README so the note stays focused on ClickHouse review guidance.
Rewrite the native PromQL lowering notes around external SQL-transpiler measurements. Document measured and mixed SQL patterns with target PromQL shapes, pseudocode, and the ClickHouse-side validation expected for in-tree changes.
Rewrite the native PromQL lowering notes around external SQL-transpiler measurements. Document measured and mixed SQL patterns with target PromQL shapes, pseudocode, and the ClickHouse-side validation expected for in-tree changes.
Replace external corpus row names with concrete PromQL expressions and query-range context in the native PromQL lowering notes.
Replace external corpus row names with concrete PromQL expressions and query-range context in the native PromQL lowering notes.
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label May 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 10, 2026

Copy link
Copy Markdown
Contributor

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label May 10, 2026
Match Prometheus parser behavior by requiring vector selectors to contain at
least one matcher that cannot match an empty label value. This prevents broad
selectors like `{job=~".*"}` or `{__name__=~".*"}` from silently selecting every
metric when the user likely intended an explicit non-empty constraint.

Users who intentionally want all real metrics can still use `{__name__=~".+"}`,
and empty-matching label matchers remain valid when combined with that explicit
metric-name matcher.
Deduplicate expanded compliance cases by query text before checking the
`should_fail` expectation. A query listed once as expected success and once as
expected failure is now reported as conflicting metadata instead of slipping
through as two separate cases.
Mirror the upstream PromQL compliance corpus for label transformation invalid
label-name cases. These cases should remain should-fail entries so ClickHouse
acceptance of invalid destination labels stays visible as a rejection mismatch
rather than being counted as expected coverage.
`label_replace` and `label_join` create or read label keys from string
arguments. Keep those arguments constrained to PromQL label-name syntax instead
of accepting every non-empty UTF-8 string, so invalid names such as `~invalid`
are rejected before the SQL path emits inaccessible label keys.
Comment thread src/Parsers/Prometheus/PrometheusQueryParsingUtil-antlr.cpp
Do not stop selector validation after finding the first matcher that cannot match an empty label value. Later regex matchers still need syntax validation so equivalent selector reorderings cannot change parse validity.

Add parser regressions for invalid regex matchers after an implicit metric-name matcher and after explicit non-empty matchers.
Comment thread src/Storages/TimeSeries/PrometheusQueryToSQL/applyLabelFunction.cpp
Add parser and HTTP API coverage for Prometheus selector validation rules: negated non-empty matchers, invalid regex validation after non-empty matchers, and duplicate metric-name matchers. Rejecting duplicate outside/in-brace metric names keeps ClickHouse parsing aligned with Prometheus expression parsing.
Use Prometheus UTF-8 label-name validation for label transformation functions, allow zero-source `label_join`, and match Go `regexp.ExpandString` replacement parsing for `label_replace`.

Extend focused integration and compliance coverage for legacy-invalid UTF-8 labels, empty label deletion, metric-name rewrites, missing sources, replacement edge cases, and duplicate transformed labelsets.
Ignore string literals and selector matchers when routing compliance failures by query shape. This keeps report categories from treating label values or exponent signs as binary operators, and recognizes bare '<' and '>' comparisons.
Use the existing eps parameter when checking live Prometheus HTTP results against expected JSON, and compare ClickHouse HTTP output against the live Prometheus result. This keeps numeric drift bounded to eps while preserving exact shape, label, and timestamp checks.
Comment thread src/Parsers/Prometheus/PrometheusQueryParsingUtil-antlr.cpp Outdated
Matcher validation intentionally turns malformed label regexes into PromQL parse errors. Construct RE2 with log_errors disabled so rejected selectors do not also emit internal RE2 error log lines on every parse attempt.
alexey-milovidov and others added 11 commits May 17, 2026 21:05
Keep the PromQL parser, modifier, and selector fixes from the local matrix pass, but move the high-value regression coverage into the existing protocol and compliance suites instead of adding the standalone matrix harness to the PR branch.\n\nThis keeps fixed-time range evaluation covered through the existing evaluation tests and records the parser, matcher, timestamp-modifier, and sparse range cases in the shared compliance corpus.
Build (arm_tidy) flagged the vector-grid finalization predicate because one call cloned an AST pointer while another argument moved the same pointer. C++ argument evaluation order can make that a use-after-move.\n\nCreate separate AST nodes for the null and stale-marker checks so the generated predicate stays the same without relying on argument evaluation order.
Parse `@ start()` and `@ end()` as timestamp modifier variants instead of
rewriting the raw query text before parsing. This keeps label values and
regular expressions untouched while resolving symbolic bounds in the query
range planner.

Propagate symbolic `@` modifiers through the PromQL tree and SQL lowering so
instant selectors and range-vector functions bind to the query start or end
consistently with Prometheus.
The selector-matcher validation branch should not also introduce symbolic
`@ start()` and `@ end()` modifier parsing. Remove that parser/evaluation
feature from the base branch so the behavior can be reviewed independently.

Move the symbolic timestamp-modifier compliance cases out with the feature.
Keep the offset-only case here because it exercises existing modifier behavior.
Symbolic `@ start()` and `@ end()` modifiers need parser-level handling so
selectors, string literals, and regular expressions are not rewritten as raw
text. Remove the temporary text-substitution path from the base branch; the
symbolic modifier implementation belongs with the dedicated parser change.
…ions

# Conflicts:
#	src/Functions/TimeSeries/timeSeriesReplaceTag.cpp
#	src/Interpreters/ContextTimeSeriesTagsCollector.cpp
#	tests/integration/test_prometheus_protocols/test_evaluation.py
if (isFunctionOverRange(function_name))
return applyFunctionOverRange(function_node, std::move(arguments), context);

if (isLabelFunction(function_name))

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.

The new applyLabelFunction path is unreachable here. applyFunction still checks isLabelManipulationFunction first, and that helper returns true for both label_replace and label_join, so execution returns through applyLabelManipulationFunction before reaching this branch.

That means the new validation and anyForEach / countForEach duplicate handling in applyLabelFunction are never used; cases this PR claims to fix still run through the old count() > 1 lowering. Please remove or bypass the older branch for these functions, or fold the new semantics into applyLabelManipulationFunction, and add a regression test that would fail if this dispatch remains shadowed.

@clickhouse-gh

clickhouse-gh Bot commented May 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.40% 84.40% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.90% 76.90% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 298/451 (66.08%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 1 line(s) · Uncovered code

Full report · Diff report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-experimental Experimental Feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants