Skip to content
ClickHouse Docs
ClickHouse DocsClickHouse Docs

timeSeriesInstantRateToGrid

timeSeriesInstantRateToGrid

導入バージョン: v25.6.0

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

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

構文

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

パラメータ

  • start_timestamp — グリッドの開始時点を指定します。DateTime64 の timestamp 引数の場合は、小数、または数値や日時テキストを含む文字列も指定できます。UInt32 または DateTime または DateTime64 または Float* または Decimal* または String
  • end_timestamp — グリッドの終了時点を指定します。DateTime64 の timestamp 引数の場合は、小数、または数値や日時テキストを含む文字列も指定できます。UInt32 または DateTime または DateTime64 または Float* または Decimal* または String
  • grid_step — グリッドの刻み幅を秒単位で指定します。DateTime64 の timestamp 引数の場合は、小数、または数値や '15s'、'1m' のような期間を表す文字列も指定できます。UInt32 または Float* または Decimal* または String
  • staleness — 対象とするサンプルの許容される最大の古さを秒単位で指定します。staleness ウィンドウは左開右閉区間です。DateTime64 の timestamp 引数の場合は、小数、または数値や '15s'、'1m' のような期間を表す文字列も指定できます。UInt32 または Float* または Decimal* または String

引数

  • timestamp — サンプルのタイムスタンプ。単一の値または配列を指定できます。UInt32 または DateTime または Array(UInt32) または Array(DateTime)
  • valuetimestamp に対応する時系列の値。単一の値または配列を指定できます。Float* または Array(Float*)

戻り値

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

単一の timestamp-value ペアを使う基本的な使い方

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
SELECT timeSeriesInstantRateToGrid(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
);
┌─timeSeriesInstantRateToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)─┐
│ [NULL,NULL,0,0.2,0.1,0.1,NULL,NULL,0.3]                                                       │
└───────────────────────────────────────────────────────────────────────────────────────────────┘

Array 型の引数を使用する

SET allow_experimental_time_series_aggregate_functions = 1;
-- it is possible to pass multiple samples of timestamps and values as Arrays of equal size
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
SELECT timeSeriesInstantRateToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values);
┌─timeSeriesInstantRateToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values)─┐
│ [NULL,NULL,0,0.2,0.1,0.1,NULL,NULL,0.3]                                                         │
└─────────────────────────────────────────────────────────────────────────────────────────────────┘
Navigation