跳转到主要内容

timeSeriesInstantDeltaToGrid

引入版本:v25.6.0 该聚合函数接受以时间戳和值对表示的时间序列数据,并根据由起始时间戳、结束时间戳和步长定义的规则时间网格,从这些数据中计算类似 PromQL 的 idelta。 对于网格上的每个点,计算 idelta 时会使用指定时间窗口内的样本。
此函数为 Experimental 功能,请通过设置 allow_experimental_ts_to_grid_aggregate_function=true 启用。
语法
timeSeriesInstantDeltaToGrid(start_timestamp, end_timestamp, grid_step, staleness)(timestamp, value)
参数
  • start_timestamp — 指定网格的起始时间。UInt32DateTime
  • end_timestamp — 指定网格的结束时间。UInt32DateTime
  • grid_step — 指定网格步长 (以秒为单位) 。UInt32
  • staleness — 指定所考虑样本允许的最大陈旧时间 (以秒为单位) 。陈旧窗口为左开右闭区间。UInt32
参数 返回值 返回指定网格上的 idelta 值。返回的数组中,每个时间网格点对应一个值。如果在窗口内没有足够的样本来计算某个网格点的瞬时 delta 值,则该值为 NULL。Array(Nullable(Float64)) 示例 使用单个时间戳-值对的基本用法
Query
WITH
    -- 注意:140 和 190 之间的间隔用于展示 ts = 150、165、180 的值如何根据 window 参数进行填充
    [110, 120, 130, 140, 190, 200, 210, 220, 230]::Array(DateTime) AS timestamps,
    [1, 1, 3, 4, 5, 5, 8, 12, 13]::Array(Float32) AS values, -- 与上述时间戳对应的值数组
    90 AS start_ts,       -- 时间戳网格的起点
    90 + 120 AS end_ts,   -- 时间戳网格的终点
    15 AS step_seconds,   -- 时间戳网格的步长
    45 AS window_seconds  -- "staleness" 窗口
SELECT timeSeriesInstantDeltaToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)
FROM
(
    -- 此子查询将时间戳数组和值数组转换为 `timestamp`、`value` 行
    SELECT
        arrayJoin(arrayZip(timestamps, values)) AS ts_and_val,
        ts_and_val.1 AS timestamp,
        ts_and_val.2 AS value
);
Response
┌─timeSeriesInstantDeltaToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)─┐
│ [NULL,NULL,0,2,1,1,NULL,NULL,3]                                                               │
└───────────────────────────────────────────────────────────────────────────────────────────────┘
使用数组参数
Query
-- 可以将多个时间戳和值的样本以等长 Arrays 的形式传入
WITH
    [110, 120, 130, 140, 190, 200, 210, 220, 230]::Array(DateTime) AS timestamps,
    [1, 1, 3, 4, 5, 5, 8, 12, 13]::Array(Float32) AS values,
    90 AS start_ts,
    90 + 120 AS end_ts,
    15 AS step_seconds,
    45 AS window_seconds
SELECT timeSeriesInstantDeltaToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values);
Response
┌─timeSeriesInstantDeltaToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values)─┐
│ [NULL,NULL,0,2,1,1,NULL,NULL,3]                                                                 │
└─────────────────────────────────────────────────────────────────────────────────────────────────┘
最后修改于 2026年6月10日