Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
124292: sql: implement pgvector datatype and evaluation r=jordanlewis a=jordanlewis

This commit adds the pgvector datatype and associated evaluation operators and functions. It doesn't include index acceleration.

Functionality included:

- `CREATE EXTENSION vector`
- `vector` datatype with optional length, storage and retrieval in non-indexed table columns
- Equality and inequality operators
- `<->` operator - L2 distance
- `<#>` operator - (negative) inner product
- `<=>` operator - cosine distance
- `l1_distance` builtin
- `l2_distance` builtin
- `cosine_distance` builtin
- `inner_product` builtin
- `vector_dims` builtin
- `vector_norm` builtin

Updates cockroachdb#121432
Epic: None

Release note (sql change): implement pgvector encoding, decoding, and operators, without index acceleration.

Co-authored-by: Jordan Lewis <[email protected]>
  • Loading branch information
craig[bot] and jordanlewis committed Jul 6, 2024
2 parents c557fb5 + 3e5a520 commit 9fd3626
Show file tree
Hide file tree
Showing 102 changed files with 2,100 additions and 86 deletions.
12 changes: 11 additions & 1 deletion docs/generated/sql/bnf/stmt_block.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,7 @@ col_name_keyword ::=
| 'VALUES'
| 'VARBIT'
| 'VARCHAR'
| 'VECTOR'
| 'VIRTUAL'
| 'WORK'

Expand Down Expand Up @@ -1755,7 +1756,7 @@ backup_options_list ::=
( backup_options ) ( ( ',' backup_options ) )*

a_expr ::=
( c_expr | '+' a_expr | '-' a_expr | '~' a_expr | 'SQRT' a_expr | 'CBRT' a_expr | qual_op a_expr | 'NOT' a_expr | 'NOT' a_expr | row 'OVERLAPS' row | 'DEFAULT' ) ( ( 'TYPECAST' cast_target | 'TYPEANNOTATE' typename | 'COLLATE' collation_name | 'AT' 'TIME' 'ZONE' a_expr | '+' a_expr | '-' a_expr | '*' a_expr | '/' a_expr | 'FLOORDIV' a_expr | '%' a_expr | '^' a_expr | '#' a_expr | '&' a_expr | '|' a_expr | '<' a_expr | '>' a_expr | '?' a_expr | 'JSON_SOME_EXISTS' a_expr | 'JSON_ALL_EXISTS' a_expr | 'CONTAINS' a_expr | 'CONTAINED_BY' a_expr | '=' a_expr | 'CONCAT' a_expr | 'LSHIFT' a_expr | 'RSHIFT' a_expr | 'FETCHVAL' a_expr | 'FETCHTEXT' a_expr | 'FETCHVAL_PATH' a_expr | 'FETCHTEXT_PATH' a_expr | 'REMOVE_PATH' a_expr | 'INET_CONTAINED_BY_OR_EQUALS' a_expr | 'AND_AND' a_expr | 'AT_AT' a_expr | 'INET_CONTAINS_OR_EQUALS' a_expr | 'LESS_EQUALS' a_expr | 'GREATER_EQUALS' a_expr | 'NOT_EQUALS' a_expr | qual_op a_expr | 'AND' a_expr | 'OR' a_expr | 'LIKE' a_expr | 'LIKE' a_expr 'ESCAPE' a_expr | 'NOT' 'LIKE' a_expr | 'NOT' 'LIKE' a_expr 'ESCAPE' a_expr | 'ILIKE' a_expr | 'ILIKE' a_expr 'ESCAPE' a_expr | 'NOT' 'ILIKE' a_expr | 'NOT' 'ILIKE' a_expr 'ESCAPE' a_expr | 'SIMILAR' 'TO' a_expr | 'SIMILAR' 'TO' a_expr 'ESCAPE' a_expr | 'NOT' 'SIMILAR' 'TO' a_expr | 'NOT' 'SIMILAR' 'TO' a_expr 'ESCAPE' a_expr | '~' a_expr | 'NOT_REGMATCH' a_expr | 'REGIMATCH' a_expr | 'NOT_REGIMATCH' a_expr | 'IS' 'NAN' | 'IS' 'NOT' 'NAN' | 'IS' 'NULL' | 'ISNULL' | 'IS' 'NOT' 'NULL' | 'NOTNULL' | 'IS' 'TRUE' | 'IS' 'NOT' 'TRUE' | 'IS' 'FALSE' | 'IS' 'NOT' 'FALSE' | 'IS' 'UNKNOWN' | 'IS' 'NOT' 'UNKNOWN' | 'IS' 'DISTINCT' 'FROM' a_expr | 'IS' 'NOT' 'DISTINCT' 'FROM' a_expr | 'IS' 'OF' '(' type_list ')' | 'IS' 'NOT' 'OF' '(' type_list ')' | 'BETWEEN' opt_asymmetric b_expr 'AND' a_expr | 'NOT' 'BETWEEN' opt_asymmetric b_expr 'AND' a_expr | 'BETWEEN' 'SYMMETRIC' b_expr 'AND' a_expr | 'NOT' 'BETWEEN' 'SYMMETRIC' b_expr 'AND' a_expr | 'IN' in_expr | 'NOT' 'IN' in_expr | subquery_op sub_type a_expr ) )*
( c_expr | '+' a_expr | '-' a_expr | '~' a_expr | 'SQRT' a_expr | 'CBRT' a_expr | qual_op a_expr | 'NOT' a_expr | 'NOT' a_expr | row 'OVERLAPS' row | 'DEFAULT' ) ( ( 'TYPECAST' cast_target | 'TYPEANNOTATE' typename | 'COLLATE' collation_name | 'AT' 'TIME' 'ZONE' a_expr | '+' a_expr | '-' a_expr | '*' a_expr | '/' a_expr | 'FLOORDIV' a_expr | '%' a_expr | '^' a_expr | '#' a_expr | '&' a_expr | '|' a_expr | '<' a_expr | '>' a_expr | '?' a_expr | 'JSON_SOME_EXISTS' a_expr | 'JSON_ALL_EXISTS' a_expr | 'CONTAINS' a_expr | 'CONTAINED_BY' a_expr | '=' a_expr | 'CONCAT' a_expr | 'LSHIFT' a_expr | 'RSHIFT' a_expr | 'FETCHVAL' a_expr | 'FETCHTEXT' a_expr | 'FETCHVAL_PATH' a_expr | 'FETCHTEXT_PATH' a_expr | 'REMOVE_PATH' a_expr | 'INET_CONTAINED_BY_OR_EQUALS' a_expr | 'AND_AND' a_expr | 'AT_AT' a_expr | 'DISTANCE' a_expr | 'COS_DISTANCE' a_expr | 'NEG_INNER_PRODUCT' a_expr | 'INET_CONTAINS_OR_EQUALS' a_expr | 'LESS_EQUALS' a_expr | 'GREATER_EQUALS' a_expr | 'NOT_EQUALS' a_expr | qual_op a_expr | 'AND' a_expr | 'OR' a_expr | 'LIKE' a_expr | 'LIKE' a_expr 'ESCAPE' a_expr | 'NOT' 'LIKE' a_expr | 'NOT' 'LIKE' a_expr 'ESCAPE' a_expr | 'ILIKE' a_expr | 'ILIKE' a_expr 'ESCAPE' a_expr | 'NOT' 'ILIKE' a_expr | 'NOT' 'ILIKE' a_expr 'ESCAPE' a_expr | 'SIMILAR' 'TO' a_expr | 'SIMILAR' 'TO' a_expr 'ESCAPE' a_expr | 'NOT' 'SIMILAR' 'TO' a_expr | 'NOT' 'SIMILAR' 'TO' a_expr 'ESCAPE' a_expr | '~' a_expr | 'NOT_REGMATCH' a_expr | 'REGIMATCH' a_expr | 'NOT_REGIMATCH' a_expr | 'IS' 'NAN' | 'IS' 'NOT' 'NAN' | 'IS' 'NULL' | 'ISNULL' | 'IS' 'NOT' 'NULL' | 'NOTNULL' | 'IS' 'TRUE' | 'IS' 'NOT' 'TRUE' | 'IS' 'FALSE' | 'IS' 'NOT' 'FALSE' | 'IS' 'UNKNOWN' | 'IS' 'NOT' 'UNKNOWN' | 'IS' 'DISTINCT' 'FROM' a_expr | 'IS' 'NOT' 'DISTINCT' 'FROM' a_expr | 'IS' 'OF' '(' type_list ')' | 'IS' 'NOT' 'OF' '(' type_list ')' | 'BETWEEN' opt_asymmetric b_expr 'AND' a_expr | 'NOT' 'BETWEEN' opt_asymmetric b_expr 'AND' a_expr | 'BETWEEN' 'SYMMETRIC' b_expr 'AND' a_expr | 'NOT' 'BETWEEN' 'SYMMETRIC' b_expr 'AND' a_expr | 'IN' in_expr | 'NOT' 'IN' in_expr | subquery_op sub_type a_expr ) )*

for_schedules_clause ::=
'FOR' 'SCHEDULES' select_stmt
Expand Down Expand Up @@ -3139,6 +3140,9 @@ all_op ::=
| 'NOT_REGIMATCH'
| 'AND_AND'
| 'AT_AT'
| 'DISTANCE'
| 'COS_DISTANCE'
| 'NEG_INNER_PRODUCT'
| '~'
| 'SQRT'
| 'CBRT'
Expand Down Expand Up @@ -3397,6 +3401,7 @@ const_typename ::=
| character_with_length
| const_datetime
| const_geo
| const_vector

interval_type ::=
'INTERVAL'
Expand Down Expand Up @@ -4160,6 +4165,7 @@ bare_label_keywords ::=
| 'VARCHAR'
| 'VARIABLES'
| 'VARIADIC'
| 'VECTOR'
| 'VERIFY_BACKUP_TABLE_DATA'
| 'VIEW'
| 'VIEWACTIVITY'
Expand Down Expand Up @@ -4313,6 +4319,10 @@ const_geo ::=
| 'GEOMETRY' '(' geo_shape_type ',' signed_iconst ')'
| 'GEOGRAPHY' '(' geo_shape_type ',' signed_iconst ')'

const_vector ::=
'VECTOR'
| 'VECTOR' '(' iconst32 ')'

interval_qualifier ::=
'YEAR'
| 'MONTH'
Expand Down
19 changes: 19 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,25 @@ the locality flag on node startup. Returns an error if no region is set.</p>
</span></td><td>Stable</td></tr></tbody>
</table>

### PGVector functions

<table>
<thead><tr><th>Function &rarr; Returns</th><th>Description</th><th>Volatility</th></tr></thead>
<tbody>
<tr><td><a name="cosine_distance"></a><code>cosine_distance(v1: vector, v2: vector) &rarr; <a href="float.html">float</a></code></td><td><span class="funcdesc"><p>Returns the cosine distance between the two vectors.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="inner_product"></a><code>inner_product(v1: vector, v2: vector) &rarr; <a href="float.html">float</a></code></td><td><span class="funcdesc"><p>Returns the inner product between the two vectors.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="l1_distance"></a><code>l1_distance(v1: vector, v2: vector) &rarr; <a href="float.html">float</a></code></td><td><span class="funcdesc"><p>Returns the Manhattan distance between the two vectors.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="l2_distance"></a><code>l2_distance(v1: vector, v2: vector) &rarr; <a href="float.html">float</a></code></td><td><span class="funcdesc"><p>Returns the Euclidean distance between the two vectors.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="vector_dims"></a><code>vector_dims(vector: vector) &rarr; <a href="int.html">int</a></code></td><td><span class="funcdesc"><p>Returns the number of the dimensions in the vector.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="vector_norm"></a><code>vector_norm(vector: vector) &rarr; <a href="float.html">float</a></code></td><td><span class="funcdesc"><p>Returns the Euclidean norm of the vector.</p>
</span></td><td>Immutable</td></tr></tbody>
</table>

### STRING[] functions

<table>
Expand Down
23 changes: 23 additions & 0 deletions docs/generated/sql/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<tr><td><a href="interval.html">interval</a> <code>*</code> <a href="decimal.html">decimal</a></td><td><a href="interval.html">interval</a></td></tr>
<tr><td><a href="interval.html">interval</a> <code>*</code> <a href="float.html">float</a></td><td><a href="interval.html">interval</a></td></tr>
<tr><td><a href="interval.html">interval</a> <code>*</code> <a href="int.html">int</a></td><td><a href="interval.html">interval</a></td></tr>
<tr><td>vector <code>*</code> vector</td><td>vector</td></tr>
</tbody></table>
<table><thead>
<tr><td><code>+</code></td><td>Return</td></tr>
Expand Down Expand Up @@ -89,6 +90,7 @@
<tr><td><a href="timestamp.html">timestamptz</a> <code>+</code> <a href="interval.html">interval</a></td><td><a href="timestamp.html">timestamptz</a></td></tr>
<tr><td>timetz <code>+</code> <a href="date.html">date</a></td><td><a href="timestamp.html">timestamptz</a></td></tr>
<tr><td>timetz <code>+</code> <a href="interval.html">interval</a></td><td>timetz</td></tr>
<tr><td>vector <code>+</code> vector</td><td>vector</td></tr>
</tbody></table>
<table><thead>
<tr><td><code>-</code></td><td>Return</td></tr>
Expand Down Expand Up @@ -123,6 +125,7 @@
<tr><td><a href="timestamp.html">timestamptz</a> <code>-</code> <a href="timestamp.html">timestamp</a></td><td><a href="interval.html">interval</a></td></tr>
<tr><td><a href="timestamp.html">timestamptz</a> <code>-</code> <a href="timestamp.html">timestamptz</a></td><td><a href="interval.html">interval</a></td></tr>
<tr><td>timetz <code>-</code> <a href="interval.html">interval</a></td><td>timetz</td></tr>
<tr><td>vector <code>-</code> vector</td><td>vector</td></tr>
</tbody></table>
<table><thead>
<tr><td><code>-></code></td><td>Return</td></tr>
Expand Down Expand Up @@ -213,6 +216,17 @@
<tr><td><a href="uuid.html">uuid</a> <code><</code> <a href="uuid.html">uuid</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="uuid.html">uuid[]</a> <code><</code> <a href="uuid.html">uuid[]</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td>varbit <code><</code> varbit</td><td><a href="bool.html">bool</a></td></tr>
<tr><td>vector <code><</code> vector</td><td><a href="bool.html">bool</a></td></tr>
</tbody></table>
<table><thead>
<tr><td><code><#></code></td><td>Return</td></tr>
</thead><tbody>
<tr><td>vector <code><#></code> vector</td><td><a href="float.html">float</a></td></tr>
</tbody></table>
<table><thead>
<tr><td><code><-></code></td><td>Return</td></tr>
</thead><tbody>
<tr><td>vector <code><-></code> vector</td><td><a href="float.html">float</a></td></tr>
</tbody></table>
<table><thead>
<tr><td><code><<</code></td><td>Return</td></tr>
Expand Down Expand Up @@ -278,6 +292,12 @@
<tr><td><a href="uuid.html">uuid</a> <code><=</code> <a href="uuid.html">uuid</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="uuid.html">uuid[]</a> <code><=</code> <a href="uuid.html">uuid[]</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td>varbit <code><=</code> varbit</td><td><a href="bool.html">bool</a></td></tr>
<tr><td>vector <code><=</code> vector</td><td><a href="bool.html">bool</a></td></tr>
</tbody></table>
<table><thead>
<tr><td><code><=></code></td><td>Return</td></tr>
</thead><tbody>
<tr><td>vector <code><=></code> vector</td><td><a href="float.html">float</a></td></tr>
</tbody></table>
<table><thead>
<tr><td><code><@</code></td><td>Return</td></tr>
Expand Down Expand Up @@ -344,6 +364,7 @@
<tr><td><a href="uuid.html">uuid</a> <code>=</code> <a href="uuid.html">uuid</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="uuid.html">uuid[]</a> <code>=</code> <a href="uuid.html">uuid[]</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td>varbit <code>=</code> varbit</td><td><a href="bool.html">bool</a></td></tr>
<tr><td>vector <code>=</code> vector</td><td><a href="bool.html">bool</a></td></tr>
</tbody></table>
<table><thead>
<tr><td><code>>></code></td><td>Return</td></tr>
Expand Down Expand Up @@ -412,6 +433,7 @@
<tr><td>tuple <code>IN</code> tuple</td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="uuid.html">uuid</a> <code>IN</code> tuple</td><td><a href="bool.html">bool</a></td></tr>
<tr><td>varbit <code>IN</code> tuple</td><td><a href="bool.html">bool</a></td></tr>
<tr><td>vector <code>IN</code> tuple</td><td><a href="bool.html">bool</a></td></tr>
</tbody></table>
<table><thead>
<tr><td><code>IS NOT DISTINCT FROM</code></td><td>Return</td></tr>
Expand Down Expand Up @@ -475,6 +497,7 @@
<tr><td><a href="uuid.html">uuid</a> <code>IS NOT DISTINCT FROM</code> <a href="uuid.html">uuid</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td><a href="uuid.html">uuid[]</a> <code>IS NOT DISTINCT FROM</code> <a href="uuid.html">uuid[]</a></td><td><a href="bool.html">bool</a></td></tr>
<tr><td>varbit <code>IS NOT DISTINCT FROM</code> varbit</td><td><a href="bool.html">bool</a></td></tr>
<tr><td>vector <code>IS NOT DISTINCT FROM</code> vector</td><td><a href="bool.html">bool</a></td></tr>
<tr><td>void <code>IS NOT DISTINCT FROM</code> unknown</td><td><a href="bool.html">bool</a></td></tr>
</tbody></table>
<table><thead>
Expand Down
5 changes: 5 additions & 0 deletions pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ ALL_TESTS = [
"//pkg/ccl/logictestccl/tests/3node-tenant-multiregion:3node-tenant-multiregion_test",
"//pkg/ccl/logictestccl/tests/3node-tenant:3node-tenant_test",
"//pkg/ccl/logictestccl/tests/5node:5node_test",
"//pkg/ccl/logictestccl/tests/cockroach-go-testserver-23.2:cockroach-go-testserver-23_2_test",
"//pkg/ccl/logictestccl/tests/fakedist-disk:fakedist-disk_test",
"//pkg/ccl/logictestccl/tests/fakedist-vec-off:fakedist-vec-off_test",
"//pkg/ccl/logictestccl/tests/fakedist:fakedist_test",
Expand Down Expand Up @@ -754,6 +755,7 @@ ALL_TESTS = [
"//pkg/util/ulid:ulid_test",
"//pkg/util/unique:unique_test",
"//pkg/util/uuid:uuid_test",
"//pkg/util/vector:vector_test",
"//pkg/util/version:version_test",
"//pkg/util:util_test",
"//pkg/workload/bank:bank_test",
Expand Down Expand Up @@ -899,6 +901,7 @@ GO_TARGETS = [
"//pkg/ccl/logictestccl/tests/3node-tenant-multiregion:3node-tenant-multiregion_test",
"//pkg/ccl/logictestccl/tests/3node-tenant:3node-tenant_test",
"//pkg/ccl/logictestccl/tests/5node:5node_test",
"//pkg/ccl/logictestccl/tests/cockroach-go-testserver-23.2:cockroach-go-testserver-23_2_test",
"//pkg/ccl/logictestccl/tests/fakedist-disk:fakedist-disk_test",
"//pkg/ccl/logictestccl/tests/fakedist-vec-off:fakedist-vec-off_test",
"//pkg/ccl/logictestccl/tests/fakedist:fakedist_test",
Expand Down Expand Up @@ -2604,6 +2607,8 @@ GO_TARGETS = [
"//pkg/util/unique:unique_test",
"//pkg/util/uuid:uuid",
"//pkg/util/uuid:uuid_test",
"//pkg/util/vector:vector",
"//pkg/util/vector:vector_test",
"//pkg/util/version:version",
"//pkg/util/version:version_test",
"//pkg/util:util",
Expand Down
3 changes: 3 additions & 0 deletions pkg/ccl/changefeedccl/avro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ func TestAvroSchema(t *testing.T) {
case types.AnyFamily, types.OidFamily, types.TupleFamily:
// These aren't expected to be needed for changefeeds.
return true
case types.PGVectorFamily:
// We don't support PGVector in Avro yet.
return true
case types.ArrayFamily:
if !randgen.IsAllowedForArray(typ.ArrayContents()) {
return true
Expand Down
2 changes: 1 addition & 1 deletion pkg/ccl/changefeedccl/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ func TestJsonRountrip(t *testing.T) {
switch typ {
case types.Jsonb:
// Unsupported by sql/catalog/colinfo
case types.TSQuery, types.TSVector:
case types.TSQuery, types.TSVector, types.PGVector:
// Unsupported by pkg/sql/parser
default:
if arrayTyp.InternalType.ArrayContents == typ {
Expand Down
63 changes: 63 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/mixed_version_pgvector
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# LogicTest: cockroach-go-testserver-23.2

# Verify that all nodes are running the previous version.

query T nodeidx=0
SELECT crdb_internal.node_executable_version()
----
23.2

query T nodeidx=1
SELECT crdb_internal.node_executable_version()
----
23.2

query T nodeidx=2
SELECT crdb_internal.node_executable_version()
----
23.2

statement error syntax error
CREATE TABLE t (v VECTOR(1))

# Upgrade one node to 24.2

upgrade 0

# Verify that node index 0 is now running 24.2 binary.

query T nodeidx=0
SELECT crdb_internal.release_series(crdb_internal.node_executable_version())
----
24.2

statement error pg_vector not supported until version 24.2
CREATE TABLE t (v VECTOR(1))

upgrade 1

upgrade 2

statement ok
SET CLUSTER SETTING version = crdb_internal.node_executable_version();

query T nodeidx=1
SELECT crdb_internal.release_series(crdb_internal.node_executable_version())
----
24.2

query T nodeidx=2
SELECT crdb_internal.release_series(crdb_internal.node_executable_version())
----
24.2

query B retry
SELECT crdb_internal.is_at_least_version('24.1-02')
----
true

# Note: the following statement would succeed if there cluster had an enterprise
# license, but the mixed version logic framework doesn't support adding one.
# This is tested normally in the vector ccl logic test.
statement error pgcode XXC02 use of vector datatype requires an enterprise license
CREATE TABLE t (v VECTOR(1))
103 changes: 103 additions & 0 deletions pkg/ccl/logictestccl/testdata/logic_test/vector
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# LogicTest: !local-mixed-23.2

query F
SELECT '[1,2,3]'::vector <-> '[4,5,6]'::vector
----
5.196152422706632

statement error pgcode 42601 dimensions for type vector must be at least 1
CREATE TABLE v (v vector(0))

statement error pgcode 42601 dimensions for type vector cannot exceed 16000
CREATE TABLE v (v vector(16001))

statement error column v is of type vector and thus is not indexable
CREATE TABLE v (v vector(2) PRIMARY KEY)

statement ok
CREATE TABLE v (v vector);
CREATE TABLE v2 (v vector(2))

statement ok
INSERT INTO v VALUES('[1]'), ('[2,3]')

query T rowsort
SELECT * FROM v
----
[1]
[2,3]

query T
SELECT * FROM v WHERE v = '[1,2]'
----

query error pgcode 22000 different vector dimensions 2 and 1
SELECT l2_distance('[1,2]', '[1]')

statement error pgcode 22000 expected 2 dimensions, not 1
INSERT INTO v2 VALUES('[1]'), ('[2,3]')

statement ok
INSERT INTO v2 VALUES('[1,2]'), ('[3,4]')

query T rowsort
SELECT * FROM v2
----
[1,2]
[3,4]

query T
SELECT * FROM v2 WHERE v = '[1,2]'
----
[1,2]

query TT
SELECT '[1,2]'::text::vector, ARRAY[1,2]::vector
----
[1,2] [1,2]

query error pgcode 22004 array must not contain nulls
SELECT ARRAY[1,2,null]::vector

query error pgcode 22000 expected 1 dimensions, not 2
select '[3,1]'::vector(1)

query error pgcode 22000 NaN not allowed in vector
select '[3,NaN]'::vector

query error pgcode 22000 infinite value not allowed in vector
select '[3,Inf]'::vector

query error pgcode 22000 infinite value not allowed in vector
select '[3,-Inf]'::vector

statement ok
CREATE TABLE x (a float[], b real[])

# Test implicit cast from vector to array.
statement ok
INSERT INTO x VALUES('[1,2]'::vector, '[3,4]'::vector)

statement ok
CREATE TABLE v3 (v1 vector(1), v2 vector(1));
INSERT INTO v3 VALUES
('[1]', '[2]'),
('[1]', '[-2]'),
(NULL, '[1]'),
('[1]', NULL)

query FFFTTT rowsort
SELECT v1<->v2, v1<#>v2, v1<=>v2, v1+v2, v1-v2, v1*v2 FROM v3
----
1 -2 0 [3] [-1] [2]
3 2 2 [-1] [3] [-2]
NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL

query FFFFFI rowsort
SELECT l1_distance(v1,v2), l2_distance(v1,v2), cosine_distance(v1,v2), inner_product(v1,v2), vector_norm(v1), vector_dims(v1) FROM v3
----
1 1 0 2 1 1
3 3 2 -2 1 1
NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL 1 1
7 changes: 7 additions & 0 deletions pkg/ccl/logictestccl/tests/3node-tenant/generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9fd3626

Please sign in to comment.