timeSeriesResampleToGridWithStaleness
導入バージョン: v25.6.0
タイムスタンプと値のペアとして時系列データを受け取り、このデータを開始タイムスタンプ、終了タイムスタンプ、step で表される一定間隔の時間グリッドに再サンプリングする集約関数です。グリッド上の各点では、直近のサンプル (指定された time window 内のもの) が選択されます。
Alias: timeSeriesLastToGrid.
複数のサンプルのタイムスタンプが同じ場合は、そのうち 1 つのみが使用されます。最も大きい値を持つサンプルです。NaN 値は他のすべての値より優先度が低いため、このタイムスタンプのすべてのサンプルが NaN の場合にのみ使用されます。
構文
timeSeriesResampleToGridWithStaleness(start_timestamp, end_timestamp, grid_step, staleness_window)(timestamp, value)別名: timeSeriesLastToGrid
パラメータ
start_timestamp— グリッドの開始時刻を指定します。DateTime64のタイムスタンプ引数では、小数、数値または日時テキストを含む文字列も指定できます。UInt32またはDateTimeまたはDateTime64またはFloat*またはDecimal*またはStringend_timestamp— グリッドの終了時刻を指定します。DateTime64のタイムスタンプ引数では、小数、数値または日時テキストを含む文字列も指定できます。UInt32またはDateTimeまたはDateTime64またはFloat*またはDecimal*またはStringgrid_step— グリッドの間隔を秒単位で指定します。DateTime64のタイムスタンプ引数では、小数、数値、または '15s' や '1m' のような期間を含む文字列も指定できます。UInt32またはFloat*またはDecimal*またはStringstaleness_window— 最新のサンプルについて、許容される最大の古さを秒単位で指定します。DateTime64のタイムスタンプ引数では、小数、数値、または '15s' や '1m' のような期間を含む文字列も指定できます。UInt32またはFloat*またはDecimal*またはString
引数
timestamp— サンプルのタイムスタンプ。単一の値または配列を指定できます。UInt32またはDateTimeまたはArray(UInt32)またはArray(DateTime)value— タイムスタンプに対応する時系列の値。単一の値または配列を指定できます。Float*またはArray(Float*)
戻り値
指定したグリッドに再サンプリングした時系列の値を返します。返される配列には、各時間グリッドポイントごとに 1 つの値が含まれます。特定のグリッドポイントに対応するサンプルがない場合、その値は NULL になります。Array(Nullable(Float64))
例
個々のタイムスタンプと値のペアを使った基本的な使い方
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 staleness 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
30 AS window_seconds -- "staleness" window
SELECT timeSeriesResampleToGridWithStaleness(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
);┌─timeSeriesResampleToGridWithStaleness(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)─┐
│ [NULL,NULL,1,3,4,4,NULL,5,8] │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘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,
30 AS window_seconds
SELECT timeSeriesResampleToGridWithStaleness(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values);┌─timeSeriesResampleToGridWithStaleness(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values)─┐
│ [NULL,NULL,1,3,4,4,NULL,5,8] │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────┘