Skip to content

Commit

Permalink
sessionctx/variable: fix select variable return wrong result when var…
Browse files Browse the repository at this point in the history
…iable is only global scope variable (#8968) (#8992)
  • Loading branch information
crazycs520 authored and zz-jason committed Jan 15, 2019
1 parent a6753a3 commit 9b1ff68
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
20 changes: 20 additions & 0 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,23 @@ func (s *testSuite) TestSetCharset(c *C) {
// Issue 1523
tk.MustExec(`SET NAMES binary`)
}

func (s *testSuite) TestSelectGlobalVar(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustQuery("select @@global.max_connections;").Check(testkit.Rows("151"))
tk.MustQuery("select @@max_connections;").Check(testkit.Rows("151"))

tk.MustExec("set @@global.max_connections=100;")

tk.MustQuery("select @@global.max_connections;").Check(testkit.Rows("100"))
tk.MustQuery("select @@max_connections;").Check(testkit.Rows("100"))

tk.MustExec("set @@global.max_connections=151;")

// test for unknown variable.
_, err := tk.Exec("select @@invalid")
c.Assert(err.Error(), Equals, variable.UnknownSystemVar.GenByArgs("invalid").Error())
_, err = tk.Exec("select @@global.invalid")
c.Assert(err.Error(), Equals, variable.UnknownSystemVar.GenByArgs("invalid").Error())
}
8 changes: 7 additions & 1 deletion plan/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,13 @@ func (er *expressionRewriter) rewriteVariable(v *ast.VariableExpr) {
}
var val string
var err error
if v.IsGlobal {
sysVar := variable.SysVars[name]
if sysVar == nil {
er.err = variable.UnknownSystemVar.GenByArgs(name)
return
}
// Variable is @@gobal.variable_name or variable is only global scope variable.
if v.IsGlobal || sysVar.Scope == variable.ScopeGlobal {
val, err = variable.GetGlobalSystemVar(sessionVars, name)
} else {
val, err = variable.GetSessionSystemVar(sessionVars, name)
Expand Down

0 comments on commit 9b1ff68

Please sign in to comment.