-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
delegate.go
279 lines (222 loc) · 7.92 KB
/
delegate.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// 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 delegate
import (
"context"
"github.com/cockroachdb/cockroach/pkg/sql/deprecatedshowranges"
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
"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/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
)
// Certain statements (most SHOW variants) are just syntactic sugar for a more
// complicated underlying query.
//
// This package contains the logic to convert the AST of such a statement to the
// AST of the equivalent query to be planned.
// TryDelegate takes a statement and checks if it is one of the statements that
// can be rewritten as a lower level query. If it can, returns a new AST which
// is equivalent to the original statement. Otherwise, returns nil.
func TryDelegate(
ctx context.Context,
catalog cat.Catalog,
evalCtx *eval.Context,
stmt tree.Statement,
qualifyDataSourceNamesInAST bool,
) (tree.Statement, error) {
d := delegator{
ctx: ctx,
catalog: catalog,
evalCtx: evalCtx,
qualifyDataSourceNamesInAST: qualifyDataSourceNamesInAST,
}
switch t := stmt.(type) {
case *tree.ShowClusterSettingList:
return d.delegateShowClusterSettingList(t)
case *tree.ShowTenantClusterSettingList:
return d.delegateShowTenantClusterSettingList(t)
case *tree.ShowDatabases:
return d.delegateShowDatabases(t)
case *tree.ShowEnums:
return d.delegateShowEnums(t)
case *tree.ShowTypes:
return d.delegateShowTypes()
case *tree.ShowCreate:
return d.delegateShowCreate(t)
case *tree.ShowCreateFunction:
return d.delegateShowCreateFunction(t)
case *tree.ShowCreateAllSchemas:
return d.delegateShowCreateAllSchemas()
case *tree.ShowCreateAllTables:
return d.delegateShowCreateAllTables()
case *tree.ShowCreateAllTypes:
return d.delegateShowCreateAllTypes()
case *tree.ShowDatabaseIndexes:
return d.delegateShowDatabaseIndexes(t)
case *tree.ShowIndexes:
return d.delegateShowIndexes(t)
case *tree.ShowColumns:
return d.delegateShowColumns(t)
case *tree.ShowConstraints:
return d.delegateShowConstraints(t)
case *tree.ShowPartitions:
return d.delegateShowPartitions(t)
case *tree.ShowGrants:
return d.delegateShowGrants(t)
case *tree.ShowJobs:
return d.delegateShowJobs(t)
case *tree.ShowChangefeedJobs:
return d.delegateShowChangefeedJobs(t)
case *tree.ShowQueries:
return d.delegateShowQueries(t)
case *tree.ShowRanges:
// Remove in v23.2.
//
// This chooses a different implementation of the SHOW statement
// depending on a run-time config setting.
if deprecatedshowranges.EnableDeprecatedBehavior(ctx, evalCtx.Settings, evalCtx.ClientNoticeSender) {
return d.delegateShowRangesDEPRECATED(t)
}
return d.delegateShowRanges(t)
case *tree.ShowRangeForRow:
return d.delegateShowRangeForRow(t)
case *tree.ShowSurvivalGoal:
return d.delegateShowSurvivalGoal(t)
case *tree.ShowRegions:
return d.delegateShowRegions(t)
case *tree.ShowRoleGrants:
return d.delegateShowRoleGrants(t)
case *tree.ShowRoles:
return d.delegateShowRoles()
case *tree.ShowSchemas:
return d.delegateShowSchemas(t)
case *tree.ShowSequences:
return d.delegateShowSequences(t)
case *tree.ShowSessions:
return d.delegateShowSessions(t)
case *tree.ShowSyntax:
return d.delegateShowSyntax(t)
case *tree.ShowFunctions:
return d.delegateShowFunctions(t)
case *tree.ShowTables:
return d.delegateShowTables(t)
case *tree.ShowTransactions:
return d.delegateShowTransactions(t)
case *tree.ShowUsers:
return d.delegateShowRoles()
case *tree.ShowVar:
return d.delegateShowVar(t)
case *tree.ShowZoneConfig:
return d.delegateShowZoneConfig(t)
case *tree.ShowSchedules:
return d.delegateShowSchedules(t)
case *tree.ControlJobsForSchedules:
return d.delegateJobControl(ControlJobsDelegate{
Schedules: t.Schedules,
Command: t.Command,
})
case *tree.ControlJobsOfType:
return d.delegateJobControl(ControlJobsDelegate{
Type: t.Type,
Command: t.Command,
})
case *tree.ShowFullTableScans:
return d.delegateShowFullTableScans()
case *tree.ShowDefaultPrivileges:
return d.delegateShowDefaultPrivileges(t)
case *tree.ShowLastQueryStatistics:
return nil, unimplemented.New(
"show last query statistics",
"cannot use SHOW LAST QUERY STATISTICS as a statement source",
)
case *tree.ShowSavepointStatus:
return nil, unimplemented.NewWithIssue(
47333, "cannot use SHOW SAVEPOINT STATUS as a statement source")
// SHOW TRANSFER STATE cannot be rewritten as a low-level query due to
// the format of its output (e.g. transfer key echoed back, and errors are
// returned in the form of a SQL value).
case *tree.ShowTransferState:
return nil, pgerror.Newf(pgcode.FeatureNotSupported,
"cannot use SHOW TRANSFER STATE as a statement source")
default:
return nil, nil
}
}
type delegator struct {
ctx context.Context
catalog cat.Catalog
evalCtx *eval.Context
qualifyDataSourceNamesInAST bool
}
func (d *delegator) parse(sql string) (tree.Statement, error) {
s, err := parser.ParseOne(sql)
if err != nil {
return s.AST, err
}
d.evalCtx.Planner.MaybeReallocateAnnotations(s.NumAnnotations)
return s.AST, err
}
// We avoid the cache so that we can observe the details without
// taking a lease, like other SHOW commands.
var resolveFlags = cat.Flags{AvoidDescriptorCaches: true, NoTableStats: true}
// resolveAndModifyUnresolvedObjectName may modify the name input
// if d.qualifyDataSourceNamesInAST == true
func (d *delegator) resolveAndModifyUnresolvedObjectName(
name *tree.UnresolvedObjectName,
) (cat.DataSource, cat.DataSourceName, error) {
tn := name.ToTableName()
dataSource, resName, err := d.catalog.ResolveDataSource(d.ctx, resolveFlags, &tn)
if err != nil {
return nil, cat.DataSourceName{}, err
}
if err := d.catalog.CheckAnyPrivilege(d.ctx, dataSource); err != nil {
return nil, cat.DataSourceName{}, err
}
// Use qualifyDataSourceNamesInAST similarly to the Builder so that
// CREATE TABLE AS can source from a delegated expression.
// For example: CREATE TABLE t2 AS SELECT * FROM [SHOW CREATE t1];
if d.qualifyDataSourceNamesInAST {
resName.ExplicitSchema = true
resName.ExplicitCatalog = true
*name = *resName.ToUnresolvedObjectName()
}
return dataSource, resName, nil
}
// resolveAndModifyTableIndexName may modify the name input
// if d.qualifyDataSourceNamesInAST == true
func (d *delegator) resolveAndModifyTableIndexName(
name *tree.TableIndexName,
) (cat.DataSource, cat.DataSourceName, error) {
tn := name.Table
dataSource, resName, err := d.catalog.ResolveDataSource(d.ctx, resolveFlags, &tn)
if err != nil {
return nil, cat.DataSourceName{}, err
}
if err := d.catalog.CheckAnyPrivilege(d.ctx, dataSource); err != nil {
return nil, cat.DataSourceName{}, err
}
// Force resolution of the index.
_, _, err = cat.ResolveTableIndex(d.ctx, d.catalog, resolveFlags, name)
if err != nil {
return nil, cat.DataSourceName{}, err
}
// Use qualifyDataSourceNamesInAST similarly to the Builder so that
// CREATE TABLE AS can source from a delegated expression.
// For example: CREATE TABLE t2 AS SELECT * FROM [SHOW PARTITIONS FROM INDEX t1@t1_pkey];
if d.qualifyDataSourceNamesInAST {
resName.ExplicitSchema = true
resName.ExplicitCatalog = true
(*name).Table = resName.ToUnresolvedObjectName().ToTableName()
}
return dataSource, resName, nil
}