MCPcopy
hub / github.com/pingcap/tidb / TestIssue42125

Function TestIssue42125

pkg/planner/core/plan_cache_test.go:950–992  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

948}
949
950func TestIssue42125(t *testing.T) {
951 store := testkit.CreateMockStore(t)
952 tk := testkit.NewTestKit(t, store)
953 tk.MustExec("use test")
954 tk.MustExec("create table t (a int, b int, c int, unique key(a, b))")
955
956 // should use BatchPointGet
957 tk.MustExec("prepare st from 'select * from t where a=1 and b in (?, ?)'")
958 tk.MustExec("set @a=1, @b=2")
959 tk.MustExec("execute st using @a, @b")
960 tkProcess := tk.Session().ShowProcess()
961 ps := []*util.ProcessInfo{tkProcess}
962 tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps})
963 rows := tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Rows()
964 require.Equal(t, rows[0][0], "Batch_Point_Get_5") // use BatchPointGet
965 tk.MustExec("execute st using @a, @b")
966 tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1105 skip prepared plan-cache: Batch/PointGet plans may be over-optimized"))
967
968 // should use PointGet: unsafe PointGet
969 tk.MustExec("prepare st from 'select * from t where a=1 and b>=? and b<=?'")
970 tk.MustExec("set @a=1, @b=1")
971 tk.MustExec("execute st using @a, @b")
972 tkProcess = tk.Session().ShowProcess()
973 ps = []*util.ProcessInfo{tkProcess}
974 tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps})
975 rows = tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Rows()
976 require.Equal(t, rows[0][0], "Point_Get_5") // use Point_Get_5
977 tk.MustExec("execute st using @a, @b")
978 tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0")) // cannot hit
979
980 // safe PointGet
981 tk.MustExec("prepare st from 'select * from t where a=1 and b=? and c<?'")
982 tk.MustExec("set @a=1, @b=1")
983 tk.MustExec("execute st using @a, @b")
984 tkProcess = tk.Session().ShowProcess()
985 ps = []*util.ProcessInfo{tkProcess}
986 tk.Session().SetSessionManager(&testkit.MockSessionManager{PS: ps})
987 rows = tk.MustQuery(fmt.Sprintf("explain for connection %d", tkProcess.ID)).Rows()
988 require.Contains(t, rows[0][0], "Selection") // PointGet -> Selection
989 require.Contains(t, rows[1][0], "Point_Get")
990 tk.MustExec("execute st using @a, @b")
991 tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) // can hit
992}
993
994func TestNonPreparedPlanExplainWarning(t *testing.T) {
995 store := testkit.CreateMockStore(t)

Callers

nothing calls this directly

Calls 12

MustExecMethod · 0.95
SessionMethod · 0.95
MustQueryMethod · 0.95
CreateMockStoreFunction · 0.92
NewTestKitFunction · 0.92
RowsFunction · 0.92
ShowProcessMethod · 0.65
SetSessionManagerMethod · 0.65
RowsMethod · 0.65
EqualMethod · 0.65
CheckMethod · 0.65
ContainsMethod · 0.65

Tested by

no test coverage detected