Skip to content

Commit

Permalink
planner: more test cases for the universal binding (#49598)
Browse files Browse the repository at this point in the history
ref #48875
  • Loading branch information
qw4990 authored Dec 20, 2023
1 parent da460f1 commit 91fad01
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/bindinfo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ go_test(
embed = [":bindinfo"],
flaky = True,
race = "on",
shard_count = 48,
shard_count = 49,
deps = [
"//pkg/bindinfo/internal",
"//pkg/config",
Expand Down
24 changes: 24 additions & 0 deletions pkg/bindinfo/universal_binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,30 @@ func TestUniversalBindingSwitch(t *testing.T) {
tk3.MustQuery(`show global variables like 'tidb_opt_enable_universal_binding'`).Check(testkit.Rows("tidb_opt_enable_universal_binding OFF"))
}

func TestUniversalBindingSetVar(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t (a int, b int, key(a), key(b))`)
tk.MustExec(`create universal binding using select /*+ use_index(t, a) */ * from t`)

tk.MustExec(`set @@tidb_opt_enable_universal_binding=0`)
tk.MustExec(`select * from t`)
tk.MustQuery(`select @@last_plan_from_binding`).Check(testkit.Rows("0"))
tk.MustExec(`select /*+ set_var(tidb_opt_enable_universal_binding=1) */ * from t`)
tk.MustQuery(`select @@last_plan_from_binding`).Check(testkit.Rows("1"))
tk.MustExec(`select /*+ set_var(tidb_opt_enable_universal_binding=0) */ * from t`)
tk.MustQuery(`select @@last_plan_from_binding`).Check(testkit.Rows("0"))

tk.MustExec(`set @@tidb_opt_enable_universal_binding=1`)
tk.MustExec(`select * from t`)
tk.MustQuery(`select @@last_plan_from_binding`).Check(testkit.Rows("1"))
tk.MustExec(`select /*+ set_var(tidb_opt_enable_universal_binding=0) */ * from t`)
tk.MustQuery(`select @@last_plan_from_binding`).Check(testkit.Rows("0"))
tk.MustExec(`select /*+ set_var(tidb_opt_enable_universal_binding=1) */ * from t`)
tk.MustQuery(`select @@last_plan_from_binding`).Check(testkit.Rows("1"))
}

func TestUniversalBindingDBInHints(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down
59 changes: 59 additions & 0 deletions pkg/session/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2245,3 +2245,62 @@ func TestTiDBUpgradeToVer179(t *testing.T) {

dom.Close()
}

func TestTiDBUpgradeToVer181(t *testing.T) {
store, _ := CreateStoreAndBootstrap(t)
defer func() {
require.NoError(t, store.Close())
}()

// init to v180
ver180 := version180
seV180 := CreateSessionAndSetID(t, store)
txn, err := store.Begin()
require.NoError(t, err)
m := meta.NewMeta(txn)
err = m.FinishBootstrap(int64(ver180))
require.NoError(t, err)
MustExec(t, seV180, fmt.Sprintf("update mysql.tidb set variable_value=%d where variable_name='tidb_server_version'", ver180))
err = txn.Commit(context.Background())
require.NoError(t, err)
unsetStoreBootstrapped(store.UUID())

// create some bindings at v180
MustExec(t, seV180, "use test")
MustExec(t, seV180, "create table t (a int, b int, key(a), key(b))")
MustExec(t, seV180, `create global binding using select /*+ use_index(t, a) */ * from t where a=1`)
MustExec(t, seV180, `create global binding using select /*+ use_index(t, b) */ * from t where b=1`)

// upgrade to ver181
domCurVer, err := BootstrapSession(store)
require.NoError(t, err)
defer domCurVer.Close()
seCurVer := CreateSessionAndSetID(t, store)
ver, err := getBootstrapVersion(seCurVer)
require.NoError(t, err)
require.Equal(t, currentBootstrapVersion, ver)

// the default value of `tidb_opt_enable_universal_binding` should be `off`.
res := MustExecToRecodeSet(t, seCurVer, `show variables like 'tidb_opt_enable_universal_binding'`)
chk := res.NewChunk(nil)
require.NoError(t, res.Next(context.Background(), chk))
require.Equal(t, chk.NumRows(), 1)
require.Equal(t, chk.GetRow(0).GetString(1), "OFF")
require.NoError(t, res.Close())

res = MustExecToRecodeSet(t, seCurVer, `show global variables like 'tidb_opt_enable_universal_binding'`)
chk = res.NewChunk(nil)
require.NoError(t, res.Next(context.Background(), chk))
require.Equal(t, chk.NumRows(), 1)
require.Equal(t, chk.GetRow(0).GetString(1), "OFF")
require.NoError(t, res.Close())

// a new column `type` in `mysql.bind_info` and all previous bindings values are empty ''.
res = MustExecToRecodeSet(t, seCurVer, `select type from mysql.bind_info where source!='builtin'`)
chk = res.NewChunk(nil)
require.NoError(t, res.Next(context.Background(), chk))
require.Equal(t, chk.NumRows(), 2) // 2 bindings
require.Equal(t, chk.GetRow(0).GetString(0), "") // it means this binding is a normal binding
require.Equal(t, chk.GetRow(1).GetString(0), "")
require.NoError(t, res.Close())
}
2 changes: 1 addition & 1 deletion pkg/sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ var defaultSysVars = []*SysVar{
s.EnableNonPreparedPlanCacheForDML = TiDBOptOn(val)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptEnableUniversalBinding, Value: BoolToOnOff(false), Type: TypeBool, SetSession: func(s *SessionVars, val string) error {
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptEnableUniversalBinding, Value: BoolToOnOff(false), Type: TypeBool, IsHintUpdatableVerfied: true, SetSession: func(s *SessionVars, val string) error {
s.EnableUniversalBinding = TiDBOptOn(val)
return nil
}},
Expand Down

0 comments on commit 91fad01

Please sign in to comment.