- 函数
- Aggregate functions
- 聚合函数
- timeSeriesLastTwoSamples
timeSeriesLastTwoSamples
聚合函数接收时间序列数据,作为时间戳和值的对,并最多保存最近的 2 个样本。
参数:
timestamp- 样本的时间戳value- 与timestamp对应的时间序列值 此外,还可以将多个时间戳和值作为相同大小的数组传递。
返回值:
一个 Tuple(Array(DateTime), Array(Float64)) - 一对长度为 0 到 2 的数组。第一个数组包含采样时间序列的时间戳,第二个数组包含对应的时间序列值。
示例: 此聚合函数旨在与物化视图和聚合表结合使用,该表存储具有网格对齐时间戳的重新采样时间序列数据。 考虑以下原始数据示例表,以及用于存储重新采样数据的表:
-- Table for raw data
CREATE TABLE t_raw_timeseries
(
metric_id UInt64,
timestamp DateTime64(3, 'UTC') CODEC(DoubleDelta, ZSTD),
value Float64 CODEC(DoubleDelta)
)
ENGINE = MergeTree()
ORDER BY (metric_id, timestamp);
-- Table with data re-sampled to bigger (15 sec) time steps
CREATE TABLE t_resampled_timeseries_15_sec
(
metric_id UInt64,
grid_timestamp DateTime('UTC') CODEC(DoubleDelta, ZSTD), -- Timestamp aligned to 15 sec
samples AggregateFunction(timeSeriesLastTwoSamples, DateTime64(3, 'UTC'), Float64)
)
ENGINE = AggregatingMergeTree()
ORDER BY (metric_id, grid_timestamp);
-- MV for populating re-sampled table
CREATE MATERIALIZED VIEW mv_resampled_timeseries TO t_resampled_timeseries_15_sec
(
metric_id UInt64,
grid_timestamp DateTime('UTC') CODEC(DoubleDelta, ZSTD),
samples AggregateFunction(timeSeriesLastTwoSamples, DateTime64(3, 'UTC'), Float64)
)
AS SELECT
metric_id,
ceil(toUnixTimestamp(timestamp + interval 999 millisecond) / 15, 0) * 15 AS grid_timestamp, -- Round timestamp up to the next grid point
initializeAggregation('timeSeriesLastTwoSamplesState', timestamp, value) AS samples
FROM t_raw_timeseries
ORDER BY metric_id, grid_timestamp;
插入一些测试数据并读取 '2024-12-12 12:00:12' 和 '2024-12-12 12:00:30' 之间的数据
-- Insert some data
INSERT INTO t_raw_timeseries(metric_id, timestamp, value) SELECT number%10 AS metric_id, '2024-12-12 12:00:00'::DateTime64(3, 'UTC') + interval ((number/10)%100)*900 millisecond as timestamp, number%3+number%29 AS value FROM numbers(1000);
-- Check raw data
SELECT *
FROM t_raw_timeseries
WHERE metric_id = 3 AND timestamp BETWEEN '2024-12-12 12:00:12' AND '2024-12-12 12:00:31'
ORDER BY metric_id, timestamp;
3 2024-12-12 12:00:12.870 29
3 2024-12-12 12:00:13.770 8
3 2024-12-12 12:00:14.670 19
3 2024-12-12 12:00:15.570 30
3 2024-12-12 12:00:16.470 9
3 2024-12-12 12:00:17.370 20
3 2024-12-12 12:00:18.270 2
3 2024-12-12 12:00:19.170 10
3 2024-12-12 12:00:20.070 21
3 2024-12-12 12:00:20.970 3
3 2024-12-12 12:00:21.870 11
3 2024-12-12 12:00:22.770 22
3 2024-12-12 12:00:23.670 4
3 2024-12-12 12:00:24.570 12
3 2024-12-12 12:00:25.470 23
3 2024-12-12 12:00:26.370 5
3 2024-12-12 12:00:27.270 13
3 2024-12-12 12:00:28.170 24
3 2024-12-12 12:00:29.069 6
3 2024-12-12 12:00:29.969 14
3 2024-12-12 12:00:30.869 25
查询时间戳 '2024-12-12 12:00:15' 和 '2024-12-12 12:00:30' 的最后 2 个样本:
-- Check re-sampled data
SELECT metric_id, grid_timestamp, (finalizeAggregation(samples).1 as timestamp, finalizeAggregation(samples).2 as value)
FROM t_resampled_timeseries_15_sec
WHERE metric_id = 3 AND grid_timestamp BETWEEN '2024-12-12 12:00:15' AND '2024-12-12 12:00:30'
ORDER BY metric_id, grid_timestamp;
3 2024-12-12 12:00:15 (['2024-12-12 12:00:14.670','2024-12-12 12:00:13.770'],[19,8])
3 2024-12-12 12:00:30 (['2024-12-12 12:00:29.969','2024-12-12 12:00:29.069'],[14,6])
聚合表仅存储每个 15 秒对齐时间戳的最后 2 个值。这使得通过读取比原始表中存储的数据少得多来计算类似 PromQL 的 irate 和 idelta 成为可能。
-- Calculate idelta and irate from the raw data
WITH
'2024-12-12 12:00:15'::DateTime64(3,'UTC') AS start_ts, -- start of timestamp grid
start_ts + INTERVAL 60 SECOND AS end_ts, -- end of timestamp grid
15 AS step_seconds, -- step of timestamp grid
45 AS window_seconds -- "staleness" window
SELECT
metric_id,
timeSeriesInstantDeltaToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value),
timeSeriesInstantRateToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)
FROM t_raw_timeseries
WHERE metric_id = 3 AND timestamp BETWEEN start_ts - interval window_seconds seconds AND end_ts
GROUP BY metric_id;
3 [11,8,-18,8,11] [12.222222222222221,8.88888888888889,1.1111111111111112,8.88888888888889,12.222222222222221]
-- Calculate idelta and irate from the re-sampled data
WITH
'2024-12-12 12:00:15'::DateTime64(3,'UTC') AS start_ts, -- start of timestamp grid
start_ts + INTERVAL 60 SECOND AS end_ts, -- end of timestamp grid
15 AS step_seconds, -- step of timestamp grid
45 AS window_seconds -- "staleness" window
SELECT
metric_id,
timeSeriesInstantDeltaToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values),
timeSeriesInstantRateToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values)
FROM (
SELECT
metric_id,
finalizeAggregation(samples).1 AS timestamps,
finalizeAggregation(samples).2 AS values
FROM t_resampled_timeseries_15_sec
WHERE metric_id = 3 AND grid_timestamp BETWEEN start_ts - interval window_seconds seconds AND end_ts
)
GROUP BY metric_id;
3 [11,8,-18,8,11] [12.222222222222221,8.88888888888889,1.1111111111111112,8.88888888888889,12.222222222222221]
备注
此功能仍处于实验阶段,请通过设置 allow_experimental_ts_to_grid_aggregate_function=true 来启用它。