> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Array(T)

> ClickHouse 中 Array 数据类型的文档

由 `T` 类型元素构成的数组，数组索引从 1 开始。`T` 可以是任何数据类型，包括数组。

## 创建 array

您可以使用函数来创建 array：

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

也可以使用 `[]`。

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

此外，还支持 SQL 标准、PostgreSQL 和 Trino 的数组构造语法：`ARRAY[1, 2]` 等价于 `[1, 2]`。
该数据类型也可以写作 `ARRAY(T)`。

创建数组的示例：

```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)       │
└───────┴────────────────────┘
```

## 使用数据类型

动态创建数组时，ClickHouse 会自动将参数类型确定为能够容纳所有已列出参数的最窄数据类型。如果包含任何 [Nullable](https://clickhouse.com/docs/zh/reference/data-types/nullable.md) 或字面量 [NULL](https://clickhouse.com/docs/zh/reference/settings/formats/input-format.md#input_format_null_as_default) 值，数组元素的类型也会变为 [Nullable](https://clickhouse.com/docs/zh/reference/data-types/nullable.md)。

如果 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.
```

## Array 的大小

可以通过 `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]]   │
└───────────┴───────────┴───────────┘
```

## 从 Array 中读取嵌套子列

如果 `Array` 中的嵌套类型 `T` 具有子列 (例如，它是一个[命名元组](https://clickhouse.com/docs/zh/reference/data-types/tuple.md)) ，则可以通过 `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)          │
└────────────┴────────────────────────┴───────────────────────────┴────────────────────────┘
```
