Skip to content

Commit

Permalink
executor: fix show global variables return session variables also (pi…
Browse files Browse the repository at this point in the history
…ngcap#19341) (pingcap#21045)

Signed-off-by: ti-srebot <[email protected]>
  • Loading branch information
erwadba authored Nov 17, 2020
1 parent 68211c1 commit a1bdbef
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 33 deletions.
56 changes: 23 additions & 33 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,45 +646,35 @@ func (e *ShowExec) fetchShowMasterStatus() error {

func (e *ShowExec) fetchShowVariables() (err error) {
var (
value string
ok bool
sessionVars = e.ctx.GetSessionVars()
unreachedVars = make([]string, 0, len(variable.SysVars))
value string
sessionVars = e.ctx.GetSessionVars()
)
for _, v := range variable.SysVars {
if !e.GlobalScope {
// For a session scope variable,
// 1. try to fetch value from SessionVars.Systems;
// 2. if this variable is session-only, fetch value from SysVars
// otherwise, fetch the value from table `mysql.Global_Variables`.
value, ok, err = variable.GetSessionOnlySysVars(sessionVars, v.Name)
} else {
// If the scope of a system variable is ScopeNone,
// it's a read-only variable, so we return the default value of it.
// Otherwise, we have to fetch the values from table `mysql.Global_Variables` for global variable names.
value, ok, err = variable.GetScopeNoneSystemVar(v.Name)
}
if err != nil {
return errors.Trace(err)
}
if !ok {
unreachedVars = append(unreachedVars, v.Name)
continue
if e.GlobalScope {
// Collect global scope variables,
// 1. Exclude the variables of ScopeSession in variable.SysVars;
// 2. If the variable is ScopeNone, it's a read-only variable, return the default value of it,
// otherwise, fetch the value from table `mysql.Global_Variables`.
for _, v := range variable.SysVars {
if v.Scope != variable.ScopeSession {
value, err = variable.GetGlobalSystemVar(sessionVars, v.Name)
if err != nil {
return errors.Trace(err)
}
e.appendRow([]interface{}{v.Name, value})
}
}
e.appendRow([]interface{}{v.Name, value})
return nil
}
if len(unreachedVars) != 0 {
systemVars, err := sessionVars.GlobalVarsAccessor.GetAllSysVars()

// Collect session scope variables,
// If it is a session only variable, use the default value defined in code,
// otherwise, fetch the value from table `mysql.Global_Variables`.
for _, v := range variable.SysVars {
value, err = variable.GetSessionSystemVar(sessionVars, v.Name)
if err != nil {
return errors.Trace(err)
}
for _, varName := range unreachedVars {
varValue, ok := systemVars[varName]
if !ok {
varValue = variable.SysVars[varName].Value
}
e.appendRow([]interface{}{varName, varValue})
}
e.appendRow([]interface{}{v.Name, value})
}
return nil
}
Expand Down
24 changes: 24 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pingcap/tidb/privilege/privileges"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testutil"
Expand Down Expand Up @@ -962,3 +963,26 @@ func (s *testSuite5) TestShowClusterConfig(c *C) {
confErr = fmt.Errorf("something unknown error")
c.Assert(tk.QueryToErr("show config"), ErrorMatches, confErr.Error())
}

func (s *testSuite5) TestShowVar(c *C) {
tk := testkit.NewTestKit(c, s.store)
var showSQL string
for _, v := range variable.SysVars {
// When ScopeSession only. `show global variables` must return empty.
if v.Scope == variable.ScopeSession {
showSQL = "show variables like '" + v.Name + "'"
res := tk.MustQuery(showSQL)
c.Check(res.Rows(), HasLen, 1)
showSQL = "show global variables like '" + v.Name + "'"
res = tk.MustQuery(showSQL)
c.Check(res.Rows(), HasLen, 0)
} else {
showSQL = "show global variables like '" + v.Name + "'"
res := tk.MustQuery(showSQL)
c.Check(res.Rows(), HasLen, 1)
showSQL = "show variables like '" + v.Name + "'"
res = tk.MustQuery(showSQL)
c.Check(res.Rows(), HasLen, 1)
}
}
}

0 comments on commit a1bdbef

Please sign in to comment.