-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathroutine.go
232 lines (204 loc) · 6.41 KB
/
routine.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// 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 sql
import (
"context"
"strconv"
"github.com/cockroachdb/cockroach/pkg/kv"
"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/cockroach/pkg/util/tracing"
)
// EvalRoutineExpr returns the result of evaluating the routine. It calls the
// routine's ForEachPlan closure to generate a plan for each statement in the
// routine, then runs the plans. The resulting value of the last statement in
// the routine is returned.
func (p *planner) EvalRoutineExpr(
ctx context.Context, expr *tree.RoutineExpr, args tree.Datums,
) (result tree.Datum, err error) {
// Return the cached result if it exists.
if expr.CachedResult != nil {
return expr.CachedResult, nil
}
var g routineGenerator
g.init(p, expr, args)
defer g.Close(ctx)
err = g.Start(ctx, p.Txn())
if err != nil {
return nil, err
}
hasNext, err := g.Next(ctx)
if err != nil {
return nil, err
}
var res tree.Datum
if !hasNext {
// The result is NULL if no rows were returned by the last statement in
// the routine.
res = tree.DNull
} else {
// The result is the first and only column in the row returned by the
// last statement in the routine.
row, err := g.Values()
if err != nil {
return nil, err
}
res = row[0]
}
if len(args) == 0 && !expr.EnableStepping {
// Cache the result if there are zero arguments and stepping is
// disabled.
expr.CachedResult = res
}
return res, nil
}
// RoutineExprGenerator returns an eval.ValueGenerator that produces the results
// of a routine.
func (p *planner) RoutineExprGenerator(
ctx context.Context, expr *tree.RoutineExpr, args tree.Datums,
) eval.ValueGenerator {
var g routineGenerator
g.init(p, expr, args)
return &g
}
// routineGenerator is an eval.ValueGenerator that produces the result of a
// routine.
type routineGenerator struct {
p *planner
expr *tree.RoutineExpr
args tree.Datums
rch rowContainerHelper
rci *rowContainerIterator
currVals tree.Datums
}
var _ eval.ValueGenerator = &routineGenerator{}
// init initializes a routineGenerator.
func (g *routineGenerator) init(p *planner, expr *tree.RoutineExpr, args tree.Datums) {
*g = routineGenerator{
p: p,
expr: expr,
args: args,
}
}
// ResolvedType is part of the ValueGenerator interface.
func (g *routineGenerator) ResolvedType() *types.T {
return g.expr.ResolvedType()
}
// Start is part of the ValueGenerator interface.
// TODO(mgartner): We can cache results for future invocations of the routine by
// creating a new iterator over an existing row container helper if the routine
// is cache-able (i.e., there are no arguments to the routine and stepping is
// disabled).
func (g *routineGenerator) Start(ctx context.Context, txn *kv.Txn) (err error) {
rt := g.expr.ResolvedType()
var retTypes []*types.T
if g.expr.MultiColOutput {
// A routine with multiple output column has its types in a tuple.
retTypes = make([]*types.T, len(rt.TupleContents()))
for i, c := range rt.TupleContents() {
retTypes[i] = c
}
} else {
retTypes = []*types.T{g.expr.ResolvedType()}
}
g.rch.Init(ctx, retTypes, g.p.ExtendedEvalContext(), "routine" /* opName */)
// Configure stepping for volatile routines so that mutations made by the
// invoking statement are visible to the routine.
if g.expr.EnableStepping {
prevSteppingMode := txn.ConfigureStepping(ctx, kv.SteppingEnabled)
prevSeqNum := txn.GetLeafTxnInputState(ctx).ReadSeqNum
defer func() {
// If the routine errored, the transaction should be aborted, so
// there is no need to reconfigure stepping or revert to the
// original sequence number.
if err == nil {
_ = txn.ConfigureStepping(ctx, prevSteppingMode)
err = txn.SetReadSeqNum(prevSeqNum)
}
}()
}
// Execute each statement in the routine sequentially.
stmtIdx := 0
ef := newExecFactory(ctx, g.p)
rrw := NewRowResultWriter(&g.rch)
err = g.expr.ForEachPlan(ctx, ef, g.args, func(plan tree.RoutinePlan, isFinalPlan bool) error {
stmtIdx++
opName := "udf-stmt-" + g.expr.Name + "-" + strconv.Itoa(stmtIdx)
ctx, sp := tracing.ChildSpan(ctx, opName)
defer sp.Finish()
// If this is the last statement, use the rowResultWriter created above.
// Otherwise, use a rowResultWriter that drops all rows added to it.
var w rowResultWriter
if isFinalPlan {
w = rrw
} else {
w = &droppingResultWriter{}
}
// Place a sequence point before each statement in the routine for
// volatile functions.
if g.expr.EnableStepping {
if err := txn.Step(ctx); err != nil {
return err
}
}
// Run the plan.
err = runPlanInsidePlan(ctx, g.p.RunParams(ctx), plan.(*planComponents), w)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
g.rci = newRowContainerIterator(ctx, g.rch)
return nil
}
// Next is part of the ValueGenerator interface.
func (g *routineGenerator) Next(ctx context.Context) (bool, error) {
var err error
g.currVals, err = g.rci.Next()
if err != nil {
return false, err
}
return g.currVals != nil, nil
}
// Values is part of the ValueGenerator interface.
func (g *routineGenerator) Values() (tree.Datums, error) {
return g.currVals, nil
}
// Close is part of the ValueGenerator interface.
func (g *routineGenerator) Close(ctx context.Context) {
if g.rci != nil {
g.rci.Close()
g.rci = nil
}
g.rch.Close(ctx)
}
// droppingResultWriter drops all rows that are added to it. It only tracks
// errors with the SetError and Err functions.
type droppingResultWriter struct {
err error
}
// AddRow is part of the rowResultWriter interface.
func (d *droppingResultWriter) AddRow(ctx context.Context, row tree.Datums) error {
return nil
}
// IncrementRowsAffected is part of the rowResultWriter interface.
func (d *droppingResultWriter) IncrementRowsAffected(ctx context.Context, n int) {}
// SetError is part of the rowResultWriter interface.
func (d *droppingResultWriter) SetError(err error) {
d.err = err
}
// Err is part of the rowResultWriter interface.
func (d *droppingResultWriter) Err() error {
return d.err
}