From 05a1a80c5a1b11dd05914a513ab26ce6e7fdfa9c Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Wed, 9 Jan 2019 14:12:40 +0800 Subject: [PATCH 1/2] sessionctx/variable: fix select variable return wrong result when variable is only global scope variable (#8968) --- executor/set_test.go | 21 +++++++++++++++++++++ plan/expression_rewriter.go | 8 +++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/executor/set_test.go b/executor/set_test.go index bf7981df61a66..d779e8a7e5c83 100644 --- a/executor/set_test.go +++ b/executor/set_test.go @@ -17,6 +17,7 @@ import ( . "github.com/pingcap/check" "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/sessionctx/variable" + "github.com/pingcap/tidb/terror" "github.com/pingcap/tidb/util/testkit" "golang.org/x/net/context" ) @@ -244,3 +245,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(terror.ErrorEqual(err, variable.UnknownSystemVar), IsTrue, Commentf("err %v", err)) + _, err = tk.Exec("select @@global.invalid") + c.Assert(terror.ErrorEqual(err, variable.UnknownSystemVar), IsTrue, Commentf("err %v", err)) +} 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) From 36c1bcb31e1f4b01c1c67e9bafbea2b97f9bbf4d Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Thu, 10 Jan 2019 14:13:27 +0800 Subject: [PATCH 2/2] update test --- executor/set_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/executor/set_test.go b/executor/set_test.go index d779e8a7e5c83..980f01379e7c4 100644 --- a/executor/set_test.go +++ b/executor/set_test.go @@ -17,7 +17,6 @@ import ( . "github.com/pingcap/check" "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/sessionctx/variable" - "github.com/pingcap/tidb/terror" "github.com/pingcap/tidb/util/testkit" "golang.org/x/net/context" ) @@ -261,7 +260,7 @@ func (s *testSuite) TestSelectGlobalVar(c *C) { // test for unknown variable. _, err := tk.Exec("select @@invalid") - c.Assert(terror.ErrorEqual(err, variable.UnknownSystemVar), IsTrue, Commentf("err %v", err)) + c.Assert(err.Error(), Equals, variable.UnknownSystemVar.GenByArgs("invalid").Error()) _, err = tk.Exec("select @@global.invalid") - c.Assert(terror.ErrorEqual(err, variable.UnknownSystemVar), IsTrue, Commentf("err %v", err)) + c.Assert(err.Error(), Equals, variable.UnknownSystemVar.GenByArgs("invalid").Error()) }