Skip to content

Commit

Permalink
Revert "executor: fix show global variables return session variables …
Browse files Browse the repository at this point in the history
  • Loading branch information
bb7133 authored Sep 13, 2021
1 parent 842cb0c commit 647e246
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 47 deletions.
56 changes: 33 additions & 23 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,36 +667,46 @@ func (e *ShowExec) fetchShowMasterStatus() error {

func (e *ShowExec) fetchShowVariables() (err error) {
var (
value string
sessionVars = e.ctx.GetSessionVars()
value string
ok bool
sessionVars = e.ctx.GetSessionVars()
unreachedVars = make([]string, 0, len(variable.SysVars))
)
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})
}
}
return nil
}

// 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 !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
}
e.appendRow([]interface{}{v.Name, value})
}
if len(unreachedVars) != 0 {
systemVars, err := sessionVars.GlobalVarsAccessor.GetAllSysVars()
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})
}
}
return nil
}

Expand Down
24 changes: 0 additions & 24 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ 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 @@ -1029,26 +1028,3 @@ 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 647e246

Please sign in to comment.