> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-detect-table-modification.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 等間隔の時間グリッド上で、PromQL ライクな `first_over_time` を計算します。

# timeSeriesFirstToGrid

<h2 id="timeSeriesFirstToGrid">
  timeSeriesFirstToGrid
</h2>

導入バージョン: v26.10.0

タイムスタンプと値のペアからなる時系列データを受け取り、開始タイムスタンプ、終了タイムスタンプ、ステップで定義される一定間隔の時間グリッド上で、[PromQL ライクな first\_over\_time](https://prometheus.io/docs/prometheus/latest/querying/functions/#aggregation_over_time) を計算する集約関数です。グリッド上の各点における結果は、指定された時間ウィンドウ内で最も早いサンプルの値となります。

サンプルは次の 3 つの形式のいずれかで渡すことができます。

* 2 つの引数 `timestamp` と `value` として渡す形式。各行が 1 つのサンプルを保持します。
* タイムスタンプと値の 2 つの Array として渡す形式。各行が 1 つの時系列全体を保持します。
* `(timestamp, value)` タプルの単一の Array として渡す形式。各行が 1 つの時系列全体を保持します。

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

<Warning>
  この関数はプライベートプレビュー段階です。有効にするには `enable_time_series_aggregate_functions=true` を設定してください。
</Warning>

**構文**

```sql theme={null}
timeSeriesFirstToGrid(start_timestamp, end_timestamp, grid_step, staleness_window)(timestamp, value)
timeSeriesFirstToGrid(start_timestamp, end_timestamp, grid_step, staleness_window)(samples)
```

**パラメータ**

* `start_timestamp` — グリッドの開始時刻を指定します。小数、または数値や日時テキストを含む文字列で指定することもできます。[`UInt32`](/ja/reference/data-types/int-uint) または [`DateTime`](/ja/reference/data-types/datetime) または [`DateTime64`](/ja/reference/data-types/datetime64) または [`Float*`](/ja/reference/data-types/float) または [`Decimal*`](/ja/reference/data-types/decimal) または [`String`](/ja/reference/data-types/string)
* `end_timestamp` — グリッドの終了時刻を指定します。小数、または数値や日時テキストを含む文字列で指定することもできます。[`UInt32`](/ja/reference/data-types/int-uint) または [`DateTime`](/ja/reference/data-types/datetime) または [`DateTime64`](/ja/reference/data-types/datetime64) または [`Float*`](/ja/reference/data-types/float) または [`Decimal*`](/ja/reference/data-types/decimal) または [`String`](/ja/reference/data-types/string)
* `grid_step` — グリッドの間隔を秒単位で指定します。小数、または数値や '15s'、'1m' のような期間を表す文字列で指定することもできます。[`UInt32`](/ja/reference/data-types/int-uint) または [`Float*`](/ja/reference/data-types/float) または [`Decimal*`](/ja/reference/data-types/decimal) または [`String`](/ja/reference/data-types/string)
* `staleness_window` — 対象とするサンプルの「staleness」 (古さ) の最大値を秒単位で指定します。staleness ウィンドウは左開・右閉区間です。小数、または数値や '15s'、'1m' のような期間を表す文字列で指定することもできます。[`UInt32`](/ja/reference/data-types/int-uint) または [`Float*`](/ja/reference/data-types/float) または [`Decimal*`](/ja/reference/data-types/decimal) または [`String`](/ja/reference/data-types/string)

**引数**

* `timestamp` — サンプルのタイムスタンプ。個々の値または Array を指定できます。[`UInt32`](/ja/reference/data-types/int-uint) または [`DateTime`](/ja/reference/data-types/datetime) または [`DateTime64`](/ja/reference/data-types/datetime64) または [`Array(UInt32)`](/ja/reference/data-types/array) または [`Array(DateTime)`](/ja/reference/data-types/array) または [`Array(DateTime64)`](/ja/reference/data-types/array)
* `value` — タイムスタンプに対応する時系列の値。個々の値または Array を指定できます。[`Float*`](/ja/reference/data-types/float) または [`Array(Float*)`](/ja/reference/data-types/array)
* `samples` — `(timestamp, value)` のタプルの Array として渡す時系列のサンプル。タプルの各要素の型は、上記のタイムスタンプおよび値の型と同じです。タイムスタンプと値を 2 つの引数に分けて渡す代わりに使用できます。[`Array(Tuple(T1, T2))`](/ja/reference/data-types/array)

**戻り値**

指定したグリッド上の最も早いサンプルの値を、`value` と同じ型で返します。返される Array には、時間グリッドの各点につき 1 つの値が含まれます。あるグリッド点について、ウィンドウ内にサンプルが存在しない場合、その値は NULL になります。[`Array(Nullable(Float*))`](/ja/reference/data-types/array)

**例**

**グリッド \[90, 105, 120, 135, 150, 165, 180, 195, 210] 上で first\_over\_time の値を計算する**

```sql title=Query theme={null}
SET enable_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 timeSeriesFirstToGrid(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
);
```

```response title=Response theme={null}
┌─timeSeriesFirstToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamp, value)─┐
│ [NULL,NULL,1,1,3,4,NULL,5,5]                                                            │
└─────────────────────────────────────────────────────────────────────────────────────────┘
```

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

```sql title=Query theme={null}
SET enable_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 timeSeriesFirstToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values);
```

```response title=Response theme={null}
┌─timeSeriesFirstToGrid(start_ts, end_ts, step_seconds, window_seconds)(timestamps, values)─┐
│ [NULL,NULL,1,1,3,4,NULL,5,5]                                                              │
└───────────────────────────────────────────────────────────────────────────────────────────┘
```
