Skip to content
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

Merged
merged 6 commits into from
Jul 3, 2018
Merged

Conversation

spongedu
Copy link
Contributor

@spongedu spongedu commented Jul 2, 2018

What have you changed? (mandatory)

Add scope check when get system variables.
session variables cant' be accessed via SELECT @@global.xxxx. The same, global variables can'e be accessed via SELECT @@session.xxx or SELECT @@local.xxx.

What are the type of the changes (mandatory)?

enhancement

How has this PR been tested (mandatory)?

Unittest

@shenli shenli added the contribution This PR is from a community contributor. label Jul 2, 2018
@@ -944,6 +944,8 @@ type VariableExpr struct {
IsGlobal bool
// IsSystem indicates whether this variable is a system variable in current session.
IsSystem bool
// ExplicitScope indicates whether this variable scope is explicit set.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is set explicitly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

parser/parser.y Outdated
@@ -5008,6 +5008,7 @@ SystemVariable:
{
v := strings.ToLower($1)
var isGlobal bool
explicitScope := true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alignment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@@ -781,6 +781,13 @@ func (er *expressionRewriter) rewriteVariable(v *ast.VariableExpr) {
}
var val string
var err error
if v.ExplicitScope {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should we care if it is set explicitly or not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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:
for a global variable max_connections, select @@max_connections works, and for a session variable warning_count, select @@warnging_count works as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I got something here:

For a reference to a system variable in an expression as @@var_name (rather than with @@global. or @@session.), MySQL returns the session value if it exists and the global value otherwise. This differs from SET @@var_name = expr, which always refers to the session value.

@shenli
Copy link
Member

shenli commented Jul 2, 2018

/run-all-tests

@shenli
Copy link
Member

shenli commented Jul 3, 2018

LGTM

@shenli
Copy link
Member

shenli commented Jul 3, 2018

/run-all-tests

@shenli shenli added the status/LGT1 Indicates that a PR has LGTM 1. label Jul 3, 2018
@shenli
Copy link
Member

shenli commented Jul 3, 2018

@tiancaiamao @lysu PTAL

return UnknownSystemVar.GenByArgs(name)
}
switch sysVar.Scope {
case ScopeGlobal, ScopeNone:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why handle ScopeGlobal and ScopeNone together?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ScopeNone variables are global variables that can't be modified dynamically

@@ -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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use

ErrIncorrectGlobalLocalVar: "Variable '%-.192s' is a %s variable",
?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. These error codes can be refined. Will fix

@spongedu
Copy link
Contributor Author

spongedu commented Jul 3, 2018

@jackysp PTAL

jackysp
jackysp previously approved these changes Jul 3, 2018
Copy link
Member

@jackysp jackysp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jackysp jackysp added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Jul 3, 2018
@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/check/checks
"validates its scope when getting system variable" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. will fix

Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zz-jason zz-jason merged commit 490af37 into pingcap:master Jul 3, 2018
@spongedu spongedu deleted the 0703 branch July 3, 2018 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution This PR is from a community contributor. status/LGT2 Indicates that a PR has LGTM 2. type/compatibility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants