Repository navigation
Check duplicate PromQL series per step instead of per series - #124086
Conversation
|
Workflow [PR], commit [bded3fb] Summary: ✅
AI ReviewSummaryThis 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 Missing context / blind spots
Findings
Final VerdictRequest changes: the per-step duplicate handling is moving in the right direction, but the remaining LLVM Coverage ReportMeasured on commit bded3fb.
Changed lines: Changed C/C++ lines covered: 203/203 (100.00%) · Uncovered code |
| 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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
9d2d7c1 to
36bb3ba
Compare
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.
36bb3ba to
bded3fb
Compare
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes5 object files changed (+53.34 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 units10 translation units recompiled, 13 s compile time in total, 9 of them have a recent master baseline. |
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. |
kirillshokhin
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
| /// 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 |
There was a problem hiding this comment.
| /// 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}; |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
a > on(x) b does not need the original group, but it will still be used
| || (!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) |
There was a problem hiding this comment.
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.
|
@kirillshokhin Thank you for thorough review, I'll create a follow-up PR to fix your findings |
|
@kirillshokhin I addressed all your review comments in PR #124879 |
Changelog category (leave one):
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 byon/ignoring) are duplicates only if they have values at the same step. Series with values at different steps are now merged withanyForEach, and a series without a value at any step (for example, the left side ofandmatched by tags but at no shared step) no longer causes theMultiple series have the same tagsexception.Workflow [PR]
Sync PR [sync-upstream/pr/124086]
Version info
26.10.1.1862-master(included in26.10and later)