-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
builder.go
179 lines (155 loc) · 5.78 KB
/
builder.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
// Copyright 2018 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 execbuilder
import (
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
"github.com/cockroachdb/cockroach/pkg/sql/opt/exec"
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util/errorutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)
// Builder constructs a tree of execution nodes (exec.Node) from an optimized
// expression tree (opt.Expr).
type Builder struct {
factory exec.Factory
mem *memo.Memo
catalog cat.Catalog
e opt.Expr
disableTelemetry bool
evalCtx *tree.EvalContext
fastIsConstVisitor fastIsConstVisitor
// subqueries accumulates information about subqueries that are part of scalar
// expressions we built. Each entry is associated with a tree.Subquery
// expression node.
subqueries []exec.Subquery
// postqueries accumulates check queries that are run after the main query.
postqueries []exec.Node
// nullifyMissingVarExprs, if greater than 0, tells the builder to replace
// VariableExprs that have no bindings with DNull. This is useful for apply
// join, which needs to be able to create a plan that has outer columns.
// The number indicates the depth of apply joins.
nullifyMissingVarExprs int
// nameGen is used to generate names for the tables that will be created for
// each relational subexpression when evalCtx.SessionData.SaveTablesPrefix is
// non-empty.
nameGen *memo.ExprNameGenerator
// withExprs is the set of With expressions which may be referenced elsewhere
// in the query.
// TODO(justin): set this up so that we can look them up by index lookups
// rather than scans.
withExprs []builtWithExpr
// allowAutoCommit is passed through to factory methods for mutation
// operators. It allows execution to commit the transaction as part of the
// mutation itself. See canAutoCommit().
allowAutoCommit bool
allowInsertFastPath bool
// forceForUpdateLocking is conditionally passed through to factory methods
// for scan operators that serve as the input for mutation operators. When
// set to true, it ensures that a FOR UPDATE row-level locking mode is used
// by scans. See forUpdateLocking.
forceForUpdateLocking bool
// -- output --
// IsDDL is set to true if the statement contains DDL.
IsDDL bool
}
// New constructs an instance of the execution node builder using the
// given factory to construct nodes. The Build method will build the execution
// node tree from the given optimized expression tree.
//
// catalog is only needed if the statement contains an EXPLAIN (OPT, CATALOG).
func New(
factory exec.Factory, mem *memo.Memo, catalog cat.Catalog, e opt.Expr, evalCtx *tree.EvalContext,
) *Builder {
b := &Builder{
factory: factory,
mem: mem,
catalog: catalog,
e: e,
evalCtx: evalCtx,
}
if evalCtx != nil {
if evalCtx.SessionData.SaveTablesPrefix != "" {
b.nameGen = memo.NewExprNameGenerator(evalCtx.SessionData.SaveTablesPrefix)
}
b.allowInsertFastPath = evalCtx.SessionData.InsertFastPath
}
return b
}
// DisableTelemetry prevents the execbuilder from updating telemetry counters.
func (b *Builder) DisableTelemetry() {
b.disableTelemetry = true
}
// Build constructs the execution node tree and returns its root node if no
// error occurred.
func (b *Builder) Build() (_ exec.Plan, err error) {
plan, err := b.build(b.e)
if err != nil {
return nil, err
}
return b.factory.ConstructPlan(plan.root, b.subqueries, b.postqueries)
}
func (b *Builder) build(e opt.Expr) (_ execPlan, err error) {
defer func() {
if r := recover(); r != nil {
// This code allows us to propagate errors without adding lots of checks
// for `if err != nil` throughout the construction code. This is only
// possible because the code does not update shared state and does not
// manipulate locks.
if ok, e := errorutil.ShouldCatch(r); ok {
err = e
} else {
panic(r)
}
}
}()
rel, ok := e.(memo.RelExpr)
if !ok {
return execPlan{}, errors.AssertionFailedf(
"building execution for non-relational operator %s", log.Safe(e.Op()),
)
}
b.allowAutoCommit = b.canAutoCommit(rel)
return b.buildRelational(rel)
}
// BuildScalar converts a scalar expression to a TypedExpr. Variables are mapped
// according to the IndexedVarHelper.
func (b *Builder) BuildScalar(ivh *tree.IndexedVarHelper) (tree.TypedExpr, error) {
scalar, ok := b.e.(opt.ScalarExpr)
if !ok {
return nil, errors.AssertionFailedf("BuildScalar cannot be called for non-scalar operator %s", log.Safe(b.e.Op()))
}
ctx := buildScalarCtx{ivh: *ivh}
for i := 0; i < ivh.NumVars(); i++ {
ctx.ivarMap.Set(i+1, i)
}
return b.buildScalar(&ctx, scalar)
}
func (b *Builder) decorrelationError() error {
return errors.Errorf("could not decorrelate subquery")
}
// builtWithExpr is metadata regarding a With expression which has already been
// added to the set of subqueries for the query.
type builtWithExpr struct {
id opt.WithID
// outputCols maps the output ColumnIDs of the With expression to the ordinal
// positions they are output to. See execPlan.outputCols for more details.
outputCols opt.ColMap
bufferNode exec.Node
}
func (b *Builder) addBuiltWithExpr(id opt.WithID, outputCols opt.ColMap, bufferNode exec.Node) {
b.withExprs = append(b.withExprs, builtWithExpr{
id: id,
outputCols: outputCols,
bufferNode: bufferNode,
})
}