From 9b1ff689d4585547dbc5279f3388602e4e01a2ce Mon Sep 17 00:00:00 2001 From: crazycs Date: Tue, 15 Jan 2019 12:46:28 +0800 Subject: [PATCH] sessionctx/variable: fix select variable return wrong result when variable is only global scope variable (#8968) (#8992) --- executor/set_test.go | 20 ++++++++++++++++++++ plan/expression_rewriter.go | 8 +++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/executor/set_test.go b/executor/set_test.go index bf7981df61a66..980f01379e7c4 100644 --- a/executor/set_test.go +++ b/executor/set_test.go @@ -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()) +} diff --git a/plan/expression_rewriter.go b/plan/expression_rewriter.go index 44c11336de67c..ccace4903db08 100644 --- a/plan/expression_rewriter.go +++ b/plan/expression_rewriter.go @@ -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)