Skip to content

Commit f3ccb9a

Browse files
authored
docs: Update vector index sql examples (#2907)
1 parent b987c7c commit f3ccb9a

File tree

7 files changed

+236
-88
lines changed

7 files changed

+236
-88
lines changed

docs/en/sql-reference/00-sql-reference/10-data-types/vector.md

Lines changed: 39 additions & 38 deletions
Large diffs are not rendered by default.

docs/en/sql-reference/20-sql-functions/07-aggregate-functions/aggregate-array-agg.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: ARRAY_AGG
33
title_includes: LIST
44
---
55

6-
The ARRAY_AGG function (also known by its alias LIST) transforms all the values, including NULL, of a specific column in a query result into an array.
6+
The ARRAY_AGG function (also known by its alias LIST) transforms all the values, excluding NULL, of a specific column in a query result into an array.
77

88
## Syntax
99

@@ -68,4 +68,4 @@ GROUP BY movie_title;
6868
| movie_title | ratings |
6969
|-------------|------------|
7070
| Inception | [5, 5, 4] |
71-
```
71+
```

docs/en/sql-reference/20-sql-functions/11-vector-functions/00-vector-cosine-distance.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,28 @@ This function performs vector computations within Databend and does not rely on
4646

4747
## Examples
4848

49+
### Basic Usage
50+
51+
```sql
52+
-- Calculate cosine distance between two vectors
53+
SELECT COSINE_DISTANCE([1.0, 2.0, 3.0]::vector(3), [4.0, 5.0, 6.0]::vector(3)) AS distance;
54+
```
55+
56+
Result:
57+
```
58+
╭─────────────╮
59+
│ distance │
60+
├─────────────┤
61+
│ 0.025368214 │
62+
╰─────────────╯
63+
```
64+
4965
Create a table with vector data:
5066

5167
```sql
5268
CREATE OR REPLACE TABLE vectors (
5369
id INT,
54-
vec VECTOR(3),
55-
VECTOR INDEX idx_vec(vec) distance='cosine'
70+
vec VECTOR(3)
5671
);
5772

5873
INSERT INTO vectors VALUES
@@ -64,20 +79,22 @@ INSERT INTO vectors VALUES
6479
Find the vector most similar to [1, 2, 3]:
6580

6681
```sql
67-
SELECT
82+
SELECT
83+
id,
6884
vec,
6985
COSINE_DISTANCE(vec, [1.0000, 2.0000, 3.0000]::VECTOR(3)) AS distance
7086
FROM
7187
vectors
7288
ORDER BY
73-
distance ASC
74-
LIMIT 1;
89+
distance ASC;
7590
```
7691

7792
```
78-
+-------------------------+----------+
79-
| vec | distance |
80-
+-------------------------+----------+
81-
| [1.0000,2.2000,3.0000] | 0.0 |
82-
+-------------------------+----------+
93+
╭────────────────────────────────────╮
94+
│ id │ vec │ distance │
95+
├────┼───────────┼───────────────────┤
96+
│ 1 │ [1,2,3] │ 0.000000059604645 │
97+
│ 2 │ [1,2.2,3] │ 0.00096315145 │
98+
│ 3 │ [4,5,6] │ 0.025368214 │
99+
╰────────────────────────────────────╯
83100
```

docs/en/sql-reference/20-sql-functions/11-vector-functions/01-vector-l2-distance.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,28 @@ Where v1ᵢ and v2ᵢ are the elements of the input vectors.
4949

5050
## Examples
5151

52+
### Basic Usage
53+
54+
```sql
55+
-- Calculate L2 distance between two vectors
56+
SELECT L2_DISTANCE([1.0, 2.0, 3.0]::vector(3), [4.0, 5.0, 6.0]::vector(3)) AS distance;
57+
```
58+
59+
Result:
60+
```
61+
╭──────────╮
62+
│ distance │
63+
├──────────┤
64+
│ 5.196152 │
65+
╰──────────╯
66+
```
67+
5268
Create a table with vector data:
5369

5470
```sql
5571
CREATE OR REPLACE TABLE vectors (
5672
id INT,
57-
vec VECTOR(3),
58-
VECTOR INDEX idx_vec(vec) distance='l2'
73+
vec VECTOR(3)
5974
);
6075

6176
INSERT INTO vectors VALUES
@@ -78,11 +93,12 @@ ORDER BY
7893
```
7994

8095
```
81-
+----+-------------------------+----------+
82-
| id | vec | distance |
83-
+----+-------------------------+----------+
84-
| 1 | [1.0000,2.0000,3.0000] | 0.0 |
85-
| 2 | [1.0000,2.2000,3.0000] | 0.2 |
86-
| 3 | [4.0000,5.0000,6.0000] | 5.196152 |
87-
+----+-------------------------+----------+
88-
```
96+
╭─────────────────────────────╮
97+
│ id │ vec │ distance │
98+
├────┼───────────┼────────────┤
99+
│ 1 │ [1,2,3] │ 0 │
100+
│ 2 │ [1,2.2,3] │ 0.20000005 │
101+
│ 3 │ [4,5,6] │ 5.196152 │
102+
╰─────────────────────────────╯
103+
```
104+

docs/en/sql-reference/20-sql-functions/11-vector-functions/02-vector-l1-distance.md

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,52 @@ Formula: `L1_DISTANCE(a, b) = |a1 - b1| + |a2 - b2| + ... + |an - bn|`
3737

3838
```sql
3939
-- Calculate L1 distance between two vectors
40-
SELECT L1_DISTANCE([1.0, 2.0, 3.0], [4.0, 5.0, 6.0]) AS distance;
40+
SELECT L1_DISTANCE([1.0, 2.0, 3.0]::vector(3), [4.0, 5.0, 6.0]::vector(3)) AS distance;
4141
```
4242

4343
Result:
4444
```
45-
──────────
45+
──────────
4646
│ distance │
4747
├──────────┤
48-
9.0
49-
──────────
48+
9
49+
──────────
5050
```
5151

52-
### Using with VECTOR Type
52+
Create a table with vector data:
5353

5454
```sql
55-
-- Create table with VECTOR columns
56-
CREATE TABLE products (
55+
CREATE OR REPLACE TABLE vectors (
5756
id INT,
58-
features VECTOR(3),
59-
VECTOR INDEX idx_features(features) distance='l1'
57+
vec VECTOR(3)
6058
);
6159

62-
INSERT INTO products VALUES
63-
(1, [1.0, 2.0, 3.0]::VECTOR(3)),
64-
(2, [2.0, 3.0, 4.0]::VECTOR(3));
60+
INSERT INTO vectors VALUES
61+
(1, [1.0000, 2.0000, 3.0000]),
62+
(2, [1.0000, 2.2000, 3.0000]),
63+
(3, [4.0000, 5.0000, 6.0000]);
64+
```
65+
66+
Find the vector closest to [1, 2, 3] using L1 distance:
6567

66-
-- Find products similar to a query vector using L1 distance
67-
SELECT
68+
```sql
69+
SELECT
6870
id,
69-
features,
70-
L1_DISTANCE(features, [1.5, 2.5, 3.5]::VECTOR(3)) AS distance
71-
FROM products
72-
ORDER BY distance ASC
73-
LIMIT 5;
71+
vec,
72+
L1_DISTANCE(vec, [1.0000, 2.0000, 3.0000]::VECTOR(3)) AS distance
73+
FROM
74+
vectors
75+
ORDER BY
76+
distance ASC;
7477
```
78+
79+
```
80+
╭─────────────────────────────╮
81+
│ id │ vec │ distance │
82+
├────┼───────────┼────────────┤
83+
│ 1 │ [1,2,3] │ 0 │
84+
│ 2 │ [1,2.2,3] │ 0.20000005 │
85+
│ 3 │ [4,5,6] │ 9 │
86+
╰─────────────────────────────╯
87+
```
88+

docs/en/sql-reference/20-sql-functions/11-vector-functions/index.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,30 @@ This section provides reference information for vector functions in Databend. Th
99

1010
| Function | Description | Example |
1111
|----------|-------------|--------|
12-
| [COSINE_DISTANCE](./00-vector-cosine-distance.md) | Calculates angular distance between vectors (range: 0-1) | `COSINE_DISTANCE([1,2,3], [4,5,6])` |
13-
| [L1_DISTANCE](./02-vector-l1-distance.md) | Calculates Manhattan (L1) distance between vectors | `L1_DISTANCE([1,2,3], [4,5,6])` |
14-
| [L2_DISTANCE](./01-vector-l2-distance.md) | Calculates Euclidean (straight-line) distance | `L2_DISTANCE([1,2,3], [4,5,6])` |
12+
| [COSINE_DISTANCE](./00-vector-cosine-distance.md) | Calculates Cosine distance between vectors (range: 0-1) | `COSINE_DISTANCE([1,2,3]::VECTOR(3), [4,5,6]::VECTOR(3))` |
13+
| [L1_DISTANCE](./02-vector-l1-distance.md) | Calculates Manhattan (L1) distance between vectors | `L1_DISTANCE([1,2,3]::VECTOR(3), [4,5,6]::VECTOR(3))` |
14+
| [L2_DISTANCE](./01-vector-l2-distance.md) | Calculates Euclidean (straight-line) distance | `L2_DISTANCE([1,2,3]::VECTOR(3), [4,5,6]::VECTOR(3))` |
15+
| [INNER_PRODUCT](./03-inner-product.md) | Calculates the inner product (dot product) of two vectors | `INNER_PRODUCT([1,2,3]::VECTOR(3), [4,5,6]::VECTOR(3))` |
1516

1617
## Vector Analysis Functions
1718

1819
| Function | Description | Example |
1920
|----------|-------------|--------|
20-
| [INNER_PRODUCT](./03-inner-product.md) | Calculates the inner product (dot product) of two vectors | `INNER_PRODUCT([1,2,3], [4,5,6])` |
21-
| [VECTOR_NORM](./05-vector-norm.md) | Calculates the L2 norm (magnitude) of a vector | `VECTOR_NORM([1,2,3])` |
22-
| [VECTOR_DIMS](./04-vector-dims.md) | Returns the dimensionality of a vector | `VECTOR_DIMS([1,2,3])` |
21+
| [VECTOR_NORM](./05-vector-norm.md) | Calculates the L2 norm (magnitude) of a vector | `VECTOR_NORM([1,2,3]::VECTOR(3))` |
22+
| [VECTOR_DIMS](./04-vector-dims.md) | Returns the dimensionality of a vector | `VECTOR_DIMS([1,2,3]::VECTOR(3))` |
2323

2424
## Distance Functions Comparison
2525

2626
| Function | Description | Range | Best For | Use Cases |
2727
|----------|-------------|-------|----------|-----------|
28-
| [COSINE_DISTANCE](./00-vector-cosine-distance.md) | Angular distance between vectors | [0, 1] | When direction matters more than magnitude | • Document similarity<br/>• Semantic search<br/>• Recommendation systems<br/>• Text analysis |
28+
| [COSINE_DISTANCE](./00-vector-cosine-distance.md) | Cosine distance between vectors | [0, 1] | When direction matters more than magnitude | • Document similarity<br/>• Semantic search<br/>• Recommendation systems<br/>• Text analysis |
2929
| [L1_DISTANCE](./02-vector-l1-distance.md) | Manhattan (L1) distance between vectors | [0, ∞) | Robust to outliers | • Feature comparison<br/>• Outlier detection<br/>• Grid-based pathfinding<br/>• Clustering algorithms |
30-
| [L2_DISTANCE](./01-vector-l2-distance.md) | Euclidean (straight-line) distance | [0, ∞) | When magnitude matters | • Image similarity<br/>• Geographical data<br/>• Anomaly detection<br/>• Feature-based clustering |
30+
| [L2_DISTANCE](./01-vector-l2-distance.md) | Euclidean (straight-line) distance | [0, ∞) | When magnitude and absolute differences are important | • Image similarity<br/>• Geographical data<br/>• Anomaly detection<br/>• Feature-based clustering |
31+
| [INNER_PRODUCT](./03-inner-product.md) | Dot product of two vectors | (-∞, ∞) | When both magnitude and direction are important | • Neural networks<br/>• Machine learning<br/>• Physics calculations<br/>• Vector projections |
3132

3233
## Vector Analysis Functions Comparison
3334

3435
| Function | Description | Range | Best For | Use Cases |
3536
|----------|-------------|-------|----------|-----------|
36-
| [INNER_PRODUCT](./03-inner-product.md) | Dot product of two vectors | (-∞, ∞) | Measuring vector similarity and projections | • Neural networks<br/>• Machine learning<br/>• Physics calculations<br/>• Vector projections |
3737
| [VECTOR_NORM](./05-vector-norm.md) | L2 norm (magnitude) of a vector | [0, ∞) | Vector normalization and magnitude | • Vector normalization<br/>• Feature scaling<br/>• Magnitude calculations<br/>• Physics applications |
3838
| [VECTOR_DIMS](./04-vector-dims.md) | Number of vector dimensions | [1, 4096] | Vector validation and processing | • Data validation<br/>• Dynamic processing<br/>• Debugging<br/>• Compatibility checks |

0 commit comments

Comments
 (0)