-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
pgvector_builtins.go
142 lines (137 loc) · 4.6 KB
/
pgvector_builtins.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2024 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package builtins
import (
"context"
"github.com/cockroachdb/cockroach/pkg/sql/sem/builtins/builtinconstants"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sem/volatility"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/vector"
)
func init() {
for k, v := range pgvectorBuiltins {
v.props.Category = builtinconstants.CategoryPGVector
v.props.AvailableOnPublicSchema = true
const enforceClass = true
registerBuiltin(k, v, tree.NormalClass, enforceClass)
}
}
var pgvectorBuiltins = map[string]builtinDefinition{
"cosine_distance": makeBuiltin(defProps(),
tree.Overload{
Types: tree.ParamTypes{
{Name: "v1", Typ: types.PGVector},
{Name: "v2", Typ: types.PGVector},
},
ReturnType: tree.FixedReturnType(types.Float),
Fn: func(_ context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
v1 := tree.MustBeDPGVector(args[0])
v2 := tree.MustBeDPGVector(args[1])
distance, err := vector.CosDistance(v1.T, v2.T)
if err != nil {
return nil, err
}
return tree.NewDFloat(tree.DFloat(distance)), nil
},
Info: "Returns the cosine distance between the two vectors.",
Volatility: volatility.Immutable,
},
),
"inner_product": makeBuiltin(defProps(),
tree.Overload{
Types: tree.ParamTypes{
{Name: "v1", Typ: types.PGVector},
{Name: "v2", Typ: types.PGVector},
},
ReturnType: tree.FixedReturnType(types.Float),
Fn: func(_ context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
v1 := tree.MustBeDPGVector(args[0])
v2 := tree.MustBeDPGVector(args[1])
distance, err := vector.InnerProduct(v1.T, v2.T)
if err != nil {
return nil, err
}
return tree.NewDFloat(tree.DFloat(distance)), nil
},
Info: "Returns the inner product between the two vectors.",
Volatility: volatility.Immutable,
},
),
"l1_distance": makeBuiltin(defProps(),
tree.Overload{
Types: tree.ParamTypes{
{Name: "v1", Typ: types.PGVector},
{Name: "v2", Typ: types.PGVector},
},
ReturnType: tree.FixedReturnType(types.Float),
Fn: func(_ context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
v1 := tree.MustBeDPGVector(args[0])
v2 := tree.MustBeDPGVector(args[1])
distance, err := vector.L1Distance(v1.T, v2.T)
if err != nil {
return nil, err
}
return tree.NewDFloat(tree.DFloat(distance)), nil
},
Info: "Returns the Manhattan distance between the two vectors.",
Volatility: volatility.Immutable,
},
),
"l2_distance": makeBuiltin(defProps(),
tree.Overload{
Types: tree.ParamTypes{
{Name: "v1", Typ: types.PGVector},
{Name: "v2", Typ: types.PGVector},
},
ReturnType: tree.FixedReturnType(types.Float),
Fn: func(_ context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
v1 := tree.MustBeDPGVector(args[0])
v2 := tree.MustBeDPGVector(args[1])
distance, err := vector.L2Distance(v1.T, v2.T)
if err != nil {
return nil, err
}
return tree.NewDFloat(tree.DFloat(distance)), nil
},
Info: "Returns the Euclidean distance between the two vectors.",
Volatility: volatility.Immutable,
},
),
"vector_dims": makeBuiltin(defProps(),
tree.Overload{
Types: tree.ParamTypes{
{Name: "vector", Typ: types.PGVector},
},
ReturnType: tree.FixedReturnType(types.Int),
Fn: func(_ context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
v1 := tree.MustBeDPGVector(args[0])
return tree.NewDInt(tree.DInt(len(v1.T))), nil
},
Info: "Returns the number of the dimensions in the vector.",
Volatility: volatility.Immutable,
},
),
"vector_norm": makeBuiltin(defProps(),
tree.Overload{
Types: tree.ParamTypes{
{Name: "vector", Typ: types.PGVector},
},
ReturnType: tree.FixedReturnType(types.Float),
Fn: func(_ context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
v1 := tree.MustBeDPGVector(args[0])
return tree.NewDFloat(tree.DFloat(vector.Norm(v1.T))), nil
},
Info: "Returns the Euclidean norm of the vector.",
Volatility: volatility.Immutable,
},
),
}