Skip to content

Commit

Permalink
planner: add more test cases for auto-capture (#32994)
Browse files Browse the repository at this point in the history
ref #32466
  • Loading branch information
qw4990 authored Mar 14, 2022
1 parent 9fcc6b0 commit bf3be20
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
55 changes: 55 additions & 0 deletions bindinfo/capture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,3 +906,58 @@ func TestCaptureFilter(t *testing.T) {
require.Len(t, rows, 1)
require.Equal(t, "select * from `mysql` . `capture_plan_baselines_blacklist`", rows[0][0])
}

func TestCaptureHints(t *testing.T) {
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("SET GLOBAL tidb_capture_plan_baselines = on")
defer func() {
tk.MustExec("SET GLOBAL tidb_capture_plan_baselines = off")
}()
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(pk int primary key, a int, b int, key(a), key(b))")
require.True(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil))

captureCases := []struct {
query string
hint string
}{
// agg hints
{"select /*+ hash_agg() */ count(1) from t", "hash_agg"},
{"select /*+ stream_agg() */ count(1) from t", "stream_agg"},
// join hints
{"select /*+ merge_join(t1, t2) */ * from t t1, t t2 where t1.a=t2.a", "merge_join"},
{"select /*+ tidb_smj(t1, t2) */ * from t t1, t t2 where t1.a=t2.a", "merge_join"},
{"select /*+ hash_join(t1, t2) */ * from t t1, t t2 where t1.a=t2.a", "hash_join"},
{"select /*+ tidb_hj(t1, t2) */ * from t t1, t t2 where t1.a=t2.a", "hash_join"},
{"select /*+ inl_join(t1, t2) */ * from t t1, t t2 where t1.a=t2.a", "inl_join"},
{"select /*+ tidb_inlj(t1, t2) */ * from t t1, t t2 where t1.a=t2.a", "inl_join"},
{"select /*+ inl_hash_join(t1, t2) */ * from t t1, t t2 where t1.a=t2.a", "inl_hash_join"},
// index hints
{"select * from t use index(primary)", "use_index(@`sel_1` `test`.`t` )"},
{"select /*+ use_index(primary) */ * from t", "use_index(@`sel_1` `test`.`t` )"},
{"select * from t use index(a)", "use_index(@`sel_1` `test`.`t` `a`)"},
{"select /*+ use_index(a) */ * from t use index(a)", "use_index(@`sel_1` `test`.`t` `a`)"},
{"select * from t use index(b)", "use_index(@`sel_1` `test`.`t` `b`)"},
{"select /*+ use_index(b) */ * from t use index(b)", "use_index(@`sel_1` `test`.`t` `b`)"},
{"select /*+ use_index_merge(t, a, b) */ a, b from t where a=1 or b=1", "use_index_merge(@`sel_1` `t` `a`, `b`)"},
// push-down hints
// index-merge hints
// runtime hints
// storage hints
// query-blocks
// others
}
for _, capCase := range captureCases {
stmtsummary.StmtSummaryByDigestMap.Clear()
utilCleanBindingEnv(tk, dom)
tk.MustExec(capCase.query)
tk.MustExec(capCase.query)
tk.MustExec("admin capture bindings")
res := tk.MustQuery(`show global bindings`).Rows()
require.Equal(t, len(res), 1) // this query is captured, and
require.True(t, strings.Contains(res[0][1].(string), capCase.hint)) // the binding contains the expected hint
}
}
7 changes: 2 additions & 5 deletions planner/core/hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ func getTableName(tblName model.CIStr, asName *model.CIStr) model.CIStr {
}

func extractTableAsName(p PhysicalPlan) (*model.CIStr, *model.CIStr) {
_, isProj := p.(*PhysicalProjection)
_, isUnionScan := p.(*PhysicalUnionScan)
if isProj || isUnionScan {
return extractTableAsName(p.Children()[0])
}
if len(p.Children()) > 1 {
return nil, nil
}
Expand All @@ -76,6 +71,8 @@ func extractTableAsName(p PhysicalPlan) (*model.CIStr, *model.CIStr) {
return &is.DBName, is.TableAsName
}
return &is.DBName, &is.Table.Name
case *PhysicalSort, *PhysicalSelection, *PhysicalUnionScan, *PhysicalProjection:
return extractTableAsName(p.Children()[0])
}
return nil, nil
}
Expand Down

0 comments on commit bf3be20

Please sign in to comment.