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

> ClickHouse 배열 데이터 타입 문서

# Array(T)

배열 인덱스는 1부터 시작하며, `T` 타입 항목으로 이루어진 배열입니다. `T`에는 배열을 포함해 모든 데이터 타입을 사용할 수 있습니다.

<div id="creating-an-array">
  ## 배열 만들기
</div>

함수를 사용해 배열을 만들 수 있습니다:

```sql theme={null}
array(T)
```

또 `[]`를 사용할 수 있습니다.

```sql theme={null}
[]
```

배열을 생성하는 예시:

```sql theme={null}
SELECT array(1, 2) AS x, toTypeName(x)
```

```text theme={null}
┌─x─────┬─toTypeName(array(1, 2))─┐
│ [1,2] │ Array(UInt8)            │
└───────┴─────────────────────────┘
```

```sql theme={null}
SELECT [1, 2] AS x, toTypeName(x)
```

```text theme={null}
┌─x─────┬─toTypeName([1, 2])─┐
│ [1,2] │ Array(UInt8)       │
└───────┴────────────────────┘
```

<div id="working-with-data-types">
  ## 데이터 타입 다루기
</div>

즉석에서 배열을 생성할 때 ClickHouse는 나열된 모든 인수를 저장할 수 있는 가장 범위가 좁은 데이터 타입으로 인수 타입을 자동 지정합니다. [Nullable](/ko/reference/data-types/nullable) 또는 리터럴 [NULL](/ko/reference/settings/formats#input_format_null_as_default) 값이 하나라도 있으면 배열 요소의 타입도 [Nullable](/ko/reference/data-types/nullable)이 됩니다.

ClickHouse가 데이터 타입을 결정할 수 없으면 예외를 발생시킵니다. 예를 들어 문자열과 숫자를 동시에 사용해 배열을 만들려고 할 때 이런 문제가 발생합니다(`SELECT array(1, 'a')`).

자동 데이터 타입 감지 예시:

```sql theme={null}
SELECT array(1, 2, NULL) AS x, toTypeName(x)
```

```text theme={null}
┌─x──────────┬─toTypeName(array(1, 2, NULL))─┐
│ [1,2,NULL] │ Array(Nullable(UInt8))        │
└────────────┴───────────────────────────────┘
```

호환되지 않는 데이터 타입으로 배열을 생성하려고 하면 ClickHouse에서 예외가 발생합니다:

```sql theme={null}
SELECT array(1, 'a')
```

```text theme={null}
Received exception from server (version 1.1.54388):
Code: 386. DB::Exception: Received from localhost:9000, 127.0.0.1. DB::Exception: There is no supertype for types UInt8, String because some of them are String/FixedString and some of them are not.
```

<div id="array-size">
  ## 배열 크기
</div>

전체 컬럼을 읽지 않고도 `size0` 서브컬럼을 사용해 배열의 크기를 확인할 수 있습니다. 다차원 배열에는 `sizeN-1`을 사용할 수 있으며, 여기서 `N`은 원하는 차원입니다.

**예시**

```sql title="Query" theme={null}
CREATE TABLE t_arr (`arr` Array(Array(Array(UInt32)))) ENGINE = MergeTree ORDER BY tuple();

INSERT INTO t_arr VALUES ([[[12, 13, 0, 1],[12]]]);

SELECT arr.size0, arr.size1, arr.size2 FROM t_arr;
```

```text title="Response" theme={null}
┌─arr.size0─┬─arr.size1─┬─arr.size2─┐
│         1 │ [2]       │ [[4,1]]   │
└───────────┴───────────┴───────────┘
```

<div id="reading-nested-subcolumns-from-array">
  ## 배열에서 중첩된 서브컬럼 읽기
</div>

`Array` 안의 중첩 타입 `T`에 서브컬럼이 있으면(예: [named tuple](/ko/reference/data-types/tuple)), 동일한 서브컬럼 이름으로 `Array(T)` 타입에서 해당 서브컬럼을 읽을 수 있습니다. 서브컬럼의 타입은 원래 서브컬럼 타입의 `Array`입니다.

**예시**

```sql theme={null}
CREATE TABLE t_arr (arr Array(Tuple(field1 UInt32, field2 String))) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t_arr VALUES ([(1, 'Hello'), (2, 'World')]), ([(3, 'This'), (4, 'is'), (5, 'subcolumn')]);
SELECT arr.field1, toTypeName(arr.field1), arr.field2, toTypeName(arr.field2) from t_arr;
```

```test theme={null}
┌─arr.field1─┬─toTypeName(arr.field1)─┬─arr.field2────────────────┬─toTypeName(arr.field2)─┐
│ [1,2]      │ Array(UInt32)          │ ['Hello','World']         │ Array(String)          │
│ [3,4,5]    │ Array(UInt32)          │ ['This','is','subcolumn'] │ Array(String)          │
└────────────┴────────────────────────┴───────────────────────────┴────────────────────────┘
```
