Skip to content

Commit

Permalink
planner: add more test cases for auto-capture and support to capture …
Browse files Browse the repository at this point in the history
…more hints (#33051)
  • Loading branch information
qw4990 authored Mar 15, 2022
1 parent caad839 commit 00e9622
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion bindinfo/capture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -943,12 +943,20 @@ func TestCaptureHints(t *testing.T) {
{"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`)"},
{"select /*+ ignore_index(t, a) */ * from t where a=1", "ignore_index(`t` `a`)"},
// push-down hints
{"select /*+ limit_to_cop() */ * from t limit 10", "limit_to_cop()"},
{"select /*+ agg_to_cop() */ a, count(*) from t group by a", "agg_to_cop()"},
// index-merge hints
{"select /*+ no_index_merge() */ a, b from t where a>1 or b>1", "no_index_merge()"},
{"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`)"},
// runtime hints
{"select /*+ memory_quota(1024 MB) */ * from t", "memory_quota(1024 mb)"},
{"select /*+ max_execution_time(1000) */ * from t", "max_execution_time(1000)"},
// storage hints
// query-blocks
{"select /*+ read_from_storage(tikv[t]) */ * from t", "read_from_storage(tikv[`t`])"},
// others
{"select /*+ use_toja(true) */ t1.a, t1.b from t t1 where t1.a in (select t2.a from t t2)", "use_toja(true)"},
}
for _, capCase := range captureCases {
stmtsummary.StmtSummaryByDigestMap.Clear()
Expand Down
11 changes: 11 additions & 0 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,17 @@ func getEncodedPlan(sctx sessionctx.Context, p plannercore.Plan, genHint bool) (
}
if genHint {
hints := plannercore.GenHintsFromPhysicalPlan(p)
for _, tableHint := range sctx.GetSessionVars().StmtCtx.OriginalTableHints {
// some hints like 'memory_quota' cannot be extracted from the PhysicalPlan directly,
// so we have to iterate all hints from the customer and keep some other necessary hints.
switch tableHint.HintName.L {
case "memory_quota", "use_toja", "no_index_merge", "max_execution_time",
plannercore.HintAggToCop, plannercore.HintIgnoreIndex,
plannercore.HintReadFromStorage, plannercore.HintLimitToCop:
hints = append(hints, tableHint)
}
}

hintStr = hint.RestoreOptimizerHints(hints)
sctx.GetSessionVars().StmtCtx.SetPlanHint(hintStr)
}
Expand Down
1 change: 1 addition & 0 deletions planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ func handleStmtHints(hints []*ast.TableOptimizerHint) (stmtHints stmtctx.StmtHin
setVarsOffs = append(setVarsOffs, i)
}
}
stmtHints.OriginalTableHints = hints
stmtHints.SetVars = setVars

// Handle MEMORY_QUOTA
Expand Down
4 changes: 4 additions & 0 deletions sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/util/disk"
Expand Down Expand Up @@ -263,6 +264,9 @@ type StmtHints struct {
HasMaxExecutionTime bool
HasEnableCascadesPlannerHint bool
SetVars map[string]string

// the original table hints
OriginalTableHints []*ast.TableOptimizerHint
}

// TaskMapNeedBackUp indicates that whether we need to back up taskMap during physical optimizing.
Expand Down

0 comments on commit 00e9622

Please sign in to comment.