-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
expr.go
190 lines (166 loc) · 5.48 KB
/
expr.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package distsqlrun
import (
"fmt"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sem/transform"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sem/types"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/pkg/errors"
)
// ivarBinder is a tree.Visitor that binds ordinal references
// (IndexedVars represented by @1, @2, ...) to an IndexedVarContainer.
type ivarBinder struct {
h *tree.IndexedVarHelper
err error
}
func (v *ivarBinder) VisitPre(expr tree.Expr) (recurse bool, newExpr tree.Expr) {
if v.err != nil {
return false, expr
}
if ivar, ok := expr.(*tree.IndexedVar); ok {
newVar, err := v.h.BindIfUnbound(ivar)
if err != nil {
v.err = err
return false, expr
}
return false, newVar
}
return true, expr
}
func (*ivarBinder) VisitPost(expr tree.Expr) tree.Expr { return expr }
// processExpression parses the string expression inside an Expression,
// and associates ordinal references (@1, @2, etc) with the given helper.
func processExpression(
exprSpec Expression,
evalCtx *tree.EvalContext,
semaCtx *tree.SemaContext,
h *tree.IndexedVarHelper,
) (tree.TypedExpr, error) {
if exprSpec.Expr == "" {
return nil, nil
}
expr, err := parser.ParseExpr(exprSpec.Expr)
if err != nil {
return nil, err
}
// Bind IndexedVars to our eh.vars.
v := ivarBinder{h: h, err: nil}
expr, _ = tree.WalkExpr(&v, expr)
if v.err != nil {
return nil, v.err
}
semaCtx.IVarContainer = h.Container()
// Convert to a fully typed expression.
typedExpr, err := tree.TypeCheck(expr, semaCtx, types.Any)
if err != nil {
return nil, errors.Wrap(err, expr.String())
}
// Pre-evaluate constant expressions. This is necessary to avoid repeatedly
// re-evaluating constant values every time the expression is applied.
//
// TODO(solon): It would be preferable to enhance our expression serialization
// format so this wouldn't be necessary.
c := tree.MakeConstantEvalVisitor(evalCtx)
expr, _ = tree.WalkExpr(&c, typedExpr)
if err := c.Err(); err != nil {
return nil, err
}
return expr.(tree.TypedExpr), nil
}
// exprHelper implements the common logic around evaluating an expression that
// depends on a set of values.
type exprHelper struct {
noCopy util.NoCopy
expr tree.TypedExpr
// vars is used to generate IndexedVars that are "backed" by the values in
// `row`.
vars tree.IndexedVarHelper
evalCtx *tree.EvalContext
types []sqlbase.ColumnType
row sqlbase.EncDatumRow
datumAlloc sqlbase.DatumAlloc
}
func (eh *exprHelper) String() string {
if eh.expr == nil {
return "none"
}
return eh.expr.String()
}
// exprHelper implements tree.IndexedVarContainer.
var _ tree.IndexedVarContainer = &exprHelper{}
// IndexedVarResolvedType is part of the tree.IndexedVarContainer interface.
func (eh *exprHelper) IndexedVarResolvedType(idx int) types.T {
return eh.types[idx].ToDatumType()
}
// IndexedVarEval is part of the tree.IndexedVarContainer interface.
func (eh *exprHelper) IndexedVarEval(idx int, ctx *tree.EvalContext) (tree.Datum, error) {
err := eh.row[idx].EnsureDecoded(&eh.types[idx], &eh.datumAlloc)
if err != nil {
return nil, err
}
return eh.row[idx].Datum.Eval(ctx)
}
// IndexedVarNodeFormatter is part of the parser.IndexedVarContainer interface.
func (eh *exprHelper) IndexedVarNodeFormatter(idx int) tree.NodeFormatter {
n := tree.Name(fmt.Sprintf("$%d", idx))
return &n
}
func (eh *exprHelper) init(
expr Expression, types []sqlbase.ColumnType, evalCtx *tree.EvalContext,
) error {
eh.evalCtx = evalCtx
if expr.Expr == "" {
return nil
}
eh.types = types
eh.vars = tree.MakeIndexedVarHelper(eh, len(types))
var err error
semaContext := tree.MakeSemaContext(evalCtx.SessionData.User == security.RootUser)
eh.expr, err = processExpression(expr, evalCtx, &semaContext, &eh.vars)
if err != nil {
return err
}
var t transform.ExprTransformContext
if t.AggregateInExpr(eh.expr, evalCtx.SessionData.SearchPath) {
return errors.Errorf("expression '%s' has aggregate", eh.expr)
}
return nil
}
// evalFilter is used for filter expressions; it evaluates the expression and
// returns whether the filter passes.
func (eh *exprHelper) evalFilter(row sqlbase.EncDatumRow) (bool, error) {
eh.row = row
eh.evalCtx.PushIVarContainer(eh)
pass, err := sqlbase.RunFilter(eh.expr, eh.evalCtx)
eh.evalCtx.PopIVarContainer()
return pass, err
}
// Given a row, eval evaluates the wrapped expression and returns the
// resulting datum. For example, given a row (1, 2, 3, 4, 5):
// '@2' would return '2'
// '@2 + @5' would return '7'
// '@1' would return '1'
// '@2 + 10' would return '12'
func (eh *exprHelper) eval(row sqlbase.EncDatumRow) (tree.Datum, error) {
eh.row = row
eh.evalCtx.PushIVarContainer(eh)
d, err := eh.expr.Eval(eh.evalCtx)
eh.evalCtx.PopIVarContainer()
return d, err
}