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

> 用于在指定网格上对时间序列数据执行类似 PromQL 的线性预测的聚合函数。

# timeSeriesPredictLinearToGrid

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

引入版本：v25.6.0

此聚合函数接受由时间戳和值组成的时间序列数据对，并在由起始时间戳、结束时间戳和步长定义的规则时间网格上，基于这些数据计算具有指定预测时间戳偏移量的[类似 PromQL 的线性预测](https://prometheus.io/docs/prometheus/latest/querying/functions/#predict_linear)。对于网格上的每个点，用于计算 `predict_linear` 的样本取自指定时间窗口内。

<Note>
  此函数为实验性功能，请设置 `allow_experimental_time_series_aggregate_functions=true` 以启用。
</Note>

**语法**

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

**参数**

* `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)
* `predict_offset` — 指定要加到预测时间上的偏移秒数。[`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` — 样本的时间戳。可以是单个值或数组。- `value` — 与该时间戳对应的时间序列值。可以是单个值或数组。

**返回值**

以 `Array(Nullable(Float64))` 形式返回指定网格上的 `predict_linear` 值。返回数组中每个时间网格点对应一个值。如果窗口内没有足够的样本来计算某个网格点的速率值，则该值为 NULL。

**示例**

**计算网格 \[90, 105, 120, 135, 150, 165, 180, 195, 210] 上偏移 60 秒后的 predict\_linear 值**

```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
    60 AS predict_offset  -- prediction time offset
SELECT timeSeriesPredictLinearToGrid(start_ts, end_ts, step_seconds, window_seconds, predict_offset)(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}
┌─timeSeriesPredictLinearToGrid(start_ts, end_ts, step_seconds, window_seconds, predict_offset)(timestamp, value)─┐
│ [NULL,NULL,1,9.166667,11.6,16.916666,NULL,NULL,16.5]                                                            │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```

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

```sql title=Query theme={null}
SET allow_experimental_time_series_aggregate_functions = 1;
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,
    60 AS predict_offset
SELECT timeSeriesPredictLinearToGrid(start_ts, end_ts, step_seconds, window_seconds, predict_offset)(timestamps, values);
```

```response title=Response theme={null}
┌─timeSeriesPredictLinearToGrid(start_ts, end_ts, step_seconds, window_seconds, predict_offset)(timestamp, value)─┐
│ [NULL,NULL,1,9.166667,11.6,16.916666,NULL,NULL,16.5]                                                            │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```
