> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-parallel-read-in-order-multi-part.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 集約関数の中間状態を格納する ClickHouse の AggregateFunction データ型のドキュメント

# AggregateFunction 型

## 説明

ClickHouse のすべての [Aggregate functions](/ja/reference/functions/aggregate-functions/index) には、
実装固有の中間状態があり、これをシリアライズして
`AggregateFunction` データ型としてテーブルに保存できます。通常、これは
[materialized view](/ja/reference/statements/create/view) を使って行います。

`AggregateFunction` 型でよく使われる集約関数の [集約関数コンビネータ](/ja/reference/functions/aggregate-functions/combinators)
は 2 つあります。

* [`-State`](/ja/reference/functions/aggregate-functions/combinators#-state) 集約関数コンビネータ。これを集約
  関数名の末尾に付けると、`AggregateFunction` の中間状態を生成します。
* [`-Merge`](/ja/reference/functions/aggregate-functions/combinators#-merge) 集約
  関数コンビネータ。これは、中間状態から集約の最終結果を取得するために
  使用されます。

## 構文

```sql theme={null}
AggregateFunction(aggregate_function_name, types_of_arguments...)
```

**パラメータ**

* `aggregate_function_name` - 集約関数の名前。関数が
  パラメトリックな場合は、そのパラメータも指定する必要があります。
* `types_of_arguments` - 集約関数の引数の型。

例えば:

```sql theme={null}
CREATE TABLE t
(
    column1 AggregateFunction(uniq, UInt64),
    column2 AggregateFunction(anyIf, String, UInt8),
    column3 AggregateFunction(quantiles(0.5, 0.9), UInt64)
) ENGINE = ...
```

## 使用

### データの挿入

`AggregateFunction` 型のカラムを持つテーブルにデータを挿入するには、
集約関数と
[`-State`](/ja/reference/functions/aggregate-functions/combinators#-state) 集約関数
コンビネータを用いた `INSERT SELECT` を使用できます。

たとえば、`AggregateFunction(uniq, UInt64)` 型および
`AggregateFunction(quantiles(0.5, 0.9), UInt64)` 型のカラムに挿入する場合は、次の
コンビネータ付き集約関数を使用します。

```sql theme={null}
uniqState(UserID)
quantilesState(0.5, 0.9)(SendTiming)
```

関数 `uniq` と `quantiles` とは対照的に、`uniqState` と `quantilesState`
(`-State` コンビネータ を付加したもの) は、最終的な値ではなく状態を返します。
言い換えると、これらは `AggregateFunction` 型の値を返します。

`SELECT` クエリの結果では、`AggregateFunction` 型の値は、
ClickHouse のすべての出力フォーマットにおいて実装固有のバイナリ表現を持ちます。

入力値から状態を構築できる特別なセッションレベル設定 `aggregate_function_input_format` があります。
この設定は次のフォーマットをサポートします。

* `state` - シリアライズされた状態 (デフォルト) 。
  たとえば、`SELECT`
  クエリでデータを `TabSeparated` フォーマットにダンプした場合、その dump は `INSERT` クエリを使って再度読み込めます。
* `value` - このフォーマットでは、集約関数 の引数の単一の値、または複数の引数の場合はそれらの tuple を受け取ります。その値が集計されて状態が構築されます。
* `array` - このフォーマットでは、上記の `value` オプションで説明したとおり、値の Array を受け取ります。Array のすべての要素が集計されて状態が構築されます。

`value` モードおよび `array` モードでは、カラムはその値の型 (`T`、`Tuple(T1, T2)`、またはそれらの `Array`) を持つものとして、
その型に対して input format が使用する表現で読み取られます。これは `CSV` や `JSONEachRow` から `RowBinary` や `Parquet` に至るまで、すべての input format で同じように動作します。

### データの選択

`AggregatingMergeTree` テーブルからデータを選択する際は、`GROUP BY` 句と、
データの挿入時に使用したものと同じ集約関数を使いますが、
[`-Merge`](/ja/reference/functions/aggregate-functions/combinators#-merge) コンビネータを使用します。

集約関数に `-Merge` コンビネータを付加すると、複数の
状態 を受け取ってそれらを結合し、データ全体を集約した結果を返します。

たとえば、次の 2 つのクエリは同じ結果を返します。

```sql theme={null}
SELECT uniq(UserID) FROM table

SELECT uniqMerge(state) FROM (SELECT uniqState(UserID) AS state FROM table GROUP BY RegionID)
```

## 使用例

[AggregatingMergeTree](/ja/reference/engines/table-engines/mergetree-family/aggregatingmergetree) エンジンの説明をご覧ください。

## 関連コンテンツ

* ブログ: [ClickHouseでAggregate Combinatorを使う](https://clickhouse.com/blog/aggregate-functions-combinators-in-clickhouse-for-arrays-maps-and-states)
* [MergeState](/ja/reference/functions/aggregate-functions/combinators#-mergestate)
  コンビネータ。
* [State](/ja/reference/functions/aggregate-functions/combinators#-state) コンビネータ。
