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
  • Loading branch information
erwadba authored Nov 2, 2020
1 parent c5d0f40 commit cc7a383
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 @@ -650,45 +650,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.GetSysVars()))
value string
sessionVars = e.ctx.GetSessionVars()
)
for _, v := range variable.GetSysVars() {
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.GetSysVars() {
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.GetSysVars() {
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.GetSysVar(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 @@ -30,6 +30,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 @@ -1135,3 +1136,26 @@ func (s *testSerialSuite1) TestShowCreateTableWithIntegerDisplayLengthWarnings(c
" `e` bigint DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
}

func (s *testSuite5) TestShowVar(c *C) {
tk := testkit.NewTestKit(c, s.store)
var showSQL string
for _, v := range variable.GetSysVars() {
// 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 cc7a383

Please sign in to comment.