-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
opaque.go
222 lines (211 loc) · 6.28 KB
/
opaque.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
// Copyright 2019 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"
"reflect"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder"
"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/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/errors"
)
type opaqueMetadata struct {
info string
plan planNode
}
var _ opt.OpaqueMetadata = &opaqueMetadata{}
func (o *opaqueMetadata) ImplementsOpaqueMetadata() {}
func (o *opaqueMetadata) String() string { return o.info }
func buildOpaque(
ctx context.Context, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, stmt tree.Statement,
) (opt.OpaqueMetadata, sqlbase.ResultColumns, error) {
p := evalCtx.Planner.(*planner)
// Opaque statements handle their own scalar arguments, with no help from the
// optimizer. As such, they cannot contain subqueries.
scalarProps := &semaCtx.Properties
defer scalarProps.Restore(*scalarProps)
scalarProps.Require(stmt.StatementTag(), tree.RejectSubqueries)
var plan planNode
var err error
switch n := stmt.(type) {
case *tree.AlterIndex:
plan, err = p.AlterIndex(ctx, n)
case *tree.AlterTable:
plan, err = p.AlterTable(ctx, n)
case *tree.AlterRole:
plan, err = p.AlterRole(ctx, n)
case *tree.AlterSequence:
plan, err = p.AlterSequence(ctx, n)
case *tree.CommentOnColumn:
plan, err = p.CommentOnColumn(ctx, n)
case *tree.CommentOnDatabase:
plan, err = p.CommentOnDatabase(ctx, n)
case *tree.CommentOnIndex:
plan, err = p.CommentOnIndex(ctx, n)
case *tree.CommentOnTable:
plan, err = p.CommentOnTable(ctx, n)
case *tree.CreateDatabase:
plan, err = p.CreateDatabase(ctx, n)
case *tree.CreateIndex:
plan, err = p.CreateIndex(ctx, n)
case *tree.CreateSchema:
plan, err = p.CreateSchema(ctx, n)
case *tree.CreateRole:
plan, err = p.CreateRole(ctx, n)
case *tree.CreateSequence:
plan, err = p.CreateSequence(ctx, n)
case *tree.CreateStats:
plan, err = p.CreateStatistics(ctx, n)
case *tree.Deallocate:
plan, err = p.Deallocate(ctx, n)
case *tree.Discard:
plan, err = p.Discard(ctx, n)
case *tree.DropDatabase:
plan, err = p.DropDatabase(ctx, n)
case *tree.DropIndex:
plan, err = p.DropIndex(ctx, n)
case *tree.DropRole:
plan, err = p.DropRole(ctx, n)
case *tree.DropTable:
plan, err = p.DropTable(ctx, n)
case *tree.DropView:
plan, err = p.DropView(ctx, n)
case *tree.DropSequence:
plan, err = p.DropSequence(ctx, n)
case *tree.Grant:
plan, err = p.Grant(ctx, n)
case *tree.GrantRole:
plan, err = p.GrantRole(ctx, n)
case *tree.RenameColumn:
plan, err = p.RenameColumn(ctx, n)
case *tree.RenameDatabase:
plan, err = p.RenameDatabase(ctx, n)
case *tree.RenameIndex:
plan, err = p.RenameIndex(ctx, n)
case *tree.RenameTable:
plan, err = p.RenameTable(ctx, n)
case *tree.Revoke:
plan, err = p.Revoke(ctx, n)
case *tree.RevokeRole:
plan, err = p.RevokeRole(ctx, n)
case *tree.Scatter:
plan, err = p.Scatter(ctx, n)
case *tree.Scrub:
plan, err = p.Scrub(ctx, n)
case *tree.SetClusterSetting:
plan, err = p.SetClusterSetting(ctx, n)
case *tree.SetZoneConfig:
plan, err = p.SetZoneConfig(ctx, n)
case *tree.SetVar:
plan, err = p.SetVar(ctx, n)
case *tree.SetTransaction:
plan, err = p.SetTransaction(n)
case *tree.SetSessionAuthorizationDefault:
plan, err = p.SetSessionAuthorizationDefault()
case *tree.SetSessionCharacteristics:
plan, err = p.SetSessionCharacteristics(n)
case *tree.ShowClusterSetting:
plan, err = p.ShowClusterSetting(ctx, n)
case *tree.ShowHistogram:
plan, err = p.ShowHistogram(ctx, n)
case *tree.ShowTableStats:
plan, err = p.ShowTableStats(ctx, n)
case *tree.ShowTraceForSession:
plan, err = p.ShowTrace(ctx, n)
case *tree.ShowZoneConfig:
plan, err = p.ShowZoneConfig(ctx, n)
case *tree.ShowFingerprints:
plan, err = p.ShowFingerprints(ctx, n)
case *tree.Truncate:
plan, err = p.Truncate(ctx, n)
case tree.CCLOnlyStatement:
plan, err = p.maybePlanHook(ctx, stmt)
if plan == nil && err == nil {
return nil, nil, pgerror.Newf(pgcode.CCLRequired,
"a CCL binary is required to use this statement type: %T", stmt)
}
default:
return nil, nil, errors.AssertionFailedf("unknown opaque statement %T", stmt)
}
if err != nil {
return nil, nil, err
}
res := &opaqueMetadata{
info: stmt.StatementTag(),
plan: plan,
}
return res, planColumns(plan), nil
}
func init() {
for _, stmt := range []tree.Statement{
&tree.AlterIndex{},
&tree.AlterTable{},
&tree.AlterSequence{},
&tree.AlterRole{},
&tree.CommentOnColumn{},
&tree.CommentOnDatabase{},
&tree.CommentOnIndex{},
&tree.CommentOnTable{},
&tree.CreateDatabase{},
&tree.CreateIndex{},
&tree.CreateSchema{},
&tree.CreateSequence{},
&tree.CreateStats{},
&tree.CreateRole{},
&tree.Deallocate{},
&tree.Discard{},
&tree.DropDatabase{},
&tree.DropIndex{},
&tree.DropTable{},
&tree.DropView{},
&tree.DropRole{},
&tree.DropSequence{},
&tree.Grant{},
&tree.GrantRole{},
&tree.RenameColumn{},
&tree.RenameDatabase{},
&tree.RenameIndex{},
&tree.RenameTable{},
&tree.Revoke{},
&tree.RevokeRole{},
&tree.Scatter{},
&tree.Scrub{},
&tree.SetClusterSetting{},
&tree.SetZoneConfig{},
&tree.SetVar{},
&tree.SetTransaction{},
&tree.SetSessionAuthorizationDefault{},
&tree.SetSessionCharacteristics{},
&tree.ShowClusterSetting{},
&tree.ShowHistogram{},
&tree.ShowTableStats{},
&tree.ShowTraceForSession{},
&tree.ShowZoneConfig{},
&tree.ShowFingerprints{},
&tree.Truncate{},
// CCL statements (without Export which has an optimizer operator).
&tree.Backup{},
&tree.ShowBackup{},
&tree.Restore{},
&tree.CreateChangefeed{},
&tree.Import{},
} {
typ := optbuilder.OpaqueReadOnly
if tree.CanModifySchema(stmt) {
typ = optbuilder.OpaqueDDL
} else if tree.CanWriteData(stmt) {
typ = optbuilder.OpaqueMutation
}
optbuilder.RegisterOpaque(reflect.TypeOf(stmt), typ, buildOpaque)
}
}