From 91fad016c38db82be0874376932545922d05e059 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Wed, 20 Dec 2023 17:58:53 +0800 Subject: [PATCH] planner: more test cases for the universal binding (#49598) ref pingcap/tidb#48875 --- pkg/bindinfo/BUILD.bazel | 2 +- pkg/bindinfo/universal_binding_test.go | 24 +++++++++++ pkg/session/bootstrap_test.go | 59 ++++++++++++++++++++++++++ pkg/sessionctx/variable/sysvar.go | 2 +- 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/pkg/bindinfo/BUILD.bazel b/pkg/bindinfo/BUILD.bazel index d1f80a9559861..3db972d32a389 100644 --- a/pkg/bindinfo/BUILD.bazel +++ b/pkg/bindinfo/BUILD.bazel @@ -59,7 +59,7 @@ go_test( embed = [":bindinfo"], flaky = True, race = "on", - shard_count = 48, + shard_count = 49, deps = [ "//pkg/bindinfo/internal", "//pkg/config", diff --git a/pkg/bindinfo/universal_binding_test.go b/pkg/bindinfo/universal_binding_test.go index f424ca7797448..14451721482d0 100644 --- a/pkg/bindinfo/universal_binding_test.go +++ b/pkg/bindinfo/universal_binding_test.go @@ -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) diff --git a/pkg/session/bootstrap_test.go b/pkg/session/bootstrap_test.go index 2edafb93b0b76..7e403b6488d61 100644 --- a/pkg/session/bootstrap_test.go +++ b/pkg/session/bootstrap_test.go @@ -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()) +} diff --git a/pkg/sessionctx/variable/sysvar.go b/pkg/sessionctx/variable/sysvar.go index cbfa837fe52e3..e64c0a69b8a18 100644 --- a/pkg/sessionctx/variable/sysvar.go +++ b/pkg/sessionctx/variable/sysvar.go @@ -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 }},