> ## 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.

> Función de agregación que calcula una predicción lineal al estilo de PromQL sobre series temporales en la cuadrícula especificada.

# timeSeriesPredictLinearToGrid

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

Introducido en: v25.6.0

Función de agregación que toma datos de series temporales como pares de timestamps y valores, y calcula una [predicción lineal similar a PromQL](https://prometheus.io/docs/prometheus/latest/querying/functions/#predict_linear) con un desplazamiento de timestamp de predicción especificado a partir de estos datos, sobre una cuadrícula temporal regular definida por el timestamp de inicio, el timestamp de fin y el step. Para cada punto de la cuadrícula, las muestras utilizadas para calcular `predict_linear` se consideran dentro de la ventana temporal especificada.

<Note>
  Esta función es experimental; para habilitarla, establezca `allow_experimental_ts_to_grid_aggregate_function=true`.
</Note>

**Sintaxis**

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

**Parámetros**

* `start_timestamp` — Especifica el inicio de la cuadrícula. - `end_timestamp` — Especifica el fin de la cuadrícula. - `grid_step` — Especifica el paso de la cuadrícula en segundos. - `staleness` — Especifica la antigüedad máxima, en segundos, de las muestras consideradas. La ventana de antigüedad es un intervalo abierto por la izquierda y cerrado por la derecha. - `predict_offset` — Especifica el número de segundos de desplazamiento que se añade al instante de predicción.

**Argumentos**

* `timestamp` — Marca temporal de la muestra. Puede contener valores individuales o arrays. - `value` — Valor de la serie temporal correspondiente a la marca temporal. Puede contener valores individuales o arrays.

**Valor devuelto**

Valores de `predict_linear` en la cuadrícula especificada como un `Array(Nullable(Float64))`. El array devuelto contiene un valor por cada punto de la cuadrícula temporal. El valor es NULL si no hay suficientes muestras dentro de la ventana para calcular el valor de tasa para un punto de cuadrícula determinado.

**Ejemplos**

**Calcular valores de predict\_linear en la cuadrícula \[90, 105, 120, 135, 150, 165, 180, 195, 210] con un desplazamiento de 60 segundos**

```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]                                                            │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```

**Misma consulta con argumentos de tipo array**

```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]                                                            │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```
