Skip to content

Commit

Permalink
*: Fix sysvars to native type in @@ context (pingcap#20394)
Browse files Browse the repository at this point in the history
  • Loading branch information
Null not nil authored Oct 19, 2020
1 parent 4501f6d commit 106b04d
Show file tree
Hide file tree
Showing 17 changed files with 313 additions and 460 deletions.
36 changes: 18 additions & 18 deletions bindinfo/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,59 +797,59 @@ func (s *testSuite) TestDefaultSessionVars(c *C) {
tk := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk)
tk.MustQuery(`show variables like "%baselines%"`).Sort().Check(testkit.Rows(
"tidb_capture_plan_baselines off",
"tidb_evolve_plan_baselines off",
"tidb_use_plan_baselines on"))
"tidb_capture_plan_baselines OFF",
"tidb_evolve_plan_baselines OFF",
"tidb_use_plan_baselines ON"))
tk.MustQuery(`show global variables like "%baselines%"`).Sort().Check(testkit.Rows(
"tidb_capture_plan_baselines off",
"tidb_evolve_plan_baselines off",
"tidb_use_plan_baselines on"))
"tidb_capture_plan_baselines OFF",
"tidb_evolve_plan_baselines OFF",
"tidb_use_plan_baselines ON"))
}

func (s *testSuite) TestCaptureBaselinesScope(c *C) {
tk1 := testkit.NewTestKit(c, s.store)
tk2 := testkit.NewTestKit(c, s.store)
s.cleanBindingEnv(tk1)
tk1.MustQuery(`show session variables like "tidb_capture_plan_baselines"`).Check(testkit.Rows(
"tidb_capture_plan_baselines off",
"tidb_capture_plan_baselines OFF",
))
tk1.MustQuery(`show global variables like "tidb_capture_plan_baselines"`).Check(testkit.Rows(
"tidb_capture_plan_baselines off",
"tidb_capture_plan_baselines OFF",
))
tk1.MustQuery(`select @@session.tidb_capture_plan_baselines`).Check(testkit.Rows(
"off",
"0",
))
tk1.MustQuery(`select @@global.tidb_capture_plan_baselines`).Check(testkit.Rows(
"off",
"0",
))

tk1.MustExec("set @@session.tidb_capture_plan_baselines = on")
defer func() {
tk1.MustExec(" set @@session.tidb_capture_plan_baselines = off")
}()
tk1.MustQuery(`show session variables like "tidb_capture_plan_baselines"`).Check(testkit.Rows(
"tidb_capture_plan_baselines on",
"tidb_capture_plan_baselines ON",
))
tk1.MustQuery(`show global variables like "tidb_capture_plan_baselines"`).Check(testkit.Rows(
"tidb_capture_plan_baselines off",
"tidb_capture_plan_baselines OFF",
))
tk1.MustQuery(`select @@session.tidb_capture_plan_baselines`).Check(testkit.Rows(
"on",
"1",
))
tk1.MustQuery(`select @@global.tidb_capture_plan_baselines`).Check(testkit.Rows(
"off",
"0",
))
tk2.MustQuery(`show session variables like "tidb_capture_plan_baselines"`).Check(testkit.Rows(
"tidb_capture_plan_baselines on",
"tidb_capture_plan_baselines ON",
))
tk2.MustQuery(`show global variables like "tidb_capture_plan_baselines"`).Check(testkit.Rows(
"tidb_capture_plan_baselines off",
"tidb_capture_plan_baselines OFF",
))
tk2.MustQuery(`select @@session.tidb_capture_plan_baselines`).Check(testkit.Rows(
"on",
"1",
))
tk2.MustQuery(`select @@global.tidb_capture_plan_baselines`).Check(testkit.Rows(
"off",
"0",
))
}

Expand Down
2 changes: 1 addition & 1 deletion ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func buildTablePartitionInfo(ctx sessionctx.Context, s *ast.CreateTableStmt) (*m
return nil, nil
}

if ctx.GetSessionVars().EnableTablePartition == "off" {
if strings.EqualFold(ctx.GetSessionVars().EnableTablePartition, "OFF") {
ctx.GetSessionVars().StmtCtx.AppendWarning(errTablePartitionDisabled)
return nil, nil
}
Expand Down
4 changes: 3 additions & 1 deletion executor/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ func (e *SetExecutor) setSysVariable(name string, v *expression.VarAssignment) e
logutil.BgLogger().Debug(fmt.Sprintf("set %s var", scopeStr), zap.Uint64("conn", sessionVars.ConnectionID), zap.String("name", name), zap.String("val", valStr))
}

valStrToBoolStr := variable.BoolToOnOff(variable.TiDBOptOn(valStr))

switch name {
case variable.TiDBEnableStmtSummary:
return stmtsummary.StmtSummaryByDigestMap.SetEnabled(valStr, !v.IsGlobal)
Expand All @@ -234,7 +236,7 @@ func (e *SetExecutor) setSysVariable(name string, v *expression.VarAssignment) e
case variable.TiDBStmtSummaryMaxSQLLength:
return stmtsummary.StmtSummaryByDigestMap.SetMaxSQLLength(valStr, !v.IsGlobal)
case variable.TiDBCapturePlanBaseline:
variable.CapturePlanBaseline.Set(strings.ToLower(valStr), !v.IsGlobal)
variable.CapturePlanBaseline.Set(valStrToBoolStr, !v.IsGlobal)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func (s *testSuite5) TestSetVar(c *C) {
tk.MustQuery(`select @@session.tidb_wait_split_region_timeout;`).Check(testkit.Rows("1"))
_, err = tk.Exec("set tidb_wait_split_region_timeout = 0")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "tidb_wait_split_region_timeout(0) cannot be smaller than 1")
c.Assert(err, ErrorMatches, ".*Variable 'tidb_wait_split_region_timeout' can't be set to the value of '0'")
tk.MustQuery(`select @@session.tidb_wait_split_region_timeout;`).Check(testkit.Rows("1"))

tk.MustExec("set session tidb_backoff_weight = 3")
Expand Down Expand Up @@ -414,14 +414,14 @@ func (s *testSuite5) TestSetVar(c *C) {
tk.MustExec("set @@session.tidb_metric_query_step = 120")
_, err = tk.Exec("set @@session.tidb_metric_query_step = 9")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "tidb_metric_query_step(9) cannot be smaller than 10 or larger than 216000")
c.Assert(err, ErrorMatches, ".*Variable 'tidb_metric_query_step' can't be set to the value of '9'")
tk.MustQuery("select @@session.tidb_metric_query_step;").Check(testkit.Rows("120"))

tk.MustQuery("select @@session.tidb_metric_query_range_duration;").Check(testkit.Rows("60"))
tk.MustExec("set @@session.tidb_metric_query_range_duration = 120")
_, err = tk.Exec("set @@session.tidb_metric_query_range_duration = 9")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "tidb_metric_query_range_duration(9) cannot be smaller than 10 or larger than 216000")
c.Assert(err, ErrorMatches, ".*Variable 'tidb_metric_query_range_duration' can't be set to the value of '9'")
tk.MustQuery("select @@session.tidb_metric_query_range_duration;").Check(testkit.Rows("120"))

// test for tidb_slow_log_masking
Expand Down
5 changes: 2 additions & 3 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,10 @@ func (s *testSuite5) TestShow2(c *C) {

tk.MustExec("set global autocommit=0")
tk1 := testkit.NewTestKit(c, s.store)
tk1.MustQuery("show global variables where variable_name = 'autocommit'").Check(testkit.Rows("autocommit 0"))
tk1.MustQuery("show global variables where variable_name = 'autocommit'").Check(testkit.Rows("autocommit OFF"))
tk.MustExec("set global autocommit = 1")
tk2 := testkit.NewTestKit(c, s.store)
// TODO: In MySQL, the result is "autocommit ON".
tk2.MustQuery("show global variables where variable_name = 'autocommit'").Check(testkit.Rows("autocommit 1"))
tk2.MustQuery("show global variables where variable_name = 'autocommit'").Check(testkit.Rows("autocommit ON"))

// TODO: Specifying the charset for national char/varchar should not be supported.
tk.MustExec("drop table if exists test_full_column")
Expand Down
2 changes: 1 addition & 1 deletion expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4731,7 +4731,7 @@ func (s *testIntegrationSuite) TestForeignKeyVar(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("SET FOREIGN_KEY_CHECKS=1")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 8047 variable 'foreign_key_checks' does not yet support value: 1"))
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 8047 variable 'foreign_key_checks' does not yet support value: ON"))
}

func (s *testIntegrationSuite) TestUserVarMockWindFunc(c *C) {
Expand Down
3 changes: 2 additions & 1 deletion planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,8 @@ func (er *expressionRewriter) rewriteVariable(v *ast.VariableExpr) {
er.err = err
return
}
e := expression.DatumToConstant(types.NewStringDatum(val), mysql.TypeVarString)
nativeVal, nativeType := variable.GetNativeValType(name, val)
e := expression.DatumToConstant(nativeVal, nativeType)
e.GetType().Charset, _ = er.sctx.GetSessionVars().GetSystemVar(variable.CharacterSetConnection)
e.GetType().Collate, _ = er.sctx.GetSessionVars().GetSystemVar(variable.CollationConnection)
er.ctxStackAppend(e, types.EmptyName)
Expand Down
2 changes: 1 addition & 1 deletion session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ func doDMLWorks(s Session) {
vVal = strconv.Itoa(variable.DefTiDBRowFormatV2)
}
if v.Name == variable.TiDBEnableClusteredIndex {
vVal = "1"
vVal = variable.BoolOn
}
if v.Name == variable.TiDBPartitionPruneMode {
vVal = string(variable.StaticOnly)
Expand Down
2 changes: 1 addition & 1 deletion session/pessimistic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,7 @@ func (s *testPessimisticSuite) TestAmendTxnVariable(c *C) {
// Set off the global variable.
tk2.MustExec("set global tidb_enable_amend_pessimistic_txn = 0;")
tk4 := testkit.NewTestKitWithInit(c, s.store)
tk4.MustQuery(`show variables like "tidb_enable_amend_pessimistic_txn"`).Check(testkit.Rows("tidb_enable_amend_pessimistic_txn 0"))
tk4.MustQuery(`show variables like "tidb_enable_amend_pessimistic_txn"`).Check(testkit.Rows("tidb_enable_amend_pessimistic_txn OFF"))
tk4.MustExec("use test_db")
tk4.MustExec("begin pessimistic")
tk4.MustExec("insert into t1 values(5, 5, 5, 5)")
Expand Down
8 changes: 4 additions & 4 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2691,17 +2691,17 @@ func (s *testSessionSuite2) TestCommitRetryCount(c *C) {
func (s *testSessionSuite3) TestEnablePartition(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("set tidb_enable_table_partition=off")
tk.MustQuery("show variables like 'tidb_enable_table_partition'").Check(testkit.Rows("tidb_enable_table_partition off"))
tk.MustQuery("show variables like 'tidb_enable_table_partition'").Check(testkit.Rows("tidb_enable_table_partition OFF"))

tk.MustExec("set global tidb_enable_table_partition = on")

tk.MustQuery("show variables like 'tidb_enable_table_partition'").Check(testkit.Rows("tidb_enable_table_partition off"))
tk.MustQuery("show global variables like 'tidb_enable_table_partition'").Check(testkit.Rows("tidb_enable_table_partition on"))
tk.MustQuery("show variables like 'tidb_enable_table_partition'").Check(testkit.Rows("tidb_enable_table_partition OFF"))
tk.MustQuery("show global variables like 'tidb_enable_table_partition'").Check(testkit.Rows("tidb_enable_table_partition ON"))

// Disable global variable cache, so load global session variable take effect immediate.
s.dom.GetGlobalVarsCache().Disable()
tk1 := testkit.NewTestKitWithInit(c, s.store)
tk1.MustQuery("show variables like 'tidb_enable_table_partition'").Check(testkit.Rows("tidb_enable_table_partition on"))
tk1.MustQuery("show variables like 'tidb_enable_table_partition'").Check(testkit.Rows("tidb_enable_table_partition ON"))
}

func (s *testSessionSerialSuite) TestTxnRetryErrMsg(c *C) {
Expand Down
Loading

0 comments on commit 106b04d

Please sign in to comment.