跳转到主内容
跳转到主内容

timeSeriesResetsToGrid

timeSeriesResetsToGrid

引入版本:v25.6

聚合函数,将时间序列数据作为时间戳与数值的成对输入,并在由起始时间戳、结束时间戳和步长描述的规则时间网格上,从这些数据中计算类似 PromQL 的 resets。对于网格上的每一个点,用于计算 resets 的样本都会在指定的时间窗口内进行考虑。

注意

此函数为实验特性,可通过将 allow_experimental_ts_to_grid_aggregate_function 设置为 true 来启用。

语法

timeSeriesResetsToGrid(start_timestamp, end_timestamp, grid_step, staleness)(timestamp, value)

参数

  • start_timestamp — 指定网格的起始时间。 - end_timestamp — 指定网格的结束时间。 - grid_step — 指定网格的步长(以秒为单位)。 - staleness — 指定被考虑样本的最大“陈旧时间”(staleness,单位为秒)。

参数

  • timestamp — 样本的时间戳。可以是单个值或数组。 - value — 时间序列在该时间戳上对应的数值。可以是单个值或数组。

返回值

指定网格上的 resets 值,类型为 Array(Nullable(Float64))。返回的数组中,每一个时间网格点对应一个值。如果在某个网格点对应的窗口内没有样本可用于计算该点的 resets 值,则该值为 NULL。

示例

在网格 [90, 105, 120, 135, 150, 165, 180, 195, 210, 225] 上计算 resets

WITH
    -- NOTE: the gap between 130 and 190 is to show how values are filled for ts = 180 according to window parameter
    [110, 120, 130, 190, 200, 210, 220, 230]::Array(DateTime) AS timestamps,
    [1, 3, 2, 6, 6, 4, 2, 0]::Array(Float32) AS values, -- array of values corresponding to timestamps above
    90 AS start_ts,       -- start of timestamp grid
    90 + 135 AS end_ts,   -- end of timestamp grid
    15 AS step_seconds,   -- step of timestamp grid
    45 AS window_seconds  -- "staleness" window
SELECT timeSeriesResetsToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)
FROM
(
    -- This subquery converts arrays of timestamps and values into rows of `timestamp`, `value`
    SELECT
        arrayJoin(arrayZip(timestamps, values)) AS ts_and_val,
        ts_and_val.1 AS timestamp,
        ts_and_val.2 AS value
);
┌─timeSeriesResetsToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)─┐
│ [NULL,NULL,0,1,1,1,NULL,0,1,2]                                                           │
└──────────────────────────────────────────────────────────────────────────────────────────┘

使用数组参数的相同查询

WITH
    [110, 120, 130, 190, 200, 210, 220, 230]::Array(DateTime) AS timestamps,
    [1, 3, 2, 6, 6, 4, 2, 0]::Array(Float32) AS values,
    90 AS start_ts,
    90 + 135 AS end_ts,
    15 AS step_seconds,
    45 AS window_seconds
SELECT timeSeriesResetsToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values);
┌─timeSeriesResetsToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)─┐
│ [NULL,NULL,0,1,1,0,NULL,0,1,2]                                                           │
└──────────────────────────────────────────────────────────────────────────────────────────┘