Skip to content

Commit 9319872

Browse files
committed
use session default value instead of global variable default
1 parent 021d100 commit 9319872

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

server/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func (s *SessionManager) InitSessionDefaultVariable(ctx context.Context, conn *m
241241
if err != nil {
242242
return err
243243
}
244-
return sess.InitSessionDefaultVariable(s.ctxFactory(ctx, sql.WithSession(sess)), name, value)
244+
return sess.InitSessionVariableDefault(s.ctxFactory(ctx, sql.WithSession(sess)), name, value)
245245
}
246246

247247
// NewContextWithQuery creates a new context for the session at the given conn.

sql/base_session.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,20 @@ func (s *BaseSession) InitSessionVariable(ctx *Context, sysVarName string, value
159159
return s.setSessVar(ctx, sysVar, value, true)
160160
}
161161

162-
// InitSessionDefaultVariable implements the Session interface and is used to initialize variables (Including read-only variables)
163-
func (s *BaseSession) InitSessionDefaultVariable(ctx *Context, sysVarName string, value interface{}) error {
162+
// InitSessionVariableDefault implements the Session interface and is used to initialize variables (Including read-only variables)
163+
func (s *BaseSession) InitSessionVariableDefault(ctx *Context, sysVarName string, value interface{}) error {
164164
sysVar, _, ok := SystemVariables.GetGlobal(sysVarName)
165165
if !ok {
166166
return ErrUnknownSystemVariable.New(sysVarName)
167167
}
168168

169-
sysVarName = strings.ToLower(sysVarName)
170-
val, ok := s.systemVars[sysVarName]
171-
if ok && val.Val != sysVar.GetDefault() {
172-
return ErrSystemVariableReinitialized.New(sysVarName)
173-
}
174-
svv, err := sysVar.InitValue(ctx, "", value, false)
169+
sysVar.SetDefault(value)
170+
svv, err := sysVar.InitValue(ctx, sysVar.GetDefault(), value, false)
175171
if err != nil {
176172
return err
177173
}
178-
svv.Var.SetDefault(value)
174+
175+
sysVarName = strings.ToLower(sysVarName)
179176
s.systemVars[sysVarName] = svv
180177
if sysVarName == characterSetResultsSysVarName {
181178
s.charset = CharacterSet_Unspecified
@@ -230,6 +227,22 @@ func (s *BaseSession) GetSessionVariable(ctx *Context, sysVarName string) (inter
230227
return sysVar.Val, nil
231228
}
232229

230+
// GetSessionVariableDefault implements the Session interface.
231+
func (s *BaseSession) GetSessionVariableDefault(ctx *Context, sysVarName string) (interface{}, error) {
232+
sysVarName = strings.ToLower(sysVarName)
233+
sysVar, ok := s.systemVars[sysVarName]
234+
if !ok {
235+
return nil, ErrUnknownSystemVariable.New(sysVarName)
236+
}
237+
// TODO: this is duplicated from within variables.globalSystemVariables, suggesting the need for an interface
238+
if sysType, ok := sysVar.Var.GetType().(SetType); ok {
239+
if sv, ok := sysVar.Var.GetDefault().(uint64); ok {
240+
return sysType.BitsToString(sv)
241+
}
242+
}
243+
return sysVar.Var.GetDefault(), nil
244+
}
245+
233246
// GetUserVariable implements the Session interface.
234247
func (s *BaseSession) GetUserVariable(ctx *Context, varName string) (Type, interface{}, error) {
235248
return s.userVars.GetUserVariable(ctx, varName)

sql/planbuilder/set.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,9 @@ func (b *Builder) simplifySetExpr(name *ast.ColName, varScope ast.SetScope, val
341341

342342
switch varScope {
343343
case ast.SetScope_None, ast.SetScope_Session, ast.SetScope_Global:
344-
_, value, ok := sql.SystemVariables.GetGlobal(varName)
345-
if ok {
344+
// cannot use sql.SystemVariables.GetGlobal as the default value can be defined at session start runtime.
345+
value, err := b.ctx.GetSessionVariableDefault(b.ctx, varName)
346+
if err == nil {
346347
return expression.NewLiteral(value, types.ApproximateTypeFromValue(value)), true
347348
}
348349
err = sql.ErrUnknownSystemVariable.New(varName)

sql/session.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ type Session interface {
6666
Client() Client
6767
// SetClient returns a new session with the given client.
6868
SetClient(Client)
69-
// InitSessionDefaultVariable sets the given value as the default value of given system variable for this session.
70-
InitSessionDefaultVariable(ctx *Context, sysVarName string, value interface{}) error
69+
// InitSessionVariableDefault sets this session's default value of the system variable with the given name.
70+
InitSessionVariableDefault(ctx *Context, sysVarName string, value interface{}) error
7171
// SetSessionVariable sets the given system variable to the value given for this session.
7272
SetSessionVariable(ctx *Context, sysVarName string, value interface{}) error
7373
// InitSessionVariable sets the given system variable to the value given for this session and will allow for
@@ -78,6 +78,9 @@ type Session interface {
7878
// GetSessionVariable returns this session's value of the system variable with the given name.
7979
// To access global scope, use sql.SystemVariables.GetGlobal instead.
8080
GetSessionVariable(ctx *Context, sysVarName string) (interface{}, error)
81+
// GetSessionVariableDefault returns this session's default value of the system variable with the given name.
82+
// To access global scope, use sql.SystemVariables.GetGlobal instead.
83+
GetSessionVariableDefault(ctx *Context, sysVarName string) (interface{}, error)
8184
// GetUserVariable returns this session's value of the user variable with the given name, along with its most
8285
// appropriate type.
8386
GetUserVariable(ctx *Context, varName string) (Type, interface{}, error)

0 commit comments

Comments
 (0)