Skip to content

Commit

Permalink
planner: add IGNORE_INDEX hint (pingcap#12059)
Browse files Browse the repository at this point in the history
  • Loading branch information
foreyes committed Sep 16, 2019
1 parent 57c8267 commit 3f6d325
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ require (
github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e
github.com/pingcap/kvproto v0.0.0-20190703131923-d9830856b531
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596
github.com/pingcap/parser v0.0.0-20190828080649-a621a0f9b06c
github.com/pingcap/parser v0.0.0-20190912032624-978b8272c04e
github.com/pingcap/pd v0.0.0-20190712044914-75a1f9f3062b
github.com/pingcap/tidb-tools v2.1.3-0.20190321065848-1e8b48f5c168+incompatible
github.com/pingcap/tipb v0.0.0-20190428032612-535e1abaa330
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ github.com/pingcap/kvproto v0.0.0-20190703131923-d9830856b531/go.mod h1:QMdbTAXC
github.com/pingcap/log v0.0.0-20190214045112-b37da76f67a7/go.mod h1:xsfkWVaFVV5B8e1K9seWfyJWFrIhbtUTAD8NV1Pq3+w=
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596 h1:t2OQTpPJnrPDGlvA+3FwJptMTt6MEPdzK1Wt99oaefQ=
github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596/go.mod h1:WpHUKhNZ18v116SvGrmjkA9CBhYmuUTKL+p8JC9ANEw=
github.com/pingcap/parser v0.0.0-20190828080649-a621a0f9b06c h1:VcQ60QkH7Vv98dbedNKDEO7RfUw+6TE59r/lQokNz/k=
github.com/pingcap/parser v0.0.0-20190828080649-a621a0f9b06c/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
github.com/pingcap/parser v0.0.0-20190912032624-978b8272c04e h1:QeD1wC7bGElAhufSHH4JcIbs1cVdxnGWD3n3gcE5qeY=
github.com/pingcap/parser v0.0.0-20190912032624-978b8272c04e/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
github.com/pingcap/pd v0.0.0-20190712044914-75a1f9f3062b h1:oS9PftxQqgcRouKhhdaB52tXhVLEP7Ng3Qqsd6Z18iY=
github.com/pingcap/pd v0.0.0-20190712044914-75a1f9f3062b/go.mod h1:3DlDlFT7EF64A1bmb/tulZb6wbPSagm5G4p1AlhaEDs=
github.com/pingcap/tidb-tools v2.1.3-0.20190321065848-1e8b48f5c168+incompatible h1:MkWCxgZpJBgY2f4HtwWMMFzSBb3+JPzeJgF3VrXE/bU=
Expand Down
19 changes: 16 additions & 3 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ const (
HintHashAgg = "hash_agg"
// HintStreamAgg is hint enforce stream aggregation.
HintStreamAgg = "stream_agg"
// HintIndex is hint enforce using some indexes.
HintIndex = "index"
// HintUseIndex is hint enforce using some indexes.
HintUseIndex = "use_index"
// HintIgnoreIndex is hint enforce ignoring some indexes.
HintIgnoreIndex = "ignore_index"
// HintAggToCop is hint enforce pushing aggregation to coprocessor.
HintAggToCop = "agg_to_cop"
)
Expand Down Expand Up @@ -1967,7 +1969,7 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, nodeType n
aggHints.preferAggType |= preferStreamAgg
case HintAggToCop:
aggHints.preferAggToCop = true
case HintIndex:
case HintUseIndex:
if len(hint.Tables) != 0 {
indexHintList = append(indexHintList, indexHintInfo{
tblName: hint.Tables[0].TableName,
Expand All @@ -1978,6 +1980,17 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, nodeType n
},
})
}
case HintIgnoreIndex:
if len(hint.Tables) != 0 {
indexHintList = append(indexHintList, indexHintInfo{
tblName: hint.Tables[0].TableName,
indexHint: &ast.IndexHint{
IndexNames: hint.Indexes,
HintType: ast.HintIgnore,
HintScope: ast.HintForScan,
},
})
}
default:
// ignore hints that not implemented
}
Expand Down
12 changes: 7 additions & 5 deletions planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1687,20 +1687,20 @@ func (s *testPlanSuite) TestAggToCopHint(c *C) {
warning string
}{
{
sql: "select /*+ AGG_TO_COP(), HASH_AGG(), INDEX(t) */ sum(a) from t group by a",
sql: "select /*+ AGG_TO_COP(), HASH_AGG(), USE_INDEX(t) */ sum(a) from t group by a",
best: "TableReader(Table(t)->HashAgg)->HashAgg",
},
{
sql: "select /*+ AGG_TO_COP(), INDEX(t) */ sum(b) from t group by b",
sql: "select /*+ AGG_TO_COP(), USE_INDEX(t) */ sum(b) from t group by b",
best: "TableReader(Table(t)->HashAgg)->HashAgg",
},
{
sql: "select /*+ AGG_TO_COP(), HASH_AGG(), INDEX(t) */ distinct a from t group by a",
sql: "select /*+ AGG_TO_COP(), HASH_AGG(), USE_INDEX(t) */ distinct a from t group by a",
best: "TableReader(Table(t)->HashAgg)->HashAgg->HashAgg",
warning: "[planner:1815]Optimizer Hint AGG_TO_COP is inapplicable",
},
{
sql: "select /*+ AGG_TO_COP(), HASH_AGG(), HASH_JOIN(t1), INDEX(t1), INDEX(t2) */ sum(t1.a) from t t1, t t2 where t1.a = t2.b group by t1.a",
sql: "select /*+ AGG_TO_COP(), HASH_AGG(), HASH_JOIN(t1), USE_INDEX(t1), USE_INDEX(t2) */ sum(t1.a) from t t1, t t2 where t1.a = t2.b group by t1.a",
best: "LeftHashJoin{TableReader(Table(t))->TableReader(Table(t))}(test.t1.a,test.t2.b)->Projection->HashAgg",
warning: "[planner:1815]Optimizer Hint AGG_TO_COP is inapplicable",
},
Expand Down Expand Up @@ -1836,7 +1836,9 @@ func (s *testPlanSuite) TestIndexHint(c *C) {
ctx := context.Background()
for i, test := range tests {
comment := Commentf("case:%v sql:%s", i, test)
stmt, err := s.ParseOneStmt(test.sql, "", "")
se.GetSessionVars().StmtCtx.SetWarnings(nil)

stmt, err := s.ParseOneStmt(test, "", "")
c.Assert(err, IsNil, comment)

p, err := planner.Optimize(ctx, se, stmt, s.is)
Expand Down

0 comments on commit 3f6d325

Please sign in to comment.