forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
270 lines (253 loc) · 7.32 KB
/
set.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
// Copyright 2016 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package executor
import (
"fmt"
"strings"
"time"
"github.com/juju/errors"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/charset"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/sqlexec"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
)
// SetExecutor executes set statement.
type SetExecutor struct {
baseExecutor
vars []*expression.VarAssignment
done bool
}
// Next implements the Executor Next interface.
func (e *SetExecutor) Next(ctx context.Context, chk *chunk.Chunk) error {
chk.Reset()
if e.done {
return nil
}
e.done = true
sessionVars := e.ctx.GetSessionVars()
for _, v := range e.vars {
// Variable is case insensitive, we use lower case.
if v.Name == ast.SetNames {
// This is set charset stmt.
dt, err := v.Expr.(*expression.Constant).Eval(nil)
if err != nil {
return errors.Trace(err)
}
cs := dt.GetString()
var co string
if v.ExtendValue != nil {
co = v.ExtendValue.Value.GetString()
}
err = e.setCharset(cs, co)
if err != nil {
return errors.Trace(err)
}
continue
}
name := strings.ToLower(v.Name)
if !v.IsSystem {
// Set user variable.
value, err := v.Expr.Eval(nil)
if err != nil {
return errors.Trace(err)
}
if value.IsNull() {
delete(sessionVars.Users, name)
} else {
svalue, err1 := value.ToString()
if err1 != nil {
return errors.Trace(err1)
}
sessionVars.Users[name] = fmt.Sprintf("%v", svalue)
}
continue
}
syns := e.getSynonyms(name)
// Set system variable
for _, n := range syns {
err := e.setSysVariable(n, v)
if err != nil {
return errors.Trace(err)
}
}
}
return nil
}
func (e *SetExecutor) getSynonyms(varName string) []string {
synonyms, ok := variable.SynonymsSysVariables[varName]
if ok {
return synonyms
}
synonyms = []string{varName}
return synonyms
}
func (e *SetExecutor) setSysVariable(name string, v *expression.VarAssignment) error {
sessionVars := e.ctx.GetSessionVars()
sysVar := variable.GetSysVar(name)
if sysVar == nil {
return variable.UnknownSystemVar.GenByArgs(name)
}
if sysVar.Scope == variable.ScopeNone {
return errors.Errorf("Variable '%s' is a read only variable", name)
}
if v.IsGlobal {
// Set global scope system variable.
if sysVar.Scope&variable.ScopeGlobal == 0 {
return errors.Errorf("Variable '%s' is a SESSION variable and can't be used with SET GLOBAL", name)
}
value, err := e.getVarValue(v, sysVar)
if err != nil {
return errors.Trace(err)
}
if value.IsNull() {
value.SetString("")
}
svalue, err := value.ToString()
if err != nil {
return errors.Trace(err)
}
err = sessionVars.GlobalVarsAccessor.SetGlobalSysVar(name, svalue)
if err != nil {
return errors.Trace(err)
}
} else {
// Set session scope system variable.
if sysVar.Scope&variable.ScopeSession == 0 {
return errors.Errorf("Variable '%s' is a GLOBAL variable and should be set with SET GLOBAL", name)
}
value, err := e.getVarValue(v, nil)
if err != nil {
return errors.Trace(err)
}
oldSnapshotTS := sessionVars.SnapshotTS
if name == variable.TxnIsolationOneShot && sessionVars.InTxn() {
return errors.Trace(ErrCantChangeTxCharacteristics)
}
err = variable.SetSessionSystemVar(sessionVars, name, value)
if err != nil {
return errors.Trace(err)
}
newSnapshotIsSet := sessionVars.SnapshotTS > 0 && sessionVars.SnapshotTS != oldSnapshotTS
if newSnapshotIsSet {
err = validateSnapshot(e.ctx, sessionVars.SnapshotTS)
if err != nil {
sessionVars.SnapshotTS = oldSnapshotTS
return errors.Trace(err)
}
}
err = e.loadSnapshotInfoSchemaIfNeeded(name)
if err != nil {
sessionVars.SnapshotTS = oldSnapshotTS
return errors.Trace(err)
}
var valStr string
if value.IsNull() {
valStr = "NULL"
} else {
var err error
valStr, err = value.ToString()
terror.Log(errors.Trace(err))
}
log.Infof("[con:%d] set system variable %s = %s", sessionVars.ConnectionID, name, valStr)
}
if name == variable.TxnIsolation {
isoLevel, _ := sessionVars.GetSystemVar(variable.TxnIsolation)
if isoLevel == ast.ReadCommitted {
e.ctx.Txn().SetOption(kv.IsolationLevel, kv.RC)
}
}
return nil
}
// validateSnapshot checks that the newly set snapshot time is after GC safe point time.
func validateSnapshot(ctx sessionctx.Context, snapshotTS uint64) error {
sql := "SELECT variable_value FROM mysql.tidb WHERE variable_name = 'tikv_gc_safe_point'"
rows, _, err := ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(ctx, sql)
if err != nil {
return errors.Trace(err)
}
if len(rows) != 1 {
return errors.New("can not get 'tikv_gc_safe_point'")
}
safePointString := rows[0].GetString(0)
const gcTimeFormat = "20060102-15:04:05 -0700 MST"
safePointTime, err := time.Parse(gcTimeFormat, safePointString)
if err != nil {
return errors.Trace(err)
}
safePointTS := variable.GoTimeToTS(safePointTime)
if safePointTS > snapshotTS {
return variable.ErrSnapshotTooOld.GenByArgs(safePointString)
}
return nil
}
func (e *SetExecutor) setCharset(cs, co string) error {
var err error
if len(co) == 0 {
co, err = charset.GetDefaultCollation(cs)
if err != nil {
return errors.Trace(err)
}
}
sessionVars := e.ctx.GetSessionVars()
for _, v := range variable.SetNamesVariables {
terror.Log(errors.Trace(sessionVars.SetSystemVar(v, cs)))
}
terror.Log(errors.Trace(sessionVars.SetSystemVar(variable.CollationConnection, co)))
return nil
}
func (e *SetExecutor) getVarValue(v *expression.VarAssignment, sysVar *variable.SysVar) (value types.Datum, err error) {
if v.IsDefault {
// To set a SESSION variable to the GLOBAL value or a GLOBAL value
// to the compiled-in MySQL default value, use the DEFAULT keyword.
// See http://dev.mysql.com/doc/refman/5.7/en/set-statement.html
if sysVar != nil {
value = types.NewStringDatum(sysVar.Value)
} else {
s, err1 := variable.GetGlobalSystemVar(e.ctx.GetSessionVars(), v.Name)
if err1 != nil {
return value, errors.Trace(err1)
}
value = types.NewStringDatum(s)
}
return
}
value, err = v.Expr.Eval(nil)
return value, errors.Trace(err)
}
func (e *SetExecutor) loadSnapshotInfoSchemaIfNeeded(name string) error {
if name != variable.TiDBSnapshot {
return nil
}
vars := e.ctx.GetSessionVars()
if vars.SnapshotTS == 0 {
vars.SnapshotInfoschema = nil
return nil
}
log.Infof("[con:%d] loadSnapshotInfoSchema, SnapshotTS:%d", vars.ConnectionID, vars.SnapshotTS)
dom := domain.GetDomain(e.ctx)
snapInfo, err := dom.GetSnapshotInfoSchema(vars.SnapshotTS)
if err != nil {
return errors.Trace(err)
}
vars.SnapshotInfoschema = snapInfo
return nil
}