Skip to content

PromQL: _over_time aggregation functions - #101355

Closed
ylw510 wants to merge 14 commits into
ClickHouse:masterfrom
ylw510:promql-over-time-functions
Closed

ylw510 wants to merge 14 commits into
ClickHouse:masterfrom
ylw510:promql-over-time-functions

Conversation

@ylw510

@ylw510 ylw510 commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

Description

Adds Prometheus-style sliding-window aggregates on a fixed time grid (timeSeries*OverTimeToGrid), wires PromQL *_over_time functions in the Prometheus-query-to-SQL path (applyFunctionOverRange), and adds stateless test 04070_over_time_timeseries_functions. Refactors time-series aggregate registration helpers into AggregateFunctionTimeseriesCreation.h to keep AggregateFunctionTimeseriesHelpers.cpp smaller.

Changelog category (leave one):

  • Feature

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

Add experimental timeSeries*OverTimeToGrid aggregate combinators and map PromQL *_over_time functions to them when translating Prometheus queries to SQL.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

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 by start, end, step, and window, 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 to window = 120 when timestamps are in seconds).
    • quantile: level in [0, 1] for QuantileOverTime.
  • Setting: allow_experimental_ts_to_grid_aggregate_function must be enabled for these aggregates (same family as other experimental time-series-to-grid functions).

Implemented ClickHouse aggregate combinators (16):

SQL aggregate Arity (grid params)
timeSeriesAvgOverTimeToGrid (start, end, step, window)
timeSeriesMinOverTimeToGrid same
timeSeriesMaxOverTimeToGrid same
timeSeriesSumOverTimeToGrid same
timeSeriesCountOverTimeToGrid same
timeSeriesStddevOverTimeToGrid same (population stddev)
timeSeriesStdvarOverTimeToGrid same (population variance)
timeSeriesPresentOverTimeToGrid same
timeSeriesAbsentOverTimeToGrid same
timeSeriesFirstOverTimeToGrid same
timeSeriesTsOfLastOverTimeToGrid same
timeSeriesTsOfFirstOverTimeToGrid same
timeSeriesTsOfMinOverTimeToGrid same
timeSeriesTsOfMaxOverTimeToGrid same
timeSeriesQuantileOverTimeToGrid (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 applyFunctionOverRange to these combinators:

PromQL ClickHouse aggregate
avg_over_time timeSeriesAvgOverTimeToGrid
min_over_time timeSeriesMinOverTimeToGrid
max_over_time timeSeriesMaxOverTimeToGrid
sum_over_time timeSeriesSumOverTimeToGrid
count_over_time timeSeriesCountOverTimeToGrid
stddev_over_time timeSeriesStddevOverTimeToGrid
stdvar_over_time timeSeriesStdvarOverTimeToGrid
present_over_time timeSeriesPresentOverTimeToGrid
absent_over_time timeSeriesAbsentOverTimeToGrid
first_over_time timeSeriesFirstOverTimeToGrid
ts_of_last_over_time timeSeriesTsOfLastOverTimeToGrid
ts_of_first_over_time timeSeriesTsOfFirstOverTimeToGrid
ts_of_min_over_time timeSeriesTsOfMinOverTimeToGrid
ts_of_max_over_time timeSeriesTsOfMaxOverTimeToGrid
quantile_over_time timeSeriesQuantileOverTimeToGrid
mad_over_time timeSeriesMadOverTimeToGrid

last_over_time in the same impl_map still uses the existing timeSeriesLastToGrid combinator (not *OverTimeToGrid).

Example use:

SET allow_experimental_ts_to_grid_aggregate_function = 1;

WITH
    toUnixTimestamp(toDateTime('2024-01-01 00:00:00')) AS start,
    toUnixTimestamp(toDateTime('2024-01-01 01:00:00')) AS end,
    60 AS step,
    120 AS window,
    range(start, end + 1, step) AS grid
SELECT arrayZip(
    grid,
    timeSeriesAvgOverTimeToGrid(start, end, step, window)(toUnixTimestamp(ts), value)
)
FROM metrics;

@vitlibar vitlibar self-assigned this Apr 1, 2026
@vitlibar vitlibar added the can be tested Allows running workflows for external contributors label May 7, 2026
@vitlibar

vitlibar commented May 7, 2026

Copy link
Copy Markdown
Member

This PR looks almost ready, why is it in draft?

@vitlibar
vitlibar marked this pull request as ready for review May 7, 2026 07:21

struct Bucket
{
absl::flat_hash_map<TimestampType, ValueType> samples;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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=10 is 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

@ylw510

ylw510 commented May 7, 2026

Copy link
Copy Markdown
Contributor Author

@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]>
@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label May 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7430642]

Summary: ❌

job_name test_name status info comment
Config Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/pr_labels_and_category.py FAIL
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/pr_body_check.py FAIL
Dockers Build (amd) DROPPED
Dockers Build (arm) DROPPED
Dockers Build (multiplatform manifest) DROPPED
Style check DROPPED
Code Review DROPPED
Docs check DROPPED
Docs check (Mintlify) DROPPED
Fast test DROPPED

{"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 = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what is _4 ?

ylw510 added 4 commits May 29, 2026 23:20
…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.
Comment thread src/Core/Settings.cpp Outdated
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"(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The prometheus_query_use_stats_bucket variable is used temporarily for performance comparison and will be removed in the future.

@ylw510

ylw510 commented Jun 6, 2026

Copy link
Copy Markdown
Contributor Author

@vitlibar Hi.
When the lookback window is aligned with the grid step (window % grid_step == 0 && window > grid_step) and we do not need to deduplicate samples at the same timestamp, these _over_time aggregates no longer need to store per-sample arrays and re-scan them at finalize time. Instead, each grid bucket can maintain compact running statistics (e.g. count, sum, sum_sq, min, max), and the result is produced by sliding those bucket-level stats across the window in O(grid) rather than O(grid × window_samples).

I ran some performance comparisons against the baseline vector-bucket path;
The same dataset was written to ClickHouse and VictoriaMetrics, with a total write volume of 100 million rows.
The result looks good(perf_compare_ch_vm.py).

root@ubantu64:~/ClickHouse/build_release/programs# python3 /root/ClickHouse/tmp/perf_compare_ch_vm.py  --iters 3
VictoriaMetrics already running (pid 2770372)
SELECTOR=bench_metric{job="bench",instance="i-0"}  series=50  samples/series=2,000,000
START=1704067200  END=1706067199  RANGE_SEC=1999999  (~133,334 grid pts @ step=15; VM maxPoints=500,000)
function                                step  window  ratio  status  details
----------------------------------------------------------------------------------------------------
timeSeriesCountOverTimeToGrid             60      60     1x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesSumOverTimeToGrid               60      60     1x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesAvgOverTimeToGrid               60      60     1x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesStddevOverTimeToGrid            60      60     1x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesStdvarOverTimeToGrid            60      60     1x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesPresentOverTimeToGrid           60      60     1x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesAbsentOverTimeToGrid            60      60     1x  PASS    common=0 mismatches=0 max_diff=0 ch_only=0 vm_only=1
timeSeriesMinOverTimeToGrid               60      60     1x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesMaxOverTimeToGrid               60      60     1x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesFirstOverTimeToGrid             60      60     1x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
  ... correctness 10/98
timeSeriesTsOfFirstOverTimeToGrid         60      60     1x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfLastOverTimeToGrid          60      60     1x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfMinOverTimeToGrid           60      60     1x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfMaxOverTimeToGrid           60      60     1x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesCountOverTimeToGrid             60     300     5x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesSumOverTimeToGrid               60     300     5x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesAvgOverTimeToGrid               60     300     5x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesStddevOverTimeToGrid            60     300     5x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesStdvarOverTimeToGrid            60     300     5x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesPresentOverTimeToGrid           60     300     5x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
  ... correctness 20/98
timeSeriesAbsentOverTimeToGrid            60     300     5x  PASS    common=4 mismatches=0 max_diff=0 ch_only=0 vm_only=1
timeSeriesMinOverTimeToGrid               60     300     5x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesMaxOverTimeToGrid               60     300     5x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesFirstOverTimeToGrid             60     300     5x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfFirstOverTimeToGrid         60     300     5x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfLastOverTimeToGrid          60     300     5x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfMinOverTimeToGrid           60     300     5x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfMaxOverTimeToGrid           60     300     5x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesCountOverTimeToGrid             60     600    10x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesSumOverTimeToGrid               60     600    10x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
  ... correctness 30/98
timeSeriesAvgOverTimeToGrid               60     600    10x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesStddevOverTimeToGrid            60     600    10x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesStdvarOverTimeToGrid            60     600    10x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesPresentOverTimeToGrid           60     600    10x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesAbsentOverTimeToGrid            60     600    10x  PASS    common=9 mismatches=0 max_diff=0 ch_only=0 vm_only=1
timeSeriesMinOverTimeToGrid               60     600    10x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesMaxOverTimeToGrid               60     600    10x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesFirstOverTimeToGrid             60     600    10x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfFirstOverTimeToGrid         60     600    10x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfLastOverTimeToGrid          60     600    10x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
  ... correctness 40/98
timeSeriesTsOfMinOverTimeToGrid           60     600    10x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfMaxOverTimeToGrid           60     600    10x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesCountOverTimeToGrid             60    3600    60x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesSumOverTimeToGrid               60    3600    60x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesAvgOverTimeToGrid               60    3600    60x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesStddevOverTimeToGrid            60    3600    60x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesStdvarOverTimeToGrid            60    3600    60x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesPresentOverTimeToGrid           60    3600    60x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesAbsentOverTimeToGrid            60    3600    60x  PASS    common=59 mismatches=0 max_diff=0 ch_only=0 vm_only=1
timeSeriesMinOverTimeToGrid               60    3600    60x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
  ... correctness 50/98
timeSeriesMaxOverTimeToGrid               60    3600    60x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesFirstOverTimeToGrid             60    3600    60x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfFirstOverTimeToGrid         60    3600    60x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfLastOverTimeToGrid          60    3600    60x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfMinOverTimeToGrid           60    3600    60x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfMaxOverTimeToGrid           60    3600    60x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesCountOverTimeToGrid             60    7200   120x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesSumOverTimeToGrid               60    7200   120x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesAvgOverTimeToGrid               60    7200   120x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesStddevOverTimeToGrid            60    7200   120x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
  ... correctness 60/98
timeSeriesStdvarOverTimeToGrid            60    7200   120x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesPresentOverTimeToGrid           60    7200   120x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesAbsentOverTimeToGrid            60    7200   120x  PASS    common=119 mismatches=0 max_diff=0 ch_only=0 vm_only=1
timeSeriesMinOverTimeToGrid               60    7200   120x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesMaxOverTimeToGrid               60    7200   120x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesFirstOverTimeToGrid             60    7200   120x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfFirstOverTimeToGrid         60    7200   120x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfLastOverTimeToGrid          60    7200   120x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfMinOverTimeToGrid           60    7200   120x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfMaxOverTimeToGrid           60    7200   120x  PASS    common=33334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
  ... correctness 70/98
timeSeriesCountOverTimeToGrid             15    3600   240x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesSumOverTimeToGrid               15    3600   240x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesAvgOverTimeToGrid               15    3600   240x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesStddevOverTimeToGrid            15    3600   240x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesStdvarOverTimeToGrid            15    3600   240x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesPresentOverTimeToGrid           15    3600   240x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesAbsentOverTimeToGrid            15    3600   240x  PASS    common=239 mismatches=0 max_diff=0 ch_only=0 vm_only=1
timeSeriesMinOverTimeToGrid               15    3600   240x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesMaxOverTimeToGrid               15    3600   240x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesFirstOverTimeToGrid             15    3600   240x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
  ... correctness 80/98
timeSeriesTsOfFirstOverTimeToGrid         15    3600   240x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfLastOverTimeToGrid          15    3600   240x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfMinOverTimeToGrid           15    3600   240x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfMaxOverTimeToGrid           15    3600   240x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesCountOverTimeToGrid             15    7200   480x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesSumOverTimeToGrid               15    7200   480x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesAvgOverTimeToGrid               15    7200   480x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesStddevOverTimeToGrid            15    7200   480x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesStdvarOverTimeToGrid            15    7200   480x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesPresentOverTimeToGrid           15    7200   480x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
  ... correctness 90/98
timeSeriesAbsentOverTimeToGrid            15    7200   480x  PASS    common=479 mismatches=0 max_diff=0 ch_only=0 vm_only=1
timeSeriesMinOverTimeToGrid               15    7200   480x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesMaxOverTimeToGrid               15    7200   480x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesFirstOverTimeToGrid             15    7200   480x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfFirstOverTimeToGrid         15    7200   480x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfLastOverTimeToGrid          15    7200   480x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfMinOverTimeToGrid           15    7200   480x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
timeSeriesTsOfMaxOverTimeToGrid           15    7200   480x  PASS    common=133334 mismatches=0 max_diff=0 ch_only=0 vm_only=0
----------------------------------------------------------------------------------------------------
CORRECTNESS ALL PASSED (ch_only ≈ window/step = CH NULL grids in the pre-data region used to absorb the `_stats` bucket ramp-up — not a mismatch; VM ts_of_* queries use tfirst/tlast/tmin/tmax_over_time)

=== Performance: ClickHouse prometheusQuery (both) vs VictoriaMetrics query_range ===
best-of-3  CH: count()+query_log  (prometheus_query_use_stats_bucket: base=0, stats=1)  VM: stats.executionTimeMsec (promql.Exec only, no JSON transfer)  cells=98  total_samples=100,000,000
function                                step  window  ratio  ch_base  ch_stats    vm_ms      ch_rows     b/s    vs_vm winner
-----------------------------------------------------------------------------------------------------------------------------
timeSeriesCountOverTimeToGrid             60      60     1x       74        52       <1      2007090   1.42x 52000000.00x     VM
timeSeriesSumOverTimeToGrid               60      60     1x       72        54       <1      2007090   1.33x 54000000.00x     VM
timeSeriesAvgOverTimeToGrid               60      60     1x       72        52       <1      2007090   1.38x 52000000.00x     VM
timeSeriesStddevOverTimeToGrid            60      60     1x       73        51       <1      2007090   1.43x 51000000.00x     VM
timeSeriesStdvarOverTimeToGrid            60      60     1x       73        52       <1      2007090   1.40x 52000000.00x     VM
  ... benchmark 5/98
timeSeriesPresentOverTimeToGrid           60      60     1x       54        51       <1      2007090   1.06x 51000000.00x     VM
timeSeriesAbsentOverTimeToGrid            60      60     1x       53        51       <1      2007090   1.04x 51000000.00x     VM
timeSeriesMinOverTimeToGrid               60      60     1x       72        52       <1      2007090   1.38x 52000000.00x     VM
timeSeriesMaxOverTimeToGrid               60      60     1x       79        52       <1      2007090   1.52x 52000000.00x     VM
timeSeriesFirstOverTimeToGrid             60      60     1x       70        53       <1      2007090   1.32x 53000000.00x     VM
  ... benchmark 10/98
timeSeriesTsOfFirstOverTimeToGrid         60      60     1x       72        54       <1      2007090   1.33x 54000000.00x     VM
timeSeriesTsOfLastOverTimeToGrid          60      60     1x       53        53       <1      2007090   1.00x 53000000.00x     VM
timeSeriesTsOfMinOverTimeToGrid           60      60     1x       76        52       <1      2007090   1.46x 52000000.00x     VM
timeSeriesTsOfMaxOverTimeToGrid           60      60     1x       71        54       <1      2007090   1.31x 54000000.00x     VM
timeSeriesCountOverTimeToGrid             60     300     5x       76        52       <1      2007090   1.46x 52000000.00x     VM
  ... benchmark 15/98
timeSeriesSumOverTimeToGrid               60     300     5x       81        52       <1      2007090   1.56x 52000000.00x     VM
timeSeriesAvgOverTimeToGrid               60     300     5x       75        52       <1      2007090   1.44x 52000000.00x     VM
timeSeriesStddevOverTimeToGrid            60     300     5x       81        51       <1      2007090   1.59x 51000000.00x     VM
timeSeriesStdvarOverTimeToGrid            60     300     5x       76        51       <1      2007090   1.49x 51000000.00x     VM
timeSeriesPresentOverTimeToGrid           60     300     5x       54        49       <1      2007090   1.10x 49000000.00x     VM
  ... benchmark 20/98
timeSeriesAbsentOverTimeToGrid            60     300     5x       53        50       <1      2007090   1.06x 50000000.00x     VM
timeSeriesMinOverTimeToGrid               60     300     5x       76        52       <1      2007090   1.46x 52000000.00x     VM
timeSeriesMaxOverTimeToGrid               60     300     5x       75        52       <1      2007090   1.44x 52000000.00x     VM
timeSeriesFirstOverTimeToGrid             60     300     5x       71        54       <1      2007090   1.31x 54000000.00x     VM
timeSeriesTsOfFirstOverTimeToGrid         60     300     5x       72        55       <1      2007090   1.31x 55000000.00x     VM
  ... benchmark 25/98
timeSeriesTsOfLastOverTimeToGrid          60     300     5x       54        54       <1      2007090   1.00x 54000000.00x     VM
timeSeriesTsOfMinOverTimeToGrid           60     300     5x       78        53       <1      2007090   1.47x 53000000.00x     VM
timeSeriesTsOfMaxOverTimeToGrid           60     300     5x       78        55       <1      2007090   1.42x 55000000.00x     VM
timeSeriesCountOverTimeToGrid             60     600    10x       75        54       <1      2007090   1.39x 54000000.00x     VM
timeSeriesSumOverTimeToGrid               60     600    10x       83        52       <1      2007090   1.60x 52000000.00x     VM
  ... benchmark 30/98
timeSeriesAvgOverTimeToGrid               60     600    10x       81        51       <1      2007090   1.59x 51000000.00x     VM
timeSeriesStddevOverTimeToGrid            60     600    10x       86        52       <1      2007090   1.65x 52000000.00x     VM
timeSeriesStdvarOverTimeToGrid            60     600    10x       85        52       <1      2007090   1.63x 52000000.00x     VM
timeSeriesPresentOverTimeToGrid           60     600    10x       52        53       <1      2007090   0.98x 52000000.00x     VM
timeSeriesAbsentOverTimeToGrid            60     600    10x       55        51       <1      2007090   1.08x 51000000.00x     VM
  ... benchmark 35/98
timeSeriesMinOverTimeToGrid               60     600    10x       81        54       <1      2007090   1.50x 54000000.00x     VM
timeSeriesMaxOverTimeToGrid               60     600    10x       83        53       <1      2007090   1.57x 53000000.00x     VM
timeSeriesFirstOverTimeToGrid             60     600    10x       71        54       <1      2007090   1.31x 54000000.00x     VM
timeSeriesTsOfFirstOverTimeToGrid         60     600    10x       73        56       <1      2007090   1.30x 56000000.00x     VM
timeSeriesTsOfLastOverTimeToGrid          60     600    10x       56        55       <1      2007090   1.02x 55000000.00x     VM
  ... benchmark 40/98
timeSeriesTsOfMinOverTimeToGrid           60     600    10x       84        53       <1      2007090   1.58x 53000000.00x     VM
timeSeriesTsOfMaxOverTimeToGrid           60     600    10x       85        54       <1      2007090   1.57x 54000000.00x     VM
timeSeriesCountOverTimeToGrid             60    3600    60x       73        51       <1      2007090   1.43x 51000000.00x     VM
timeSeriesSumOverTimeToGrid               60    3600    60x      125        51       <1      2007090   2.45x 51000000.00x     VM
timeSeriesAvgOverTimeToGrid               60    3600    60x      119        52       <1      2007090   2.29x 52000000.00x     VM
  ... benchmark 45/98
timeSeriesStddevOverTimeToGrid            60    3600    60x      122        52       <1      2007090   2.35x 52000000.00x     VM
timeSeriesStdvarOverTimeToGrid            60    3600    60x      125        52       <1      2007090   2.40x 52000000.00x     VM
timeSeriesPresentOverTimeToGrid           60    3600    60x       56        51       <1      2007090   1.10x 51000000.00x     VM
timeSeriesAbsentOverTimeToGrid            60    3600    60x       55        51       <1      2007090   1.08x 51000000.00x     VM
timeSeriesMinOverTimeToGrid               60    3600    60x      123        53       <1      2007090   2.32x 53000000.00x     VM
  ... benchmark 50/98
timeSeriesMaxOverTimeToGrid               60    3600    60x      127        52       <1      2007090   2.44x 52000000.00x     VM
timeSeriesFirstOverTimeToGrid             60    3600    60x       72        53       <1      2007090   1.36x 53000000.00x     VM
timeSeriesTsOfFirstOverTimeToGrid         60    3600    60x       75        54       <1      2007090   1.39x 54000000.00x     VM
timeSeriesTsOfLastOverTimeToGrid          60    3600    60x       54        53       <1      2007090   1.02x 53000000.00x     VM
timeSeriesTsOfMinOverTimeToGrid           60    3600    60x      133        53       <1      2007090   2.51x 53000000.00x     VM
  ... benchmark 55/98
timeSeriesTsOfMaxOverTimeToGrid           60    3600    60x      132        52       <1      2007090   2.54x 52000000.00x     VM
timeSeriesCountOverTimeToGrid             60    7200   120x       72        52       <1      2007090   1.38x 52000000.00x     VM
timeSeriesSumOverTimeToGrid               60    7200   120x      176        53       <1      2007090   3.32x 53000000.00x     VM
timeSeriesAvgOverTimeToGrid               60    7200   120x      162        54       <1      2007090   3.00x 54000000.00x     VM
timeSeriesStddevOverTimeToGrid            60    7200   120x      172        53       <1      2007090   3.25x 53000000.00x     VM
  ... benchmark 60/98
timeSeriesStdvarOverTimeToGrid            60    7200   120x      165        53       <1      2007090   3.11x 53000000.00x     VM
timeSeriesPresentOverTimeToGrid           60    7200   120x       56        51       <1      2007090   1.10x 51000000.00x     VM
timeSeriesAbsentOverTimeToGrid            60    7200   120x       53        53       <1      2007090   1.00x 53000000.00x     VM
timeSeriesMinOverTimeToGrid               60    7200   120x      180        53       <1      2007090   3.40x 53000000.00x     VM
timeSeriesMaxOverTimeToGrid               60    7200   120x      178        52       <1      2007090   3.42x 52000000.00x     VM
  ... benchmark 65/98
timeSeriesFirstOverTimeToGrid             60    7200   120x       72        54       <1      2007090   1.33x 54000000.00x     VM
timeSeriesTsOfFirstOverTimeToGrid         60    7200   120x       74        55       <1      2007090   1.35x 55000000.00x     VM
timeSeriesTsOfLastOverTimeToGrid          60    7200   120x       54        54       <1      2007090   1.00x 54000000.00x     VM
timeSeriesTsOfMinOverTimeToGrid           60    7200   120x      189        55       <1      2007090   3.44x 55000000.00x     VM
timeSeriesTsOfMaxOverTimeToGrid           60    7200   120x      183        55       <1      2007090   3.33x 55000000.00x     VM
  ... benchmark 70/98
timeSeriesCountOverTimeToGrid             15    3600   240x       80        60       <1      2007090   1.33x 60000000.00x     VM
timeSeriesSumOverTimeToGrid               15    3600   240x      283        59        1      2007090   4.80x   59.00x     VM
timeSeriesAvgOverTimeToGrid               15    3600   240x      260        59        1      2007090   4.41x   59.00x     VM
timeSeriesStddevOverTimeToGrid            15    3600   240x      270        61        1      2007090   4.43x   61.00x     VM
timeSeriesStdvarOverTimeToGrid            15    3600   240x      275        64        1      2007090   4.30x   64.00x     VM
  ... benchmark 75/98
timeSeriesPresentOverTimeToGrid           15    3600   240x       67        58        1      2007090   1.16x   58.00x     VM
timeSeriesAbsentOverTimeToGrid            15    3600   240x       60        57        3      2007090   1.05x   19.00x     VM
timeSeriesMinOverTimeToGrid               15    3600   240x      293        62        3      2007090   4.73x   20.67x     VM
timeSeriesMaxOverTimeToGrid               15    3600   240x      290        64        1      2007090   4.53x   64.00x     VM
timeSeriesFirstOverTimeToGrid             15    3600   240x       82        63        1      2007090   1.30x   63.00x     VM
  ... benchmark 80/98
timeSeriesTsOfFirstOverTimeToGrid         15    3600   240x       86        67        1      2007090   1.28x   67.00x     VM
timeSeriesTsOfLastOverTimeToGrid          15    3600   240x       63        64        1      2007090   0.98x   63.00x     VM
timeSeriesTsOfMinOverTimeToGrid           15    3600   240x      322        63        1      2007090   5.11x   63.00x     VM
timeSeriesTsOfMaxOverTimeToGrid           15    3600   240x      314        60        1      2007090   5.23x   60.00x     VM
timeSeriesCountOverTimeToGrid             15    7200   480x       77        59        3      2007090   1.31x   19.67x     VM
  ... benchmark 85/98
timeSeriesSumOverTimeToGrid               15    7200   480x      491        59        4      2007090   8.32x   14.75x     VM
timeSeriesAvgOverTimeToGrid               15    7200   480x      445        60        2      2007090   7.42x   30.00x     VM
timeSeriesStddevOverTimeToGrid            15    7200   480x      457        58        3      2007090   7.88x   19.33x     VM
timeSeriesStdvarOverTimeToGrid            15    7200   480x      445        61        2      2007090   7.30x   30.50x     VM
timeSeriesPresentOverTimeToGrid           15    7200   480x       62        57        1      2007090   1.09x   57.00x     VM
  ... benchmark 90/98
timeSeriesAbsentOverTimeToGrid            15    7200   480x       64        56        2      2007090   1.14x   28.00x     VM
timeSeriesMinOverTimeToGrid               15    7200   480x      492        59        2      2007090   8.34x   29.50x     VM
timeSeriesMaxOverTimeToGrid               15    7200   480x      496        60        7      2007090   8.27x    8.57x     VM
timeSeriesFirstOverTimeToGrid             15    7200   480x       77        60       10      2007090   1.28x    6.00x     VM
timeSeriesTsOfFirstOverTimeToGrid         15    7200   480x       81        62        2      2007090   1.31x   31.00x     VM
  ... benchmark 95/98
timeSeriesTsOfLastOverTimeToGrid          15    7200   480x       60        60        2      2007090   1.00x   30.00x     VM
timeSeriesTsOfMinOverTimeToGrid           15    7200   480x      541        61        2      2007090   8.87x   30.50x     VM
timeSeriesTsOfMaxOverTimeToGrid           15    7200   480x      533        61        2      2007090   8.74x   30.50x     VM
-----------------------------------------------------------------------------------------------------------------------------
b/s: ch_base / ch_stats speedup (higher = stats faster). '=' means no separate _stats variant or grid misaligned. vs_vm / winner: best of ch_base and ch_stats vs VM (VM ts_of_* uses tfirst/tlast/tmin/tmax_over_time).

Do we still need same-timestamp deduplication logic? VictoriaMetrics handles this via a configuration parameter rather than inside individual rollup functions.
https://github.com/VictoriaMetrics/VictoriaMetrics/blob/7dd8892af1e25662076f29e82cdedaf09af33333/app/vmselect/main.go#L57

	minScrapeInterval = flag.Duration("dedup.minScrapeInterval", 0, "Leave only the last sample in every time series per each discrete interval "+

https://github.com/VictoriaMetrics/VictoriaMetrics/blob/7dd8892af1e25662076f29e82cdedaf09af33333/app/vmstorage/main.go#L60

I think this add logic here might also be compatible with VictoriaMetrics' deduplication semantics — when multiple samples share the same timestamp, we keep the one with the maximum value.

@clickhouse-gh

clickhouse-gh Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

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 vitlibar self-assigned this Jul 17, 2026
@vitlibar

vitlibar commented Jul 19, 2026 •

Copy link
Copy Markdown
Member

Please rework this PR on top of #106724.

Each *_over_time function should be implemented as a Summary/Aggregator (reusing SlidingSum and the window-aligned buckets), which would delete most of the helper files in this PR. Also please drop setting prometheus_query_use_stats_bucket setting (window-aligned buckets and two-stacks optimization in #106724 should be enough to make these functions fast and exact).

@ylw510
ylw510 requested a review from vitlibar July 23, 2026 05:37
@ylw510

ylw510 commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

@vitlibar @nikitamikhaylov

Hi. I've noticed that this PR is duplicated with @valerypetrov's. How should we coordinate this?
#112355
#112354
#112353

@valerypetrov

valerypetrov commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Sorry, I haven't seen your PR before submitting. I can close these PRs, JFYI.

@ylw510

ylw510 commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

Sorry, I haven't seen your PR before submitting. I can close these PRs, JFYI.

Thanks for reaching out!
If you're okay with it, I can keep my PR open and continue driving it forward. Alternatively, we can collaborate on this PR.

@nikitamikhaylov nikitamikhaylov added the comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... label Aug 3, 2026
@nikitamikhaylov

Copy link
Copy Markdown
Member

@ylw510 @valerypetrov The first implementation will come from @vitlibar with all the "infra" for *_over_time functions. You can continue from there / help with testing.

@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

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 vitlibar self-assigned this Aug 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

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.

@ylw510

ylw510 commented Sep 23, 2026

Copy link
Copy Markdown
Contributor Author

@vitlibar It looks like all time-series aggregation functions will be implemented. I'll close this.

@ylw510 ylw510 closed this Sep 24, 2026
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 comp-promql PromQL / time-series subsystem: TimeSeries storage engine, PromQL parser, PromQL-to-SQL converter... manual approve Manual approve required to run CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants