Skip to content
ClickHouse Docs
ClickHouse DocsClickHouse Docs

timeSeriesChangesToGrid

timeSeriesChangesToGrid

引入于:v25.6.0

该聚合函数将时间序列数据视为由时间戳和值组成的成对数据,并在由起始时间戳、结束时间戳和步长定义的规则时间网格上,基于这些数据计算类似 PromQL 的 changes。对于网格上的每个点,会在指定的时间窗口内选取样本来计算 changes

如果多个样本具有相同的时间戳,则仅使用其中一个:值最大的样本。非数字值会输给任何其他值,因此仅当该时间戳处的所有样本均为非数字值时,才会使用非数字值。

语法

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

参数

  • start_timestamp — 指定网格的起始时间。使用 DateTime64 时间戳参数时,还可以是小数,或包含数字或日期时间文本的字符串。UInt32DateTimeDateTime64Float*Decimal*String
  • end_timestamp — 指定网格的结束时间。使用 DateTime64 时间戳参数时,还可以是小数,或包含数字或日期时间文本的字符串。UInt32DateTimeDateTime64Float*Decimal*String
  • grid_step — 指定网格步长 (以秒为单位) 。使用 DateTime64 时间戳参数时,还可以是小数,或包含数字或类似 '15s' 或 '1m' 的时长的字符串。UInt32Float*Decimal*String
  • staleness — 指定所考虑样本允许的最大 "陈旧" 时间 (以秒为单位) 。使用 DateTime64 时间戳参数时,还可以是小数,或包含数字或类似 '15s' 或 '1m' 的时长的字符串。UInt32Float*Decimal*String

参数

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

返回值

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

示例

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

SET allow_experimental_time_series_aggregate_functions = 1;
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, 1, 3, 5, 5, 8, 12, 13]::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 timeSeriesChangesToGrid(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
);
┌─timeSeriesChangesToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)─┐
│ [NULL,NULL,0,1,1,0,NULL,0,1,2]                                                            │
└───────────────────────────────────────────────────────────────────────────────────────────┘

使用数组参数的同一查询

SET allow_experimental_time_series_aggregate_functions = 1;
WITH
    [110, 120, 130, 190, 200, 210, 220, 230]::Array(DateTime) AS timestamps,
    [1, 1, 3, 5, 5, 8, 12, 13]::Array(Float32) AS values,
    90 AS start_ts,
    90 + 135 AS end_ts,
    15 AS step_seconds,
    45 AS window_seconds
SELECT timeSeriesChangesToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values);
┌─timeSeriesChangesToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values)─┐
│ [NULL,NULL,0,1,1,0,NULL,0,1,2]                                                              │
└─────────────────────────────────────────────────────────────────────────────────────────────┘
Navigation