> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-parallel-read-in-order-multi-part.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 用于在指定网格上计算时间序列数据的 PromQL-like resets 的聚合函数。

# timeSeriesResetsToGrid

## timeSeriesResetsToGrid

引入版本：v25.6.0

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

样本可以通过以下三种形式之一传入：

* 作为两个参数 `timestamp` 和 `value`，其中每行包含单个样本；
* 作为时间戳数组和值数组这两个数组，其中每行包含一个完整的时间序列；
* 作为单个 `(timestamp, value)` 元组数组，其中每行包含一个完整的时间序列。

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

<Note>
  此函数处于私有预览阶段，请通过设置 `enable_time_series_aggregate_functions=true` 启用。
</Note>

**语法**

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

**参数**

* `start_timestamp` — 指定网格的起始时间。使用 `DateTime64` 时间戳参数时，也可以是小数，或包含数字或日期时间文本的字符串。[`UInt32`](/zh/reference/data-types/int-uint) 或 [`DateTime`](/zh/reference/data-types/datetime) 或 [`DateTime64`](/zh/reference/data-types/datetime64) 或 [`Float*`](/zh/reference/data-types/float) 或 [`Decimal*`](/zh/reference/data-types/decimal) 或 [`String`](/zh/reference/data-types/string)
* `end_timestamp` — 指定网格的结束时间。使用 `DateTime64` 时间戳参数时，也可以是小数，或包含数字或日期时间文本的字符串。[`UInt32`](/zh/reference/data-types/int-uint) 或 [`DateTime`](/zh/reference/data-types/datetime) 或 [`DateTime64`](/zh/reference/data-types/datetime64) 或 [`Float*`](/zh/reference/data-types/float) 或 [`Decimal*`](/zh/reference/data-types/decimal) 或 [`String`](/zh/reference/data-types/string)
* `grid_step` — 指定网格步长 (以秒为单位) 。使用 `DateTime64` 时间戳参数时，也可以是小数，或包含数字或类似 '15s' 或 '1m' 的时长字符串。[`UInt32`](/zh/reference/data-types/int-uint) 或 [`Float*`](/zh/reference/data-types/float) 或 [`Decimal*`](/zh/reference/data-types/decimal) 或 [`String`](/zh/reference/data-types/string)
* `staleness` — 指定所考虑样本允许的最大 "陈旧" 时间 (以秒为单位) 。使用 `DateTime64` 时间戳参数时，也可以是小数，或包含数字或类似 '15s' 或 '1m' 的时长字符串。[`UInt32`](/zh/reference/data-types/int-uint) 或 [`Float*`](/zh/reference/data-types/float) 或 [`Decimal*`](/zh/reference/data-types/decimal) 或 [`String`](/zh/reference/data-types/string)

**参数**

* `timestamp` — 样本的时间戳。可以是单个值或数组。[`UInt32`](/zh/reference/data-types/int-uint) 或 [`DateTime`](/zh/reference/data-types/datetime) 或 [`DateTime64`](/zh/reference/data-types/datetime64) 或 [`Array(UInt32)`](/zh/reference/data-types/array) 或 [`Array(DateTime)`](/zh/reference/data-types/array) 或 [`Array(DateTime64)`](/zh/reference/data-types/array)
* `value` — 与该时间戳对应的时间序列值。可以是单个值或数组。[`Float*`](/zh/reference/data-types/float) 或 [`Array(Float*)`](/zh/reference/data-types/array)
* `samples` — 以元组数组 `(timestamp, value)` 形式传递的时间序列样本，其中元组元素具有上面列出的时间戳和值类型。这是将时间戳和值作为两个单独参数传递的替代方式。[`Array(Tuple(T1, T2))`](/zh/reference/data-types/array)

**返回值**

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

**示例**

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

```sql title=Query theme={null}
SET enable_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, 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
);
```

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

**使用数组参数的相同查询**

```sql title=Query theme={null}
SET enable_time_series_aggregate_functions = 1;
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);
```

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