Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TiDB vector search doc #18502

Open
wants to merge 75 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
48f906c
TiDB vector data type and vector index Doc
EricZequan Sep 2, 2024
0b31525
remove vector index part
EricZequan Sep 2, 2024
96f2701
modify cluster type
EricZequan Sep 2, 2024
3fdab1b
fix
EricZequan Sep 2, 2024
98e417b
modify expression
EricZequan Sep 3, 2024
9bd83d6
fix ci
EricZequan Sep 3, 2024
52934cb
fix comment
EricZequan Sep 3, 2024
26027f9
fix ci
EricZequan Sep 3, 2024
df68638
fix ci
EricZequan Sep 3, 2024
febd534
fix comment
EricZequan Sep 4, 2024
69429af
vector-search-overview: refine descriptions
qiancai Sep 4, 2024
d285b55
vector-search-data-types: refine descriptions
qiancai Sep 4, 2024
3852ed3
vector-search-functions-and-operators: refine descriptions
qiancai Sep 4, 2024
93b7e90
add remaining doc
EricZequan Sep 5, 2024
15e18d0
remove rows
EricZequan Sep 5, 2024
402f1d0
fix
EricZequan Sep 5, 2024
f869c42
get started: refine descriptions
qiancai Sep 5, 2024
374b687
Merge branch 'VectorFunction-and-VectorIndex' of https://github.com/E…
qiancai Sep 5, 2024
ea2b0b1
integrate-with-django-orm: refine descriptions
qiancai Sep 6, 2024
2cf3e79
Apply suggestions from code review
qiancai Sep 6, 2024
0eea4d7
fix comment
EricZequan Sep 6, 2024
93fe602
fix comment
EricZequan Sep 6, 2024
abe5eac
integrate-with-peewee/sqlalchemy: refine descriptions
qiancai Sep 6, 2024
a01ba0e
Merge branch 'VectorFunction-and-VectorIndex' of https://github.com/E…
qiancai Sep 6, 2024
48f47af
get-started and integrate-with-jinaai-embedding: refine descriptions
qiancai Sep 6, 2024
f9b6dc0
get-started-using-sql: update connection instructions
qiancai Sep 6, 2024
ae94864
Update vector-search-data-types.md
breezewish Sep 6, 2024
f485b58
integrate-with-llamaindex: refine descriptions
qiancai Sep 9, 2024
2e56e20
integrate-with-langchain: refine descriptions
qiancai Sep 9, 2024
bb7ce0b
overview and limitation: refine descriptions
qiancai Sep 9, 2024
d50b638
add vector index doc introduction
EricZequan Sep 10, 2024
5290946
fix comment
EricZequan Sep 10, 2024
eb604f7
modify introduction of self-hosted tidb connection type
EricZequan Sep 10, 2024
65656f8
modify tidb connection when using tidb self-hosted
EricZequan Sep 11, 2024
99f9ab7
fix comment
EricZequan Sep 11, 2024
2c5efa4
fix comment
EricZequan Sep 11, 2024
b2e32b7
fix comment
EricZequan Sep 11, 2024
9a51513
shorten index example case
EricZequan Sep 11, 2024
c09d85f
fix comment
EricZequan Sep 11, 2024
bf063b7
fix comment
EricZequan Sep 12, 2024
9eaf1f7
fix comment
EricZequan Sep 13, 2024
c62f7c9
fix comment
EricZequan Sep 13, 2024
0083ea1
fix comment
EricZequan Sep 13, 2024
6a673b9
index & improve performance: refine descriptions
qiancai Sep 13, 2024
5ebe116
Merge branch 'VectorFunction-and-VectorIndex' of https://github.com/E…
qiancai Sep 13, 2024
a250dff
add vector index part in other document
EricZequan Sep 14, 2024
94e8ab5
Merge branch 'VectorFunction-and-VectorIndex' of https://github.com/E…
EricZequan Sep 14, 2024
634c602
modify index name when create vector index
EricZequan Sep 14, 2024
4b54e6d
Update vector-search-improve-performance.md
EricZequan Sep 14, 2024
5eeb336
refine descriptions for TiDB self-managed connection
qiancai Sep 14, 2024
c6dad29
Merge branch 'VectorFunction-and-VectorIndex' of https://github.com/E…
qiancai Sep 14, 2024
9a77c65
fix comment
EricZequan Sep 14, 2024
502bd52
vector-index: refine descriptions
qiancai Sep 14, 2024
652b639
remove index part when create table in integration-doc
EricZequan Sep 14, 2024
05784ea
Resolve merge conflicts
EricZequan Sep 14, 2024
cfacbef
fix comment
EricZequan Sep 14, 2024
3a68b70
Merge remote-tracking branch 'upstream/master' into pr/18502
qiancai Sep 14, 2024
eb42ae8
TiDB Serverless -> TiDB Cloud Serverless
qiancai Sep 14, 2024
8d938d4
add the experimental warning
qiancai Sep 18, 2024
f7a31f2
fix comment
EricZequan Sep 19, 2024
894fcd4
fix comment
EricZequan Sep 19, 2024
92e1aee
fix comment
EricZequan Sep 23, 2024
0f91e5a
Apply suggestions from code review
qiancai Sep 24, 2024
39958f3
UI changes: Endpoint Type -> Connection Type
qiancai Sep 24, 2024
f8af8f6
fix comment
EricZequan Sep 24, 2024
12abce8
fix comment
EricZequan Sep 24, 2024
c9ef22f
fix comment
EricZequan Sep 25, 2024
3350c10
remove 'vector64()' sytax
EricZequan Sep 26, 2024
f18d840
Update desc about tiflash upgrade
JaySon-Huang Sep 27, 2024
9cbc09e
Update desc about br support
JaySon-Huang Sep 29, 2024
13bc862
Add limitation about BR restore
JaySon-Huang Sep 29, 2024
7704118
Update desc about limitation
JaySon-Huang Sep 29, 2024
d135903
add limit about cdc
wk989898 Sep 29, 2024
f374485
Update tiflash-configuration
JaySon-Huang Sep 29, 2024
2ba7811
fix comment
EricZequan Sep 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions br/backup-and-restore-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ TiDB 支持将数据备份到 Amazon S3、Google Cloud Storage (GCS)、Azure Blo
| 全局临时表 | | 确保使用 BR v5.3.0 及以上版本进行备份和恢复,否则会导致全局临时表的表定义错误。 |
| TiDB Lightning 物理导入模式| |上游数据库使用 TiDB Lightning 物理导入模式导入的数据,无法作为数据日志备份下来。推荐在数据导入后执行一次全量备份,细节参考[上游数据库使用 TiDB Lightning 物理导入模式导入数据的恢复](/faq/backup-and-restore-faq.md#上游数据库使用-tidb-lightning-物理导入模式导入数据时为什么无法使用日志备份功能)。|
| TiCDC | | BR v8.2.0 及以上版本:如果在恢复的目标集群有 [CheckpointTS](/ticdc/ticdc-architecture.md#checkpointts) 早于 BackupTS 的 Changefeed,BR 会拒绝执行恢复。BR v8.2.0 之前的版本:如果在恢复的目标集群有任何活跃的 TiCDC Changefeed,BR 会拒绝执行恢复。 |
| 向量搜索 | | 确保使用 BR v8.4.0 及以上版本进行备份与恢复。不支持将带有向量数据类型的恢复至 v8.4.0 之前的 TiDB 集群。 |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add limitation about BR


### 版本间兼容性

Expand Down
4 changes: 4 additions & 0 deletions dm/dm-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ tiup install dm dmctl

- DM 不支持 MySQL 8.0 的新特性 binlog 事务压缩 [Transaction_payload_event](https://dev.mysql.com/doc/refman/8.0/en/binary-log-transaction-compression.html)。使用 binlog 事务压缩有导致上下游数据不一致的风险。

+ 向量类型数据同步

- DM 不支持 MySQL 9.0 的向量数据类型同步。
Comment on lines +63 to +65
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add limitation about DM


## Contributing

欢迎参与 DM 开源项目并万分感谢您的贡献,可以查看 [CONTRIBUTING.md](https://github.com/pingcap/tiflow/blob/master/dm/CONTRIBUTING.md) 了解更多信息。
Expand Down
Binary file added media/vector-search/embedding-search.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions ticdc/ticdc-compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,11 @@ TiCDC 从 v5.3.0 开始支持[全局临时表](/temporary-tables.md#全局临时
你需要使用 TiCDC v5.3.0 及以上版本同步全局临时表到下游。低于该版本,会导致表定义错误。

如果 TiCDC 的上游集群包含全局临时表,下游集群也必须是 TiDB 5.3.0 及以上版本,否则同步报错。

### 向量搜索功能兼容性说明(实验特性)

TiCDC 从 v8.4.0 开始支持[向量搜索功能](/vector-search-overview.md)。

写入下游为 Kafka 或者存储服务(如:Amazon S3、GCS、Azure Blob Storage 和 NFS)时,会将向量类型转为字符串类型。

写入到不支持向量类型的 MySQL 兼容数据库时, 涉及向量类型的 DDL 事件无法成功写入。在`sink-url`中添加配置参数`has-vector-type=true`后,会将向量类型转为 `LONGTEXT` 类型进行写入。
4 changes: 4 additions & 0 deletions tiflash-upgrade-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ TiFlash 在 v6.2.0 将数据格式升级到 V3 版本,因此,从 v5.x 或 v6

从 v7.4 开始,为了减少数据整理时产生的读、写放大,PageStorage V3 数据整理时逻辑进行了优化,导致底层部分存储文件名发生改动。因此,升级 TiFlash 到 v7.4 或以上版本后,不支持原地降级到之前的版本。

## 从 v7.x 升级至 v8.4 或以上版本

从 v8.4 开始,为了支持[向量搜索功能](/vector-search-index.md),TiFlash 底层存储格式发生改动。因此,升级 TiFlash 到 v8.4 或以上版本后,不支持原地降级到之前的版本。
Comment on lines +122 to +124
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I've pushed a commit about the tiflash upgrade notice


**测试环境及特殊回退需求下的对策**

如果在测试环境下或者其他有特殊回退需求的场景下,可以强制缩容 TiFlash 节点,并重新同步数据。操作步骤详见[缩容 TiFlash 节点](/scale-tidb-using-tiup.md#缩容-tiflash-节点)。
4 changes: 3 additions & 1 deletion tiflash/tiflash-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ delta_index_cache_size = 0
## * format_version = 3 v6.0.0 及 v6.1.x 版本的默认文件格式,具有更完善的检验功能
## * format_version = 4 v7.3.0 及以前版本的默认文件格式,优化了写放大问题,同时减少了后台线程消耗。
## * format_version = 5 v7.4.0 及以后版本的默认文件格式(从 v7.3.0 开始引入),该格式可以合并小文件从而减少了物理文件数量。
# format_version = 5
## * format_version = 6 从 v8.4.0 开始引入,部分支持了向量索引的构建与存储。
## * format_version = 7 v8.4.0 及以后版本的默认文件格式 (从 v8.4.0 开始引入),该格式用于支持向量索引的构建与存储。
# format_version = 7

[storage.main]
## 用于存储主要的数据,该目录列表中的数据占总数据的 90% 以上。
Expand Down
248 changes: 248 additions & 0 deletions vector-search-data-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
---
title: 向量数据类型
summary: 本文介绍 TiDB 的向量数据类型。
---

# 向量数据类型 (Vector)

向量指的是一组浮点数序列,例如 `[0.3, 0.5, -0.1, ...]`。针对 AI 应用中大量使用到的嵌入向量 (vector embedding) 数据,TiDB 专门提供了向量数据类型,以便高效地存储和访问这些数据。

> **警告:**
>
> 该功能目前为实验特性,不建议在生产环境中使用。该功能可能会在未事先通知的情况下发生变化或删除。如果发现 bug,请在 GitHub 上提 [issue](https://github.com/pingcap/tidb/issues) 反馈。

目前支持的向量数据类型包括:

- `VECTOR`: 存储一组单精度浮点数 (Float) 向量,向量维度可以是任意的。
- `VECTOR(D)`: 存储一组单精度浮点数 (Float) 向量,向量维度固定为 `D`。

与使用 [`JSON`](/data-type-json.md) 类型相比,使用向量类型具有以下优势:

- 支持向量索引。可以通过构建[向量搜索索引](/vector-search-index.md)加速查询。
- 可指定维度。指定一个固定维度后,不符合维度的数据将被阻止写入到表中。
- 存储格式更优。向量数据类型针对向量数据进行了特别优化,在空间利用和性能效率上都优于 `JSON` 类型。

## 语法

可以使用以下格式的字符串来表示一个数据类型为向量的值:

```sql
'[<float>, <float>, ...]'
```

示例:

```sql
CREATE TABLE vector_table (
id INT PRIMARY KEY,
embedding VECTOR(3)
);

INSERT INTO vector_table VALUES (1, '[0.3, 0.5, -0.1]');

INSERT INTO vector_table VALUES (2, NULL);
```

当将不符合语法的字符串作为向量数据插入时,TiDB 会进行报错:

```sql
[tidb]> INSERT INTO vector_table VALUES (3, '[5, ]');
ERROR 1105 (HY000): Invalid vector text: [5, ]
```

下面的示例中 `embedding` 向量列的维度在建表时已经定义为 `3`,因此当插入其他维度的向量数据时,TiDB 会进行报错:

```sql
[tidb]> INSERT INTO vector_table VALUES (4, '[0.3, 0.5]');
ERROR 1105 (HY000): vector has 2 dimensions, does not fit VECTOR(3)
```

可参阅[向量函数与操作符](/vector-search-functions-and-operators.md)了解向量数据类型支持的所有函数和操作符。

可参阅[向量搜索索引](/vector-search-index.md)了解向量搜索索引的信息。

## 混合存储不同维度的向量

省略 `VECTOR` 类型中的维度参数后,就可以在同一列中存储不同维度的向量:

```sql
CREATE TABLE vector_table (
id INT PRIMARY KEY,
embedding VECTOR
);

INSERT INTO vector_table VALUES (1, '[0.3, 0.5, -0.1]'); -- 插入一个 3 维向量
INSERT INTO vector_table VALUES (2, '[0.3, 0.5]'); -- 插入一个 2 维向量
```

需要注意的是,存储了不同维度向量的列不支持构建[向量搜索索引](/vector-search-index.md),因为只有维度相同的向量之间才能计算向量距离。

## 比较

[比较运算符](/vector-search-functions-and-operators.md#扩展的内置函数和运算符) 如 `=`, `!=`, `<`, `>`, `<=` 和 `>=` 等都能正常对向量数据进行比较。可参阅[向量函数与操作符](/vector-search-functions-and-operators.md#扩展的内置函数和运算符)了解向量数据类型支持的所有函数和操作符。

比较向量数据类型时,TiDB 会以向量中的各个元素为单位进行依次比较,如:

- `[1] < [12]`
- `[1,2,3] < [1,2,5]`
- `[1,2,3] = [1,2,3]`
- `[2,2,3] > [1,2,3]`

当两个向量的维度不同时,TiDB 采用字典序 (Lexicographical Order) 进行比较,具体规则如下:

- 两个向量中的各个元素逐一进行数值比较。
- 当遇到第一个不同的元素时,它们之间的数值比较结果即为两个向量之间的比较结果。
- 如果一个向量是另一个向量的前缀,那么维度小的向量 _小于_ 维度大的向量。例如,`[1,2,3] < [1,2,3,0]`。
- 长度相同且各个元素相同的两个向量 _相等_ 。
- 空向量 _小于_ 任何非空向量。例如,`[] < [1]`。
- 两个空向量 _相等_ 。


在进行向量比较时,请使用[显式转换](#类型转换-cast)将向量数据从字符串转换为向量类型,以避免 TiDB 直接基于字符串进行比较:

```sql
-- 因为给出的数据实际上是字符串,因此 TiDB 会按字符串进行比较
[tidb]> SELECT '[12.0]' < '[4.0]';
+--------------------+
| '[12.0]' < '[4.0]' |
+--------------------+
| 1 |
+--------------------+
1 row in set (0.01 sec)

-- 显式转换为向量类型,从而按照向量的比较规则进行正确的比较
[tidb]> SELECT VEC_FROM_TEXT('[12.0]') < VEC_FROM_TEXT('[4.0]');
+--------------------------------------------------+
| VEC_FROM_TEXT('[12.0]') < VEC_FROM_TEXT('[4.0]') |
+--------------------------------------------------+
| 0 |
+--------------------------------------------------+
1 row in set (0.01 sec)
```

## 运算

向量数据类型支持算术运算 `+` 和 `-`,对应的是两个向量以元素为单位进行的加法和减法。不支持对不同维度向量进行算术运算,执行这类运算会遇到报错。

以下是一些示例:

```sql
[tidb]> SELECT VEC_FROM_TEXT('[4]') + VEC_FROM_TEXT('[5]');
+---------------------------------------------+
| VEC_FROM_TEXT('[4]') + VEC_FROM_TEXT('[5]') |
+---------------------------------------------+
| [9] |
+---------------------------------------------+
1 row in set (0.01 sec)

[tidb]> SELECT VEC_FROM_TEXT('[2,3,4]') - VEC_FROM_TEXT('[1,2,3]');
+-----------------------------------------------------+
| VEC_FROM_TEXT('[2,3,4]') - VEC_FROM_TEXT('[1,2,3]') |
+-----------------------------------------------------+
| [1,1,1] |
+-----------------------------------------------------+
1 row in set (0.01 sec)

[tidb]> SELECT VEC_FROM_TEXT('[4]') + VEC_FROM_TEXT('[1,2,3]');
ERROR 1105 (HY000): vectors have different dimensions: 1 and 3
```

## 类型转换 (Cast)

### 向量与字符串之间的转换

可以使用以下函数在向量和字符串之间进行转换:

- `CAST(... AS VECTOR)`: 将字符串类型转换为向量类型
- `CAST(... AS CHAR)`: 将向量类型转换为字符串类型
- `VEC_FROM_TEXT`: 将字符串类型转换为向量类型
- `VEC_AS_TEXT`: 将向量类型转换为字符串类型
Comment on lines +156 to +159
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `CAST(... AS VECTOR)`: 将字符串类型转换为向量类型
- `CAST(... AS CHAR)`: 将向量类型转换为字符串类型
- `VEC_FROM_TEXT`: 将字符串类型转换为向量类型
- `VEC_AS_TEXT`: 将向量类型转换为字符串类型
- `CAST(... AS VECTOR)`将字符串类型转换为向量类型
- `CAST(... AS CHAR)`将向量类型转换为字符串类型
- `VEC_FROM_TEXT`将字符串类型转换为向量类型
- `VEC_AS_TEXT`将向量类型转换为字符串类型


出于易用性考虑,如果你使用的函数只支持向量数据类型(例如,向量相关距离函数),那么你也可以直接传入符合格式要求的字符串数据,TiDB 会进行隐式转换:

```sql
-- VEC_DIMS 只接受向量类型,因此你可以直接传入字符串类型,TiDB 会隐式转换为向量类型:
[tidb]> SELECT VEC_DIMS('[0.3, 0.5, -0.1]');
+------------------------------+
| VEC_DIMS('[0.3, 0.5, -0.1]') |
+------------------------------+
| 3 |
+------------------------------+
1 row in set (0.01 sec)

-- 也可以使用 VEC_FROM_TEXT 显式地将字符串转换为向量类型后传递给 VEC_DIMS 函数:
[tidb]> SELECT VEC_DIMS(VEC_FROM_TEXT('[0.3, 0.5, -0.1]'));
+---------------------------------------------+
| VEC_DIMS(VEC_FROM_TEXT('[0.3, 0.5, -0.1]')) |
+---------------------------------------------+
| 3 |
+---------------------------------------------+
1 row in set (0.01 sec)

-- 也可以使用 CAST(... AS VECTOR) 进行显式转换:
[tidb]> SELECT VEC_DIMS(CAST('[0.3, 0.5, -0.1]' AS VECTOR));
+----------------------------------------------+
| VEC_DIMS(CAST('[0.3, 0.5, -0.1]' AS VECTOR)) |
+----------------------------------------------+
| 3 |
+----------------------------------------------+
1 row in set (0.01 sec)
```

当你使用的运算符或函数接受多种数据类型时,TiDB 不会进行隐式转换,请先显式地将字符串类型转换为向量类型后,再传递给这些运算符或函数。例如,进行比较运算前,需要显式地将字符串转换为向量类型,否则 TiDB 将会按照字符串类型进行比较,而非按照向量类型进行比较:

```sql
-- 传入的类型是字符串,因此 TiDB 会按字符串进行比较:
[tidb]> SELECT '[12.0]' < '[4.0]';
+--------------------+
| '[12.0]' < '[4.0]' |
+--------------------+
| 1 |
+--------------------+
1 row in set (0.01 sec)

-- 转换为向量类型,以便使用向量类型的比较规则:
[tidb]> SELECT VEC_FROM_TEXT('[12.0]') < VEC_FROM_TEXT('[4.0]');
+--------------------------------------------------+
| VEC_FROM_TEXT('[12.0]') < VEC_FROM_TEXT('[4.0]') |
+--------------------------------------------------+
| 0 |
+--------------------------------------------------+
1 row in set (0.01 sec)
```

向量也可以显式地转换为字符串。以使用 `VEC_AS_TEXT()` 函数为例:

```sql
-- 字符串首先被隐式地转换成向量,然后被显式地转为字符串,因而返回了一个规范化的字符串格式:
[tidb]> SELECT VEC_AS_TEXT('[0.3, 0.5, -0.1]');
+--------------------------------------+
| VEC_AS_TEXT('[0.3, 0.5, -0.1]') |
+--------------------------------------+
| [0.3,0.5,-0.1] |
+--------------------------------------+
1 row in set (0.01 sec)
```

如需了解其他转换函数,请参阅[向量函数和操作符](/vector-search-functions-and-operators.md)。

### 向量与其他数据类型之间的转换

目前 TiDB 无法直接在向量和其他数据类型(如 `JSON`)之间进行转换,但你可以使用字符串作为中间类型进行转换。

## 使用限制

- 向量最大支持 16383 维。
- 向量数据中不支持 `NaN`、`Infinity` 和 `-Infinity` 浮点数。
- 目前,向量类型不能存储双精度浮点数。计划在未来的版本中支持这一功能。在此期间,如果为向量类型导入双精度浮点数,它们将被转换为单精度数。

有关其他限制,请参阅[向量搜索限制](/vector-search-limitations.md)。

## MySQL 兼容性

向量数据类型只在 TiDB 中支持,MySQL 不支持。

## 另请参阅

- [向量函数和操作符](/vector-search-functions-and-operators.md)
- [向量搜索索引](/vector-search-index.md)
Loading
Loading