> ## 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.

# Vortex

> Vortex 格式文档

| 输入 | 输出 | 别名 |
| - | - | - |
| ✔ | ✔ | |

## 描述

[Vortex](https://vortex.dev/) 是一种可扩展的列式文件格式，用于存储兼容 Apache Arrow 的已压缩数据，
专为快速扫描和随机访问而设计。ClickHouse 支持读取和写入 Vortex 文件。

## 数据类型匹配

下表列出了在 `INSERT` 和 `SELECT` 查询中，Vortex 数据类型与 ClickHouse [数据类型](https://clickhouse.com/docs/zh/reference/data-types/index.md)之间的对应关系。

| Vortex 数据类型 (`INSERT`) | ClickHouse 数据类型 | Vortex 数据类型 (`SELECT`) |
| - | - | - |
| `Bool` | [Bool](https://clickhouse.com/docs/zh/reference/data-types/boolean.md) | `Bool` |
| `I8`, `U8` | [Int8/UInt8](https://clickhouse.com/docs/zh/reference/data-types/int-uint.md) | `I8`, `U8` |
| `I16`, `U16` | [Int16/UInt16](https://clickhouse.com/docs/zh/reference/data-types/int-uint.md) | `I16`, `U16` |
| `I32`, `U32` | [Int32/UInt32](https://clickhouse.com/docs/zh/reference/data-types/int-uint.md) | `I32`, `U32` |
| `I64`, `U64` | [Int64/UInt64](https://clickhouse.com/docs/zh/reference/data-types/int-uint.md) | `I64`, `U64` |
| `F32` | [Float32](https://clickhouse.com/docs/zh/reference/data-types/float.md) | `F32` |
| `F64` | [Float64](https://clickhouse.com/docs/zh/reference/data-types/float.md) | `F64` |
| `Utf8`, `Binary` | [String](https://clickhouse.com/docs/zh/reference/data-types/string.md) | `Utf8` (当 `output_format_vortex_string_as_string = 0` 时为 `Binary`) |
| `Binary` | [FixedString](https://clickhouse.com/docs/zh/reference/data-types/fixedstring.md) | `Binary` |
| `Decimal` | [Decimal](https://clickhouse.com/docs/zh/reference/data-types/decimal.md) | `Decimal` |
| `vortex.date` | [Date32](https://clickhouse.com/docs/zh/reference/data-types/date32.md) | `vortex.date` |
| `vortex.timestamp` | [DateTime](https://clickhouse.com/docs/zh/reference/data-types/datetime.md)/[DateTime64](https://clickhouse.com/docs/zh/reference/data-types/datetime64.md) | `vortex.timestamp` |
| `vortex.time` | [Time64](https://clickhouse.com/docs/zh/reference/data-types/time64.md) | `vortex.time` |
| `List` | [Array](https://clickhouse.com/docs/zh/reference/data-types/array.md) | `List` |
| `Struct` | [Tuple](https://clickhouse.com/docs/zh/reference/data-types/tuple.md) | `Struct` |
| `Null` | [Nullable(Nothing)](https://clickhouse.com/docs/zh/reference/data-types/special-data-types/nothing.md) | `Null` |

不支持其他类型。具体而言，[Map](https://clickhouse.com/docs/zh/reference/data-types/map.md)、
[Int128/UInt128/Int256/UInt256](https://clickhouse.com/docs/zh/reference/data-types/int-uint.md)、[IPv6](https://clickhouse.com/docs/zh/reference/data-types/ipv6.md)
和 [Interval](https://clickhouse.com/docs/zh/reference/data-types/special-data-types/interval.md) 列无法写入 Vortex 文件。
[String](https://clickhouse.com/docs/zh/reference/data-types/string.md) 列以 `Utf8` 写入，Vortex 对其的压缩效果优于
`Binary`。Vortex 要求 `Utf8` 值必须是有效的 UTF-8，而 ClickHouse 字符串可以是任意
字节序列，因此写入非有效 UTF-8 的值时会抛出异常；若设置
`output_format_vortex_string_as_string = 0`，字符串将改为以 `Binary` 写入。Vortex 没有
定长二进制类型，因此 [FixedString](https://clickhouse.com/docs/zh/reference/data-types/fixedstring.md) 始终以 `Binary` 写入；
schema 推断会将其读回为 [String](https://clickhouse.com/docs/zh/reference/data-types/string.md)。
[LowCardinality](https://clickhouse.com/docs/zh/reference/data-types/lowcardinality.md) 列以其底层类型写入
(Vortex 会自行自适应地选择字典编码及其他编码) 。
[DateTime](https://clickhouse.com/docs/zh/reference/data-types/datetime.md) 列以秒级精度的 `vortex.timestamp` 写入，
因此读回时为标度为 0 的 [DateTime64](https://clickhouse.com/docs/zh/reference/data-types/datetime64.md)。
`vortex.timestamp` 记录的是单位而非标度，因此只有能被某个单位精确表示的标度——
0、3、6 和 9——能在 schema 推断后保持不变；其他标度都会以下一个更宽的单位写入，
并被推断为该单位对应的标度，因此 [DateTime64](https://clickhouse.com/docs/zh/reference/data-types/datetime64.md)`(1)` 和 `(2)` 读回时
为 `DateTime64(3)`，`(4)` 和 `(5)` 为 `DateTime64(6)`，`(7)` 和 `(8)` 为 `DateTime64(9)`。这些值
会被重新缩放，其含义保持不变；变化的只是声明的标度，而显式指定类型即可原样
读回。[Time64](https://clickhouse.com/docs/zh/reference/data-types/time64.md) 同理。
`vortex.time` 表示一天中的时刻，而 ClickHouse 中的 [Time/Time64](https://clickhouse.com/docs/zh/reference/data-types/time64.md) 还可以包含
负值以及大于等于 24 小时的值；写入此类值时会抛出异常，而不会写入错误的值。
[IPv4](https://clickhouse.com/docs/zh/reference/data-types/ipv4.md) 列以 `U32` 写入，因为 Vortex 没有 IP 地址类型，
所以 schema 推断会将其读回为 [UInt32](https://clickhouse.com/docs/zh/reference/data-types/int-uint.md)。如需将此类列读回为 `IPv4`，
请显式指定类型：`SELECT * FROM file('data.vortex', Vortex, 'ip IPv4')`。

ClickHouse 表列的数据类型不必与对应的 Vortex 数据字段一致。
插入数据时，ClickHouse 会按照上表解释数据类型，然后将数据
[转换](https://clickhouse.com/docs/zh/reference/functions/regular-functions/type-conversion-functions.md#CAST)为 ClickHouse 表列所设置的数据类型。

## 使用示例

可以从 Vortex 文件中查询数据：

```sql theme={null}
SELECT * FROM file('data.vortex');
```

然后将数据写入 Vortex 文件：

```sql theme={null}
SELECT * FROM numbers(3) INTO OUTFILE 'numbers.vortex' FORMAT Vortex;
```

## 格式设置

| 设置项 | 描述 | 默认值 |
| - | - | - |
| `input_format_vortex_filter_push_down` | 将 `WHERE` 条件中可转换的部分下推到扫描中，使选择性查询读取和解码的数据更少。只有已转换的那部分条件会在扫描内部生效：对于仅部分可转换的条件，会将其放宽为至少保留所有匹配行的条件，因此不匹配的行仍可能被读取和解码，随后 ClickHouse 会重新应用完整的过滤器。下推目前支持顶层的整数、浮点、字符串/二进制、`Bool`、`Date`、`Date32` 和 `DateTime64` 列。 | `1` |
| `input_format_vortex_preserve_order` | 按文件中的顺序返回行。默认情况下，文件的各个行拆分会并行解码，并在就绪后立即返回，因此不保证行的顺序；启用该设置后，较慢的拆分会阻塞其后的拆分。 | `0` |
| `output_format_vortex_string_as_string` | 将 [String](https://clickhouse.com/docs/zh/reference/data-types/string.md) 列写入为 `Utf8` 而非 `Binary`。如果值不是有效的 UTF-8，则会抛出异常；禁用该设置即可将此类数据写入为 `Binary`。 | `1` |
| `max_parsing_threads` | 用于解码 Vortex 文件的线程数 (由同一查询并行读取的多个文件共享) 。设为 `1` 时禁用线程池：此时文件在 `read` 内部、于查询管道的线程上读取。 | CPU 核心数 |
| `max_download_threads` | 用于读取文件的线程数。设为 `0` 时，读取操作与解码共享线程。 | `4` |

与其他列式格式一样，只会从文件中读取查询用到的列，
文件中缺失的列则以默认值填充。

## 性能

文件采用并行方式读取。扫描时会将文件拆分为若干行范围(与所请求列的块边界对齐,每个范围最多 100 000 行),并对这些范围并发地执行读取、过滤和解码:
解码最多使用 `max_parsing_threads` 个线程,这些线程与 `Parquet` 读取器来自同一个线程池;
读取最多使用 `max_download_threads` 个线程;已解码的块转换为
ClickHouse 列的过程,则在负责解码该块的线程上完成。块一旦
就绪便会立即返回,因此除非设置了 `input_format_vortex_preserve_order`,否则无法保证行的顺序。对于
文件中位置相近的分段,其读取会合并为一个请求(本地文件最多 4 MiB,
远程存储最多 16 MiB)。对于选择性较高的查询,过滤器下推可以减少需要读取和解码的
数据量。
