> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> 用于在指定网格上计算时间序列数据的类似 PromQL irate 的聚合函数。

# timeSeriesInstantRateToGrid

<div id="timeSeriesInstantRateToGrid">
  ## timeSeriesInstantRateToGrid
</div>

引入版本：v25.6.0

该聚合函数接收由时间戳和值组成的时间序列数据，并在由起始时间戳、结束时间戳和步长定义的规则时间网格上，根据这些数据计算类似 [PromQL irate](https://prometheus.io/docs/prometheus/latest/querying/functions/#irate) 的结果。对于网格上的每个点，会在指定的时间窗口内选取样本来计算 `irate`。

<Warning>
  此函数为 Experimental，请通过设置 `allow_experimental_ts_to_grid_aggregate_function=true` 启用。
</Warning>

**语法**

```sql theme={null}
timeSeriesInstantRateToGrid(start_timestamp, end_timestamp, grid_step, staleness)(timestamp, value)
```

**参数**

* `start_timestamp` — 指定网格的起始时间。[`UInt32`](/docs/zh/reference/data-types/int-uint) 或 [`DateTime`](/docs/zh/reference/data-types/datetime)
* `end_timestamp` — 指定网格的结束时间。[`UInt32`](/docs/zh/reference/data-types/int-uint) 或 [`DateTime`](/docs/zh/reference/data-types/datetime)
* `grid_step` — 指定网格步长，单位为秒。[`UInt32`](/docs/zh/reference/data-types/int-uint)
* `staleness` — 指定纳入计算的样本允许的最大陈旧时长，单位为秒。陈旧窗口是左开右闭区间。[`UInt32`](/docs/zh/reference/data-types/int-uint)

**输入参数**

* `timestamp` — 样本的时间戳。可以是单个值或数组。[`UInt32`](/docs/zh/reference/data-types/int-uint) 或 [`DateTime`](/docs/zh/reference/data-types/datetime) 或 [`Array(UInt32)`](/docs/zh/reference/data-types/array) 或 [`Array(DateTime)`](/docs/zh/reference/data-types/array)
* `value` — 与该时间戳对应的时间序列值。可以是单个值或数组。[`Float*`](/docs/zh/reference/data-types/float) 或 [`Array(Float*)`](/docs/zh/reference/data-types/array)

**返回值**

返回指定网格上的 irate 值。返回的数组在每个时间网格点都包含一个值。对于某个特定网格点，如果窗口内没有足够的样本来计算瞬时速率值，则该值为 NULL。[`Array(Nullable(Float64))`](/docs/zh/reference/data-types/array)

**示例**

**使用单个时间戳-值对的基本用法**

```sql title=Query theme={null}
SET allow_experimental_time_series_aggregate_functions = 1;
WITH
    -- NOTE: the gap between 140 and 190 is to show how values are filled for ts = 150, 165, 180 according to window parameter
    [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, -- array of values corresponding to timestamps above
    90 AS start_ts,       -- start of timestamp grid
    90 + 120 AS end_ts,   -- end of timestamp grid
    15 AS step_seconds,   -- step of timestamp grid
    45 AS window_seconds  -- "staleness" window
SELECT timeSeriesInstantRateToGrid(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
);
```

```response title=Response theme={null}
┌─timeSeriesInstantRateToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)─┐
│ [NULL,NULL,0,0.2,0.1,0.1,NULL,NULL,0.3]                                                       │
└───────────────────────────────────────────────────────────────────────────────────────────────┘
```

**使用数组参数**

```sql title=Query theme={null}
SET allow_experimental_time_series_aggregate_functions = 1;
-- it is possible to pass multiple samples of timestamps and values as Arrays of equal size
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 timeSeriesInstantRateToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values);
```

```response title=Response theme={null}
┌─timeSeriesInstantRateToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values)─┐
│ [NULL,NULL,0,0.2,0.1,0.1,NULL,NULL,0.3]                                                         │
└─────────────────────────────────────────────────────────────────────────────────────────────────┘
```
