Skip to content

Commit

Permalink
cherry pick pingcap#19341 to release-4.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
erwadba authored and ti-srebot committed Nov 2, 2020
1 parent e069732 commit c3bc6b2
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 3 deletions.
33 changes: 30 additions & 3 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ func (e *ShowExec) fetchShowMasterStatus() error {

func (e *ShowExec) fetchShowVariables() (err error) {
var (
<<<<<<< HEAD
value string
ok bool
sessionVars = e.ctx.GetSessionVars()
Expand All @@ -667,21 +668,47 @@ func (e *ShowExec) fetchShowVariables() (err error) {
if !ok {
unreachedVars = append(unreachedVars, v.Name)
continue
=======
value string
sessionVars = e.ctx.GetSessionVars()
)
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})
}
>>>>>>> cc7a38327... executor: fix show global variables return session variables also (#19341)
}
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)
}
<<<<<<< HEAD
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})
>>>>>>> cc7a38327... executor: fix show global variables return session variables also (#19341)
}
return nil
}
Expand Down
91 changes: 91 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 @@ -951,3 +952,93 @@ func (s *testSuite5) TestShowClusterConfig(c *C) {
confErr = fmt.Errorf("something unknown error")
c.Assert(tk.QueryToErr("show config"), ErrorMatches, confErr.Error())
}
<<<<<<< HEAD
=======

func (s *testSerialSuite1) TestShowCreateTableWithIntegerDisplayLengthWarnings(c *C) {
parsertypes.TiDBStrictIntegerDisplayWidth = true
defer func() {
parsertypes.TiDBStrictIntegerDisplayWidth = false
}()
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int(2), b varchar(2))")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1064 You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use [parser:1681]Integer display width is deprecated and will be removed in a future release."))
tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
" `a` int DEFAULT NULL,\n" +
" `b` varchar(2) DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a bigint(10), b bigint)")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1064 You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use [parser:1681]Integer display width is deprecated and will be removed in a future release."))
tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
" `a` bigint DEFAULT NULL,\n" +
" `b` bigint DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a tinyint(5), b tinyint(2), c tinyint)")
// Here it will occur 2 warnings.
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1064 You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use [parser:1681]Integer display width is deprecated and will be removed in a future release.",
"Warning 1064 You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use [parser:1681]Integer display width is deprecated and will be removed in a future release."))
tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
" `a` tinyint DEFAULT NULL,\n" +
" `b` tinyint DEFAULT NULL,\n" +
" `c` tinyint DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a smallint(5), b smallint)")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1064 You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use [parser:1681]Integer display width is deprecated and will be removed in a future release."))
tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
" `a` smallint DEFAULT NULL,\n" +
" `b` smallint DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a mediumint(5), b mediumint)")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1064 You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use [parser:1681]Integer display width is deprecated and will be removed in a future release."))
tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
" `a` mediumint DEFAULT NULL,\n" +
" `b` mediumint DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int1(1), b int2(2), c int3, d int4, e int8)")
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1064 You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use [parser:1681]Integer display width is deprecated and will be removed in a future release.",
"Warning 1064 You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use [parser:1681]Integer display width is deprecated and will be removed in a future release."))
tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
" `a` tinyint DEFAULT NULL,\n" +
" `b` smallint DEFAULT NULL,\n" +
" `c` mediumint DEFAULT NULL,\n" +
" `d` int DEFAULT NULL,\n" +
" `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)
}
}
}
>>>>>>> cc7a38327... executor: fix show global variables return session variables also (#19341)

0 comments on commit c3bc6b2

Please sign in to comment.