-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
tsearch.go
84 lines (76 loc) · 2.77 KB
/
tsearch.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
// 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 invertedidx
import (
"context"
"github.com/cockroachdb/cockroach/pkg/sql/inverted"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
"github.com/cockroachdb/cockroach/pkg/sql/opt/invertedexpr"
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/errors"
)
type tsqueryFilterPlanner struct {
tabID opt.TableID
index cat.Index
computedColumns map[opt.ColumnID]opt.ScalarExpr
}
var _ invertedFilterPlanner = &tsqueryFilterPlanner{}
// extractInvertedFilterConditionFromLeaf implements the invertedFilterPlanner
// interface.
func (t *tsqueryFilterPlanner) extractInvertedFilterConditionFromLeaf(
_ context.Context, _ *eval.Context, expr opt.ScalarExpr,
) (
invertedExpr inverted.Expression,
remainingFilters opt.ScalarExpr,
_ *invertedexpr.PreFiltererStateForInvertedFilterer,
) {
var constantVal opt.ScalarExpr
var left, right opt.ScalarExpr
switch e := expr.(type) {
case *memo.TSMatchesExpr:
left, right = e.Left, e.Right
default:
// Only the above types are supported.
return inverted.NonInvertedColExpression{}, expr, nil
}
if isIndexColumn(t.tabID, t.index, left, t.computedColumns) && memo.CanExtractConstDatum(right) {
constantVal = right
} else if isIndexColumn(t.tabID, t.index, right, t.computedColumns) && memo.CanExtractConstDatum(left) {
constantVal = left
} else {
// Can only accelerate with a single constant value.
return inverted.NonInvertedColExpression{}, expr, nil
}
d := memo.ExtractConstDatum(constantVal)
if d.ResolvedType() != types.TSQuery {
panic(errors.AssertionFailedf(
"trying to apply tsvector inverted index to unsupported type %s", d.ResolvedType(),
))
}
q := d.(*tree.DTSQuery).TSQuery
var err error
invertedExpr, err = q.GetInvertedExpr()
if err != nil {
// An inverted expression could not be extracted.
return inverted.NonInvertedColExpression{}, expr, nil
}
// If the extracted inverted expression is not tight then remaining filters
// must be applied after the inverted index scan.
if !invertedExpr.IsTight() {
remainingFilters = expr
}
// We do not currently support pre-filtering for text search indexes, so
// the returned pre-filter state is nil.
return invertedExpr, remainingFilters, nil
}