Skip to content
ClickHouse Docs
ClickHouse DocsClickHouse Docs

timeSeriesResetsToGrid

timeSeriesResetsToGrid

導入バージョン: v25.6.0

時系列データをタイムスタンプと値の組として受け取り、開始タイムスタンプ、終了タイムスタンプ、および step で定義される一定間隔の時間グリッド上で、このデータから PromQL ライクな resets を計算する集約関数です。グリッド上の各ポイントでは、resets の計算に使用するサンプルとして、指定された time window 内のものが考慮されます。

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

構文

timeSeriesResetsToGrid(start_timestamp, end_timestamp, grid_step, staleness)(timestamp, value)

パラメータ

  • start_timestamp — グリッドの開始時刻を指定します。DateTime64 のタイムスタンプ引数では、小数、数値を含む文字列、または日時テキストを含む文字列も使用できます。UInt32 または DateTime または DateTime64 または Float* または Decimal* または String
  • end_timestamp — グリッドの終了時刻を指定します。DateTime64 のタイムスタンプ引数では、小数、数値を含む文字列、または日時テキストを含む文字列も使用できます。UInt32 または DateTime または DateTime64 または Float* または Decimal* または String
  • grid_step — グリッドの間隔を秒単位で指定します。DateTime64 のタイムスタンプ引数では、小数、数値を含む文字列、または '15s' や '1m' のような期間を含む文字列も使用できます。UInt32 または Float* または Decimal* または String
  • staleness — 対象とするサンプルの最大"古さ"を秒単位で指定します。DateTime64 のタイムスタンプ引数では、小数、数値を含む文字列、または '15s' や '1m' のような期間を含む文字列も使用できます。UInt32 または Float* または Decimal* または String

引数

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

戻り値

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

グリッド [90, 105, 120, 135, 150, 165, 180, 195, 210, 225] 上の resets 値を計算する

SET allow_experimental_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
);
┌─timeSeriesResetsToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)─┐
│ [NULL,NULL,0,1,1,0,NULL,0,1,2]                                                           │
└──────────────────────────────────────────────────────────────────────────────────────────┘

配列引数を使った同じクエリ

SET allow_experimental_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);
┌─timeSeriesResetsToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values)─┐
│ [NULL,NULL,0,1,1,0,NULL,0,1,2]                                                             │
└────────────────────────────────────────────────────────────────────────────────────────────┘
Navigation