Skip to content
ClickHouse Docs
ClickHouse DocsClickHouse Docs

timeSeriesPredictLinearToGrid

timeSeriesPredictLinearToGrid

導入バージョン: v25.6.0

タイムスタンプと値のペアからなる時系列データを受け取り、開始タイムスタンプ、終了タイムスタンプ、step で定義される規則的な時間グリッド上で、指定した予測タイムスタンプオフセットに対する PromQL ライクな線形予測を計算する集約関数です。グリッド上の各ポイントについて、predict_linear の計算には指定した時間ウィンドウ内のサンプルが使用されます。

複数のサンプルが同じタイムスタンプを持つ場合は、値が最大のサンプルのみが使用されます。NaN 値は他のすべての値より優先度が低いため、そのタイムスタンプのすべてのサンプルが NaN である場合にのみ使用されます。

構文

timeSeriesPredictLinearToGrid(start_timestamp, end_timestamp, grid_step, staleness, predict_offset)(timestamp, value)

パラメータ

  • start_timestamp — グリッドの開始時刻を指定します。DateTime64 タイムスタンプ引数の場合は、小数値、数値を含む文字列、または日時テキストも指定できます。UInt32DateTimeDateTime64Float*Decimal*、または String
  • end_timestamp — グリッドの終了時刻を指定します。DateTime64 タイムスタンプ引数の場合は、小数値、数値を含む文字列、または日時テキストも指定できます。UInt32DateTimeDateTime64Float*Decimal*、または String
  • grid_step — グリッドのステップを秒単位で指定します。DateTime64 タイムスタンプ引数の場合は、小数値、数値を含む文字列、または '15s' や '1m' のような期間も指定できます。UInt32Float*Decimal*、または String
  • staleness — 対象となるサンプルの最大 staleness を秒単位で指定します。staleness ウィンドウは左開右閉区間です。DateTime64 タイムスタンプ引数の場合は、小数値、数値を含む文字列、または '15s' や '1m' のような期間も指定できます。UInt32Float*Decimal*、または String
  • predict_offset — 予測時刻に加算するオフセットの秒数を指定します。UInt32Float*Decimal*、または String

引数

  • timestamp — サンプルのタイムスタンプです。単一の値または配列を指定できます。- value — タイムスタンプに対応する時系列の値です。単一の値または配列を指定できます。

戻り値

指定したグリッド上の predict_linear の値を Array(Nullable(Float64)) として返します。返される配列には、各時間グリッドポイントに対応する値が 1 つずつ含まれます。特定のグリッドポイントでレート値を計算するのに十分なサンプルがウィンドウ内にない場合、その値は NULL になります。

60 秒のオフセットを指定して、グリッド [90, 105, 120, 135, 150, 165, 180, 195, 210] 上の predict_linear 値を計算する

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
);
┌─timeSeriesPredictLinearToGrid(start_ts, end_ts, step_seconds, window_seconds, predict_offset)(timestamp, value)─┐
│ [NULL,NULL,1,9.166667,11.6,12.5,NULL,NULL,16.5]                                                                 │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

Array引数を使用した同じクエリ

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);
┌─timeSeriesPredictLinearToGrid(start_ts, end_ts, step_seconds, window_seconds, predict_offset)(timestamps, values)─┐
│ [NULL,NULL,1,9.166667,11.6,12.5,NULL,NULL,16.5]                                                                   │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Navigation