-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathdelegate.go
150 lines (112 loc) · 3.66 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
// 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/opt/cat"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"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 *tree.EvalContext, stmt tree.Statement,
) (tree.Statement, error) {
d := delegator{
ctx: ctx,
catalog: catalog,
evalCtx: evalCtx,
}
switch t := stmt.(type) {
case *tree.ShowClusterSettingList:
return d.delegateShowClusterSettingList(t)
case *tree.ShowDatabases:
return d.delegateShowDatabases(t)
case *tree.ShowEnums:
return d.delegateShowEnums()
case *tree.ShowTypes:
return d.delegateShowTypes()
case *tree.ShowCreate:
return d.delegateShowCreate(t)
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.ShowQueries:
return d.delegateShowQueries(t)
case *tree.ShowRanges:
return d.delegateShowRanges(t)
case *tree.ShowRangeForRow:
return d.delegateShowRangeForRow(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.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.ShowTransactionStatus:
return d.delegateShowVar(&tree.ShowVar{Name: "transaction_status"})
case *tree.ShowSchedules:
return d.delegateShowSchedules(t)
case *tree.ControlJobsForSchedules:
return d.delegateJobControl(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")
default:
return nil, nil
}
}
type delegator struct {
ctx context.Context
catalog cat.Catalog
evalCtx *tree.EvalContext
}
func parse(sql string) (tree.Statement, error) {
s, err := parser.ParseOne(sql)
return s.AST, err
}