forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vars.go
283 lines (263 loc) · 9.3 KB
/
vars.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
280
281
282
283
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package sql
import (
"fmt"
"sort"
"strings"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/storage/engine/enginepb"
)
const (
// PgServerVersion is the latest version of postgres that we claim to support.
PgServerVersion = "9.5.0"
)
// sessionVar provides a unified interface for performing operations on
// variables such as the selected database, or desired syntax.
type sessionVar struct {
// Set performs mutations (usually on p.session) to effect the change
// desired by SET commands.
Set func(ctx context.Context, p *planner, values []parser.TypedExpr) error
// Get returns a string representation of a given variable to be used
// either by SHOW or in the pg_catalog table.
Get func(p *planner) string
// Reset performs mutations (usually on p.session) to effect the change
// desired by RESET commands.
Reset func(*planner) error
}
// nopVar is a placeholder for a number of settings sent by various client
// drivers which we do not support, but should simply ignore rather than
// throwing an error when trying to SET or SHOW them.
var nopVar = sessionVar{
Set: func(context.Context, *planner, []parser.TypedExpr) error { return nil },
Get: func(*planner) string { return "" },
Reset: func(*planner) error { return nil },
}
var varGen = map[string]sessionVar{
`database`: {
Set: func(ctx context.Context, p *planner, values []parser.TypedExpr) error {
dbName, err := p.getStringVal(`database`, values)
if err != nil {
return err
}
if len(dbName) != 0 {
// Verify database descriptor exists.
if _, err := MustGetDatabaseDesc(ctx, p.txn, p.getVirtualTabler(), dbName); err != nil {
return err
}
}
p.session.Database = dbName
p.evalCtx.Database = dbName
return nil
},
Get: func(p *planner) string { return p.session.Database },
Reset: func(p *planner) error {
p.session.Database = p.session.defaults.database
return nil
},
},
`default_transaction_isolation`: {
Set: func(_ context.Context, p *planner, values []parser.TypedExpr) error {
// It's unfortunate that clients want us to support both SET
// SESSION CHARACTERISTICS AS TRANSACTION ..., which takes the
// isolation level as keywords/identifiers (e.g. JDBC), and SET
// DEFAULT_TRANSACTION_ISOLATION TO '...', which takes an
// expression (e.g. psycopg2). But that's how it is. Just ensure
// this code keeps in sync with SetDefaultIsolation() in set.go.
s, err := p.getStringVal(`default_transaction_isolation`, values)
if err != nil {
return err
}
switch strings.ToUpper(s) {
case `READ UNCOMMITTED`, `READ COMMITTED`, `SNAPSHOT`:
p.session.DefaultIsolationLevel = enginepb.SNAPSHOT
case `REPEATABLE READ`, `SERIALIZABLE`:
p.session.DefaultIsolationLevel = enginepb.SERIALIZABLE
default:
return fmt.Errorf("set default_transaction_isolation: unknown isolation level: %q", s)
}
return nil
},
Get: func(p *planner) string { return p.session.DefaultIsolationLevel.String() },
Reset: func(p *planner) error {
p.session.DefaultIsolationLevel = enginepb.IsolationType(0)
return nil
},
},
`distsql`: {
Set: func(_ context.Context, p *planner, values []parser.TypedExpr) error {
s, err := p.getStringVal(`distsql`, values)
if err != nil {
return err
}
switch parser.Name(s).Normalize() {
case parser.ReNormalizeName("off"):
p.session.DistSQLMode = DistSQLOff
case parser.ReNormalizeName("on"):
p.session.DistSQLMode = DistSQLOn
case parser.ReNormalizeName("auto"):
p.session.DistSQLMode = DistSQLAuto
case parser.ReNormalizeName("always"):
p.session.DistSQLMode = DistSQLAlways
default:
return fmt.Errorf("set distsql: \"%s\" not supported", s)
}
return nil
},
Get: func(p *planner) string {
return p.session.DistSQLMode.String()
},
Reset: func(p *planner) error {
p.session.DistSQLMode = DistSQLExecModeFromInt(DistSQLClusterExecMode.Get())
if p.session.execCfg.TestingKnobs.OverrideDistSQLMode != nil {
p.session.DistSQLMode = DistSQLExecModeFromInt(p.session.execCfg.TestingKnobs.OverrideDistSQLMode.Get())
}
return nil
},
},
`search_path`: {
Set: func(_ context.Context, p *planner, values []parser.TypedExpr) error {
// https://www.postgresql.org/docs/9.6/static/runtime-config-client.html
newSearchPath := make(parser.SearchPath, len(values))
foundPgCatalog := false
for i, v := range values {
val, err := v.Eval(&p.evalCtx)
if err != nil {
return err
}
s, ok := parser.AsDString(val)
if !ok {
return fmt.Errorf("set search_path: requires string values: %s is %s not string",
v, val.ResolvedType())
}
if s == pgCatalogName {
foundPgCatalog = true
}
newSearchPath[i] = parser.Name(s).Normalize()
}
if !foundPgCatalog {
// "The system catalog schema, pg_catalog, is always searched,
// whether it is mentioned in the path or not. If it is
// mentioned in the path then it will be searched in the
// specified order. If pg_catalog is not in the path then it
// will be searched before searching any of the path items."
newSearchPath = append([]string{"pg_catalog"}, newSearchPath...)
}
p.session.SearchPath = newSearchPath
return nil
},
Get: func(p *planner) string { return strings.Join(p.session.SearchPath, ", ") },
Reset: func(p *planner) error {
p.session.SearchPath = sqlbase.DefaultSearchPath
return nil
},
},
`standard_conforming_strings`: {
Set: func(_ context.Context, p *planner, values []parser.TypedExpr) error {
// If true, escape backslash literals in strings. We do this by default,
// and we do not support the opposite behavior.
// See https://www.postgresql.org/docs/9.1/static/runtime-config-compatible.html#GUC-STANDARD-CONFORMING-STRINGS
s, err := p.getStringVal(`standard_conforming_strings`, values)
if err != nil {
return err
}
if parser.Name(s).Normalize() != parser.ReNormalizeName("on") {
return fmt.Errorf("set standard_conforming_strings: \"%s\" not supported", s)
}
return nil
},
Get: func(*planner) string { return "on" },
Reset: func(*planner) error { return nil },
},
`application_name`: {
Set: func(_ context.Context, p *planner, values []parser.TypedExpr) error {
// Set by clients to improve query logging.
// See https://www.postgresql.org/docs/9.6/static/runtime-config-logging.html#GUC-APPLICATION-NAME
s, err := p.getStringVal(`application_name`, values)
if err != nil {
return err
}
p.session.resetApplicationName(s)
return nil
},
Get: func(p *planner) string { return p.session.ApplicationName },
Reset: func(p *planner) error {
p.session.resetApplicationName(p.session.defaults.applicationName)
return nil
},
},
`time zone`: {
Get: func(p *planner) string {
// If the time zone is a "fixed offset" one, initialized from an offset
// and not a standard name, then we use a magic format in the Location's
// name. We attempt to parse that here and retrieve the original offset
// specified by the user.
_, origRepr, parsed := sqlbase.ParseFixedOffsetTimeZone(p.session.Location.String())
if parsed {
return origRepr
}
return p.session.Location.String()
},
},
`transaction isolation level`: {
Get: func(p *planner) string { return p.txn.Isolation().String() },
},
`transaction priority`: {
Get: func(p *planner) string { return p.txn.UserPriority().String() },
},
`transaction status`: {
Get: func(p *planner) string { return getTransactionState(&p.session.TxnState, p.autoCommit) },
},
`max_index_keys`: {
Get: func(*planner) string { return "32" },
},
`server_version`: {
Get: func(*planner) string { return PgServerVersion },
},
`session_user`: {
Get: func(p *planner) string { return p.session.User },
},
// See https://www.postgresql.org/docs/9.6/static/runtime-config-client.html
`extra_float_digits`: nopVar,
// Controls returned message verbosity. We don't support this.
// See https://www.postgresql.org/docs/9.6/static/runtime-config-compatible.html
`client_min_messages`: nopVar,
// See https://www.postgresql.org/docs/9.6/static/multibyte.html
`client_encoding`: {
Get: func(*planner) string {
return "UTF8"
},
Set: func(_ context.Context, p *planner, values []parser.TypedExpr) error {
s, err := p.getStringVal(`client_encoding`, values)
if err != nil {
return err
}
upper := strings.ToUpper(s)
if upper != "UTF8" && upper != "UNICODE" {
return fmt.Errorf("non-UTF8 encoding %s not supported", s)
}
return nil
},
},
}
var varNames = func() []string {
res := make([]string, 0, len(varGen))
for vName := range varGen {
res = append(res, vName)
}
sort.Strings(res)
return res
}()