-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
constraint.go
50 lines (44 loc) · 1.88 KB
/
constraint.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
// Copyright 2022 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
package cdceval
import (
"context"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/errors"
)
// ConstrainPrimaryIndexSpanByFilter attempts to constrain table primary index span via specified
// filter. Returns constrained span, and a possibly empty remaining filter that needs to be evaluated.
func ConstrainPrimaryIndexSpanByFilter(
ctx context.Context, execCtx sql.JobExecContext, filterStr string, descr catalog.TableDescriptor,
) ([]roachpb.Span, string, error) {
if filterStr == "" {
return nil, "", errors.AssertionFailedf("unexpected empty filter")
}
filterExpr, err := parser.ParseExpr(filterStr)
if err != nil {
return nil, "", pgerror.Wrapf(err, pgcode.InvalidParameterValue,
"filter expression %q must be a valid SQL expression", filterStr)
}
semaCtx := newSemaCtx()
tableName := tree.NewUnqualifiedTableName(tree.Name(descr.GetName()))
spans, remainingFilter, err := execCtx.ConstrainPrimaryIndexSpanByExpr(
ctx, sql.BestEffortConstrain, tableName, descr, &execCtx.ExtendedEvalContext().Context, semaCtx, filterExpr)
if err != nil {
return nil, "", err
}
if remainingFilter == tree.DBoolTrue {
return spans, "", nil
}
return spans, tree.AsStringWithFQNames(remainingFilter, &semaCtx.Annotations), nil
}