-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
*: add scope check when get system variables #6958
Changes from 1 commit
8fb0a84
3781767
ded54b4
1e7805f
13f1cfd
a79d5ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5008,6 +5008,7 @@ SystemVariable: | |
{ | ||
v := strings.ToLower($1) | ||
var isGlobal bool | ||
explicitScope := true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alignment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
if strings.HasPrefix(v, "@@global.") { | ||
isGlobal = true | ||
v = strings.TrimPrefix(v, "@@global.") | ||
|
@@ -5016,9 +5017,9 @@ SystemVariable: | |
} else if strings.HasPrefix(v, "@@local.") { | ||
v = strings.TrimPrefix(v, "@@local.") | ||
} else if strings.HasPrefix(v, "@@") { | ||
v = strings.TrimPrefix(v, "@@") | ||
v, explicitScope = strings.TrimPrefix(v, "@@"), false | ||
} | ||
$$ = &ast.VariableExpr{Name: v, IsGlobal: isGlobal, IsSystem: true} | ||
$$ = &ast.VariableExpr{Name: v, IsGlobal: isGlobal, IsSystem: true, ExplicitScope: explicitScope} | ||
} | ||
|
||
UserVariable: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -781,6 +781,13 @@ func (er *expressionRewriter) rewriteVariable(v *ast.VariableExpr) { | |
} | ||
var val string | ||
var err error | ||
if v.ExplicitScope { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why should we care if it is set explicitly or not? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if the scope constraint is not set explicitly, the behavior is to choose the right scope automatically. So the scope check should't happen here, logically. For example: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right. I got something here:
|
||
err = variable.ValidateGetSystemVar(name, v.IsGlobal) | ||
if err != nil { | ||
er.err = errors.Trace(err) | ||
return | ||
} | ||
} | ||
if v.IsGlobal { | ||
val, err = variable.GetGlobalSystemVar(sessionVars, name) | ||
} else { | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -69,7 +69,7 @@ const ( | |||
var ( | ||||
UnknownStatusVar = terror.ClassVariable.New(CodeUnknownStatusVar, "unknown status variable") | ||||
UnknownSystemVar = terror.ClassVariable.New(CodeUnknownSystemVar, "unknown system variable '%s'") | ||||
ErrIncorrectScope = terror.ClassVariable.New(CodeIncorrectScope, "Incorrect variable scope") | ||||
ErrIncorrectScope = terror.ClassVariable.New(CodeIncorrectScope, "Variable '%s' is a %s variable") | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use Line 256 in 3db7594
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed. These error codes can be refined. Will fix |
||||
ErrUnknownTimeZone = terror.ClassVariable.New(CodeUnknownTimeZone, "unknown or incorrect time zone: %s") | ||||
ErrReadOnly = terror.ClassVariable.New(CodeReadOnly, "variable is read only") | ||||
) | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,6 +137,25 @@ func SetSessionSystemVar(vars *SessionVars, name string, value types.Datum) erro | |
return vars.SetSystemVar(name, sVal) | ||
} | ||
|
||
// ValidateGetSystemVar check if system variable exists and validate it's scope when get system variable. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/check/checks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. will fix |
||
func ValidateGetSystemVar(name string, isGlobal bool) error { | ||
sysVar, exists := SysVars[name] | ||
if !exists { | ||
return UnknownSystemVar.GenByArgs(name) | ||
} | ||
switch sysVar.Scope { | ||
case ScopeGlobal, ScopeNone: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why handle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if !isGlobal { | ||
return ErrIncorrectScope.GenByArgs(name, "GLOBAL") | ||
} | ||
case ScopeSession: | ||
if isGlobal { | ||
return ErrIncorrectScope.GenByArgs(name, "SESSION") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// TiDBOptOn could be used for all tidb session variable options, we use "ON"/1 to turn on those options. | ||
func TiDBOptOn(opt string) bool { | ||
return strings.EqualFold(opt, "ON") || opt == "1" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is set explicitly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok