-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtsearch_builtins.go
112 lines (107 loc) · 4.23 KB
/
tsearch_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
// Copyright 2022 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/tsearch"
)
func init() {
for k, v := range tsearchBuiltins {
v.props.Category = builtinconstants.CategoryFullTextSearch
v.props.AvailableOnPublicSchema = true
registerBuiltin(k, v)
}
}
var tsearchBuiltins = map[string]builtinDefinition{
// Full text search functions.
"to_tsvector": makeBuiltin(
tree.FunctionProperties{},
tree.Overload{
Types: tree.ParamTypes{{"config", types.String}, {"text", types.String}},
ReturnType: tree.FixedReturnType(types.TSVector),
Fn: func(_ context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
// Parse, stem, and stopword the input.
config := string(tree.MustBeDString(args[0]))
document := string(tree.MustBeDString(args[1]))
vector, err := tsearch.DocumentToTSVector(config, document)
if err != nil {
return nil, err
}
return &tree.DTSVector{TSVector: vector}, nil
},
Info: "Converts text to a tsvector, normalizing words according to the specified or default configuration. " +
"Position information is included in the result.",
Volatility: volatility.Immutable,
},
),
"to_tsquery": makeBuiltin(
tree.FunctionProperties{},
tree.Overload{
Types: tree.ParamTypes{{"config", types.String}, {"text", types.String}},
ReturnType: tree.FixedReturnType(types.TSQuery),
Fn: func(_ context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
config := string(tree.MustBeDString(args[0]))
input := string(tree.MustBeDString(args[1]))
query, err := tsearch.ToTSQuery(config, input)
if err != nil {
return nil, err
}
return &tree.DTSQuery{TSQuery: query}, nil
},
Info: "Converts the input text into a tsquery by normalizing each word in the input according to " +
"the specified or default configuration. The input must already be formatted like a tsquery, in other words, " +
"subsequent tokens must be connected by a tsquery operator (&, |, <->, !).",
Volatility: volatility.Immutable,
},
),
"plainto_tsquery": makeBuiltin(
tree.FunctionProperties{},
tree.Overload{
Types: tree.ParamTypes{{"config", types.String}, {"text", types.String}},
ReturnType: tree.FixedReturnType(types.TSQuery),
Fn: func(_ context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
config := string(tree.MustBeDString(args[0]))
input := string(tree.MustBeDString(args[1]))
query, err := tsearch.PlainToTSQuery(config, input)
if err != nil {
return nil, err
}
return &tree.DTSQuery{TSQuery: query}, nil
},
Info: "Converts text to a tsquery, normalizing words according to the specified or default configuration." +
" The & operator inserted between each token in the input.",
Volatility: volatility.Immutable,
},
),
"phraseto_tsquery": makeBuiltin(
tree.FunctionProperties{},
tree.Overload{
Types: tree.ParamTypes{{"config", types.String}, {"text", types.String}},
ReturnType: tree.FixedReturnType(types.TSQuery),
Fn: func(_ context.Context, evalCtx *eval.Context, args tree.Datums) (tree.Datum, error) {
config := string(tree.MustBeDString(args[0]))
input := string(tree.MustBeDString(args[1]))
query, err := tsearch.PhraseToTSQuery(config, input)
if err != nil {
return nil, err
}
return &tree.DTSQuery{TSQuery: query}, nil
},
Info: "Converts text to a tsquery, normalizing words according to the specified or default configuration." +
" The <-> operator inserted between each token in the input.",
Volatility: volatility.Immutable,
},
),
}