Skip to content

Commit

Permalink
sessionctx: add testcase for set_var hint (#46784)
Browse files Browse the repository at this point in the history
ref #45892
  • Loading branch information
winoros authored Sep 8, 2023
1 parent 58a2dad commit 968c712
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2536,7 +2536,12 @@ func (s *SessionVars) SetSystemVarWithOldValAsRet(name string, val string) (stri
if err != nil {
return "", err
}
oldV := sv.Value
// The map s.systems[sv.Name] is lazy initialized. If we directly read it, we might read empty result.
// Since this code path is not a hot path, we directly call GetSessionOrGlobalSystemVar to get the value safely.
oldV, err := s.GetSessionOrGlobalSystemVar(context.Background(), sv.Name)
if err != nil {
return "", err
}
return oldV, sv.SetSessionFromHook(s, val)
}

Expand Down
20 changes: 20 additions & 0 deletions sessionctx/variable/setvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,26 @@ func TestSetVarNonStringOrEnum(t *testing.T) {
tk.MustQuery(fmt.Sprintf("select @@%v", c.varName)).Check(r)
}
}

tk.MustQuery("select @@max_execution_time").Check(testkit.Rows("0"))
tk.MustExec("set @@max_execution_time=1000")
tk.MustQuery("select @@max_execution_time").Check(testkit.Rows("1000"))
tk.MustQuery("select /*+ set_var(max_execution_time=100) */ @@max_execution_time").Check(testkit.Rows("100"))
// The value is the changed value 1000, not the default value 0.
tk.MustQuery("select @@max_execution_time").Check(testkit.Rows("1000"))

tk.MustExec("set @@global.max_execution_time=1000")

tk2 := testkit.NewTestKit(t, store)
tk2.MustQuery("select @@max_execution_time").Check(testkit.Rows("1000"))
tk2.MustQuery("select /*+ set_var(max_execution_time=100) */ @@max_execution_time").Check(testkit.Rows("100"))
// The value is the global value 1000, not the default value 0.
tk2.MustQuery("select @@max_execution_time").Check(testkit.Rows("1000"))
tk2.MustExec("set @@max_execution_time=2000")
tk2.MustQuery("select @@max_execution_time").Check(testkit.Rows("2000"))
tk2.MustQuery("select /*+ set_var(max_execution_time=100) */ @@max_execution_time").Check(testkit.Rows("100"))
// The value is the changed value 2000, not the default value 0 or the global value 1000.
tk2.MustQuery("select @@max_execution_time").Check(testkit.Rows("2000"))
}

func TestSetVarStringOrEnum(t *testing.T) {
Expand Down

0 comments on commit 968c712

Please sign in to comment.