Skip to content

Check duplicate PromQL series per step instead of per series - #124086

Merged
vitlibar merged 1 commit into
ClickHouse:masterfrom
vitlibar:promql-per-step-duplicate-series-check
Oct 8, 2026
Merged

vitlibar merged 1 commit into
ClickHouse:masterfrom
vitlibar:promql-per-step-duplicate-series-check

Conversation

@vitlibar

@vitlibar vitlibar commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Check duplicate PromQL series per step instead of per series

Prometheus evaluates each step independently, so two series which get the same tags (after dropping the metric name, after label_replace, or after matching by on/ignoring) are duplicates only if they have values at the same step. Series with values at different steps are now merged with anyForEach, and a series without a value at any step (for example, the left side of and matched by tags but at no shared step) no longer causes the Multiple series have the same tags exception.


Workflow [PR]
Sync PR [sync-upstream/pr/124086]

Version info

  • Merged into: 26.10.1.1862-master (included in 26.10 and later)

@vitlibar vitlibar added the comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... label Oct 5, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bded3fb]

Summary: ✅


AI Review

Summary

This PR changes PromQL duplicate-series handling from whole-range checking to per-step checking for metric-name dropping, label manipulation, and vector matching. The semantic direction looks right and CI is green, but the group_left / group_right path still keeps a whole-range INNER ALL join when both sides retain multiple historical rows per join_group, so I do not think the change is ready to merge yet.

Missing context / blind spots
  • ⚠️ I did not benchmark a long-range churn case locally because this checkout has no runnable build artifacts; a targeted PromQL query that keeps multiple historical rows per join_group on both sides would quantify the regression directly.
Findings
  • ❌ Blockers
    • [src/Storages/TimeSeries/PrometheusQueryToSQL/applySimpleBinaryOperator.cpp:451] [dismissed by author -- https://github.com/Check duplicate PromQL series per step instead of per series #124086#discussion_r4188311715] The code still switches to JoinStrictness::All whenever both sides can keep multiple rows per join_group, so group_left / group_right with historical label churn on both sides still materializes the full N×M cross-product across the whole range before anyForEach drops the all-NULL pairs. That turns the per-step semantic fix into a quadratic intermediate blow-up on long ranges and can exhaust memory or time on realistic rename/churn histories.
    • Suggested fix: avoid the whole-range ALL JOIN in this path by pre-aggregating one side to the eventual result-group key or by carrying a per-step presence mask so rows that never overlap at a step are never paired.
Final Verdict

Request changes: the per-step duplicate handling is moving in the right direction, but the remaining INNER ALL historical cross-product in applySimpleBinaryOperator is still a merge-blocking scalability regression for churned group_left / group_right queries.

LLVM Coverage Report

Measured on commit bded3fb.

Metric Baseline Current Δ
Lines 88.20% 88.30% +0.10%
Functions 91.80% 92.10% +0.30%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 203/203 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 5, 2026
bool left_has_multiple_rows_per_join_group = group_left || !is_join_group_unique_on_side[0];
bool right_has_multiple_rows_per_join_group = group_right || !is_join_group_unique_on_side[1];

if (left_has_multiple_rows_per_join_group && right_has_multiple_rows_per_join_group)

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.

This INNER ALL path materializes the full historical cross-product when both sides can keep several rows for the same join_group. The multiplicity here is only "across the range", though: if the left side has N churned/renamed rows active at disjoint steps and the right side has M such rows, the join still builds N×M pairs, and almost all of them are all-NULL grids that get thrown away only by the later anyForEach.

That turns the new per-step duplicate handling into quadratic row growth on long ranges with group_left / group_right label churn. Can we avoid the whole-range ALL JOIN here — for example by pre-aggregating one side to the eventual result-group key or by carrying a per-step presence mask — so disjoint historical rows do not explode into N×M intermediate rows?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

fixed

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 current code still takes the INNER ALL path here: src/Storages/TimeSeries/PrometheusQueryToSQL/applySimpleBinaryOperator.cpp:451 still sets join_strictness = JoinStrictness::All whenever both sides are non-unique, so the full historical N×M cross-product is still materialized before the later anyForEach merge.

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.

This is still present in the current code: src/Storages/TimeSeries/PrometheusQueryToSQL/applySimpleBinaryOperator.cpp:451 still switches to JoinStrictness::All whenever both sides can keep multiple rows per join_group.

That means group_left / group_right with historical label churn on both sides still materializes the full N×M cross-product across the whole range before the later anyForEach merge discards the all-NULL pairs. A long range with many renamed or churned series can still blow up quadratically here even when no two rows overlap at the same step.

@vitlibar vitlibar Oct 6, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Only grouping with group_left / group_right with extra labels can use INNER ALL now.

For example a * on(host) group_left(job) b uses INNER ALL because it has to match many hosts from a with many hosts from b (we preaggregate b by host and job, but there can still be many rows with the same host in b because they keep different jobs).

For such cases INNER ALL really produces the number of rows which is a multiplication of the number of matching rows on left and right sides, but it just corresponds to how this operation works in Prometheus.

@vitlibar
vitlibar force-pushed the promql-per-step-duplicate-series-check branch from 9d2d7c1 to 36bb3ba Compare October 5, 2026 22:12
Prometheus evaluates each step independently, so two series which get
the same tags (after dropping the metric name, after label_replace, or
after matching by on/ignoring) are duplicates only if they have values
at the same step.
@vitlibar
vitlibar force-pushed the promql-per-step-duplicate-series-check branch from 36bb3ba to bded3fb Compare October 6, 2026 00:05
@clickhouse-gh

clickhouse-gh Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing bded3fbcd with master a2e4282a1 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

5 object files changed (+53.34 KiB total), 1 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/TimeSeries/PrometheusQueryToSQL/makeNoDuplicateSeriesPerStepCheck.… new 42.09 KiB +42.09 KiB
src/CMakeFiles/dbms.dir/Storages/TimeSeries/PrometheusQueryToSQL/applySimpleBinaryOperator.cpp.o 172.73 KiB 189.42 KiB +16.69 KiB (+9.66%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

10 translation units recompiled, 13 s compile time in total, 9 of them have a recent master baseline.

Job report

@clickhouse-gh

clickhouse-gh Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

PromQL Compliance Report

Baseline: S3 master a2e4282a1

Suite Baseline Current Delta
Prometheus compliance 100.00% 100.00% +0.00%
Extended support (upstream engine tests) 90.44% 90.98% +0.55%
Suite Passed Failed Unsupported
Prometheus compliance 539 -> 539 (+0) 0 -> 0 (+0) 0 -> 0 (+0)
Extended support (upstream engine tests) 662 -> 666 (+4) 67 -> 63 (-4) 3 -> 3 (+0)

Extended support (upstream engine tests): native-histogram exclusions: 149; excluded assertions: 248; upstream 3da2514eff5c.

Note: Prometheus compliance score is unchanged vs baseline.

Uploaded compliance JSON for this PR commit

@kirillshokhin kirillshokhin 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.

Overall LGTM. All my comments can be handled in follow-ups.
Also worth mentioning that we still differ from Prometheus in how the one-to-one case of simple binary operator handles several left-side samples fat the same step: we check both sides regardless of matches, while Prometheus ignores conflicts on the left side if there are no matches for them on the right side at that step. It's a pre-existing behavior, though, not introduced by this PR.

/// FROM <vector_grid>
/// GROUP BY new_group
/// HAVING timeSeriesThrowDuplicateSeriesIf(count() > 1, new_group) = 0
/// HAVING timeSeriesThrowDuplicateSeriesIf(arrayExists(c -> c > 1, countForEach(values)), new_group) = 0

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 comment is slightly misleading, since countForEach(values) would refer to the alias of the anyForEach result. The code does the right thing and builds <vector_grid>.values, while the query in the comment as written would fail with ILLEGAL_AGGREGATION.

Suggested change
/// HAVING timeSeriesThrowDuplicateSeriesIf(arrayExists(c -> c > 1, countForEach(values)), new_group) = 0
/// HAVING timeSeriesThrowDuplicateSeriesIf(arrayExists(c -> c > 1, countForEach(<vector_grid>.values)), new_group) = 0

/// FROM <vector_grid>
/// GROUP BY new_group
/// HAVING timeSeriesThrowDuplicateSeriesIf(count() > 1, new_group) = 0
/// HAVING timeSeriesThrowDuplicateSeriesIf(arrayExists(c -> c > 1, countForEach(values)), new_group) = 0

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.

Suggested change
/// HAVING timeSeriesThrowDuplicateSeriesIf(arrayExists(c -> c > 1, countForEach(values)), new_group) = 0
/// HAVING timeSeriesThrowDuplicateSeriesIf(arrayExists(c -> c > 1, countForEach(<vector_grid>.values)), new_group) = 0


for (auto & side : sides)
/// Apply the `on(tags)` or `ignoring(tags)` matching rules to the `group` column.
bool metric_name_dropped_from_join_group_on_side[2] = {left_argument.metric_name_dropped, right_argument.metric_name_dropped};

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.

These arrays are hard to follow for me. Moreover, every loop over the sides defines is_left_side, so iterations are still side-aware. I think the function could be refactored to use structs to hold the necessary per-side state, one-to-one and one-to-many/many-to-one cases handled in separate functions with one-to-many case reduced to many-to-one case, keeping the right table as "one" by swapping left and right. Not a blocker for this PR, IMO. I would be happy to try the refactoring as a follow-up.

/// (e.g. `a > b`: `join_group` has no `__name__` while the result must keep `__name__` of `a`).
bool original_group_is_used_on_side[2]
= {group_left || (group_right && !extra_labels.empty())
|| (!group_right && !drop_metric_name && !left_argument.metric_name_dropped && metric_name_dropped_from_join_group),

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.

a > on(x) b does not need the original group, but it will still be used

Suggested change
|| (!group_right && !drop_metric_name && !left_argument.metric_name_dropped && metric_name_dropped_from_join_group),
|| (!group_right && !drop_metric_name && !left_argument.metric_name_dropped && metric_name_dropped_from_join_group
&& !operator_node->on),

/// The subquery throws an exception if two series with the same `join_group` have values at the same step,
/// otherwise it returns all the values of `join_group`, so the condition keeps all the rows.
/// The check is a subquery in the WHERE clause because a subquery in the WITH clause is evaluated only if it's referenced.
for (size_t side_index = 0; side_index != 2; ++side_index)

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.

Loop is redundant here, at most one side needs the extra check - left side in one-to-one case for filtering comparisons and "one" side in one-to-many/many-to-one case with extra labels. Not a blocker.

@vitlibar
vitlibar added this pull request to the merge queue Oct 8, 2026
@vitlibar

vitlibar commented Oct 8, 2026

Copy link
Copy Markdown
Member Author

@kirillshokhin Thank you for thorough review, I'll create a follow-up PR to fix your findings

Merged via the queue into ClickHouse:master with commit 6bd4cbc Oct 8, 2026
149 checks passed
@vitlibar
vitlibar deleted the promql-per-step-duplicate-series-check branch October 8, 2026 19:14
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 8, 2026
@vitlibar

vitlibar commented Oct 12, 2026 •

Copy link
Copy Markdown
Member Author

@kirillshokhin I addressed all your review comments in PR #124879

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-bugfix Pull request with bugfix, not backported by default 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.

3 participants