Repository navigation
Conversation
|
This PR looks almost ready, why is it in draft? |
|
|
||
| struct Bucket | ||
| { | ||
| absl::flat_hash_map<TimestampType, ValueType> samples; |
There was a problem hiding this comment.
Not every function needs to collect all the samples per bucket. Functions present, absent, first, last, ts_of_first, ts_of_last, min, max, ts_of_min, ts_of_max can collect only one value and one timestamp to compute the result.
There was a problem hiding this comment.
Thank you for the suggestion! I think it works for some functions, but not for all the ones listed.
Let me walk through a concrete example to show where it breaks down.
Counter-Example: Window Is Not a Multiple of Step
Parameters: start=0, step=60, window=90
The bucket assignment rule is bucket_index = ceil((ts - start) / step), so:
- Bucket 1 holds samples with
ts ∈ (0, 60] - Bucket 2 holds samples with
ts ∈ (60, 120]
Samples inserted during add:
ts |
value |
Bucket |
|---|---|---|
| 10 | 100 | 1 |
| 50 | 1 | 1 |
| 70 | 2 | 2 |
| 110 | 3 | 2 |
Per-bucket max if we stored only one value:
| Bucket | Stored max value | Timestamp of max |
|---|---|---|
| 1 | 100 | 10 |
| 2 | 3 | 110 |
Computing max_over_time at Output Bucket i=2 (T[2] = 120)
The sliding window is (T[2] - window, T[2]] = (30, 120].
Correct evaluation — checking each sample individually:
ts |
value |
ts > 30? |
In window |
|---|---|---|---|
| 10 | 100 | No | No |
| 50 | 1 | Yes | Yes |
| 70 | 2 | Yes | Yes |
| 110 | 3 | Yes | Yes |
Correct result: max(1, 2, 3) = 3
Wrong evaluation — using per-bucket max:
- Bucket 1 contributes its stored max = 100 (but
ts=10is outside the window!) - Bucket 2 contributes its stored max = 3
Wrong result: max(100, 3) = 100 ← incorrect
Root Cause
The window boundary T[i] - window generally falls inside a bucket, not on a bucket
boundary. As a result, the oldest bucket that overlaps the window contributes only a partial
set of its samples: those with ts > T[i] - window.
Window boundary at t = 30
↓
Bucket 1: [ts=10, v=100] [ts=50, v=1]
^^^ outside ^^^ ^^^ valid ^^^
When only one (timestamp, value) pair is stored per bucket, it is impossible to determine
whether that pair's timestamp falls inside or outside the current window boundary. In the example
above, the stored max value of 100 belongs to ts=10, which is outside the window
(30, 120], yet it would be incorrectly included in the result.
There was a problem hiding this comment.
I'm still thinking about it. It would be sad if even for functions like max or first we had to store all the samples. I understood the problem with window boundary being inside a bucket. So I had an idea to first redesign buckets to make them always aligned to both step and window. I created a new PR to do that refactoring: #106724
|
@vitlibar, thanks for the review. I'll handle them this weekend. |
Regenerate PromQL lexer tokens so over-time function names (for example mad_over_time) tokenize as FUNCTION instead of breaking parsing. Extend experimental timeseries over-time aggregate functions, fold registration into AggregateFunctionTimeseriesOverTime.cpp, and add integration coverage in test_prometheus_protocols. Co-authored-by: Cursor <[email protected]>
|
Workflow [PR], commit [7430642] Summary: ❌
|
| {"value", "Value of the time series corresponding to the timestamp. Can be individual values or arrays.", {"Float32", "Float64", "Array(Float32)", "Array(Float64)"}} | ||
| }; | ||
|
|
||
| const FunctionDocumentation::Parameters parameters_timeseries_over_time_grid_4 = { |
…age. Restore Operation structs with `AggregateFunctionTimeseriesOverTimeTraits` wiring, move bucket serialization into `TimeseriesOverTimeVectorBucket`, and keep VictoriaMetrics-style tie-breaking for `ts_of_min`/`ts_of_max`.
…s with grid step, and address review feedback.
| Experimental timeSeries* aggregate functions for Prometheus-like timeseries resampling, rate, delta calculation. | ||
| )", EXPERIMENTAL, allow_experimental_ts_to_grid_aggregate_function) \ | ||
| \ | ||
| DECLARE(Bool, prometheus_query_use_stats_bucket, false, R"( |
There was a problem hiding this comment.
The prometheus_query_use_stats_bucket variable is used temporarily for performance comparison and will be removed in the future.
|
@vitlibar Hi. I ran some performance comparisons against the baseline vector-bucket path; Do we still need same-timestamp deduplication logic? VictoriaMetrics handles this via a configuration parameter rather than inside individual rollup functions. I think this |
|
Dear @vitlibar, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
|
Please rework this PR on top of #106724. Each |
|
Hi. I've noticed that this PR is duplicated with @valerypetrov's. How should we coordinate this? |
|
Sorry, I haven't seen your PR before submitting. I can close these PRs, JFYI. |
Thanks for reaching out! |
|
@ylw510 @valerypetrov The first implementation will come from @vitlibar with all the "infra" for |
|
Dear @vitlibar, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
|
Dear @vitlibar, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
|
@vitlibar It looks like all time-series aggregation functions will be implemented. I'll close this. |
Description
Adds Prometheus-style sliding-window aggregates on a fixed time grid (
timeSeries*OverTimeToGrid), wires PromQL*_over_timefunctions in the Prometheus-query-to-SQL path (applyFunctionOverRange), and adds stateless test04070_over_time_timeseries_functions. Refactors time-series aggregate registration helpers intoAggregateFunctionTimeseriesCreation.hto keepAggregateFunctionTimeseriesHelpers.cppsmaller.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Add experimental
timeSeries*OverTimeToGridaggregate combinators and map PromQL*_over_timefunctions to them when translating Prometheus queries to SQL.Documentation entry for user-facing changes
Motivation: Users who store metrics in ClickHouse and use the Prometheus-compatible query path need behavior close to PromQL range functions (
avg_over_time,count_over_time, etc.). These aggregates evaluate a sliding window at each grid point defined bystart,end,step, andwindow, similar in spirit to[range]selectors in PromQL.Parameters:
Aggregate functions (scalar timestamp + value columns, or two arrays of equal length):
timeSeries<Name>OverTimeToGrid(start, end, step, window)(timestamp, value)timeSeriesQuantileOverTimeToGrid(start, end, step, window, quantile)(timestamp, value)start,end: grid bounds (same time type / scale as timestamps).step: distance between output grid points.window: sliding window length (PromQL[2m]corresponds towindow = 120when timestamps are in seconds).quantile: level in[0, 1]forQuantileOverTime.Setting:
allow_experimental_ts_to_grid_aggregate_functionmust be enabled for these aggregates (same family as other experimental time-series-to-grid functions).Implemented ClickHouse aggregate combinators (16):
timeSeriesAvgOverTimeToGrid(start, end, step, window)timeSeriesMinOverTimeToGridtimeSeriesMaxOverTimeToGridtimeSeriesSumOverTimeToGridtimeSeriesCountOverTimeToGridtimeSeriesStddevOverTimeToGridtimeSeriesStdvarOverTimeToGridtimeSeriesPresentOverTimeToGridtimeSeriesAbsentOverTimeToGridtimeSeriesFirstOverTimeToGridtimeSeriesTsOfLastOverTimeToGridtimeSeriesTsOfFirstOverTimeToGridtimeSeriesTsOfMinOverTimeToGridtimeSeriesTsOfMaxOverTimeToGridtimeSeriesQuantileOverTimeToGrid(start, end, step, window, quantile)timeSeriesMadOverTimeToGrid(start, end, step, window)All of the above take
(timestamp, value)or(Array(timestamp), Array(value))as the aggregate arguments after the combinator parameters.PromQL functions mapped in
applyFunctionOverRangeto these combinators:avg_over_timetimeSeriesAvgOverTimeToGridmin_over_timetimeSeriesMinOverTimeToGridmax_over_timetimeSeriesMaxOverTimeToGridsum_over_timetimeSeriesSumOverTimeToGridcount_over_timetimeSeriesCountOverTimeToGridstddev_over_timetimeSeriesStddevOverTimeToGridstdvar_over_timetimeSeriesStdvarOverTimeToGridpresent_over_timetimeSeriesPresentOverTimeToGridabsent_over_timetimeSeriesAbsentOverTimeToGridfirst_over_timetimeSeriesFirstOverTimeToGridts_of_last_over_timetimeSeriesTsOfLastOverTimeToGridts_of_first_over_timetimeSeriesTsOfFirstOverTimeToGridts_of_min_over_timetimeSeriesTsOfMinOverTimeToGridts_of_max_over_timetimeSeriesTsOfMaxOverTimeToGridquantile_over_timetimeSeriesQuantileOverTimeToGridmad_over_timetimeSeriesMadOverTimeToGridlast_over_timein the sameimpl_mapstill uses the existingtimeSeriesLastToGridcombinator (not*OverTimeToGrid).Example use: