From 7dca6a579ffdbd580fa380948df1132ee1ec4801 Mon Sep 17 00:00:00 2001 From: foreyes Date: Fri, 6 Sep 2019 13:28:02 +0800 Subject: [PATCH 1/3] add & test ignore_index hint --- go.mod | 2 ++ go.sum | 4 +-- planner/core/logical_plan_builder.go | 19 +++++++++++--- planner/core/physical_plan_test.go | 39 +++++++++++++++++++++++----- 4 files changed, 53 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index b4abc607480cf..0f3e67a8acaa3 100644 --- a/go.mod +++ b/go.mod @@ -75,3 +75,5 @@ require ( sourcegraph.com/sourcegraph/appdash v0.0.0-20180531100431-4c381bd170b4 sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67 ) + +replace github.com/pingcap/parser => github.com/foreyes/parser v0.0.0-20190906050255-855c28e0407b diff --git a/go.sum b/go.sum index 2203b85737615..8f7a260cd2761 100644 --- a/go.sum +++ b/go.sum @@ -47,6 +47,8 @@ github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25Kn github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 h1:clC1lXBpe2kTj2VHdaIu9ajZQe4kcEY9j0NsnDDBZ3o= github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/foreyes/parser v0.0.0-20190906050255-855c28e0407b h1:uCOxDlOe79Ym3w8xHfKCNmuBVL7+Lrwo2n8t0AQUjxo= +github.com/foreyes/parser v0.0.0-20190906050255-855c28e0407b/go.mod h1:3lbW3qHt81o5eguDSsve9wLN8OtUzvdZrVQ/pngnIg0= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 h1:Mn26/9ZMNWSw9C9ERFA1PUxfmGpolnw2v0bKOREu5ew= @@ -167,8 +169,6 @@ github.com/pingcap/kvproto v0.0.0-20190821201150-798d27658fae/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-20190903084634-0daf3f706c76 h1:q8d5NIRT/Urmb5woYWhlrMER8nDV33tjyvJMqODI2Rk= -github.com/pingcap/parser v0.0.0-20190903084634-0daf3f706c76/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= diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 70e40c67c55e5..13e26b4151435 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -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" ) const ( @@ -1972,7 +1974,7 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, nodeType n preferAggType |= preferHashAgg case HintStreamAgg: preferAggType |= preferStreamAgg - case HintIndex: + case HintUseIndex: if len(hint.Tables) != 0 && len(hint.Indexes) != 0 { indexHintList = append(indexHintList, indexHintInfo{ tblName: hint.Tables[0].TableName, @@ -1983,6 +1985,17 @@ func (b *PlanBuilder) pushTableHints(hints []*ast.TableOptimizerHint, nodeType n }, }) } + case HintIgnoreIndex: + if len(hint.Tables) != 0 && len(hint.Indexes) != 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 } diff --git a/planner/core/physical_plan_test.go b/planner/core/physical_plan_test.go index f43f8159d1d8b..be03dc5af1f5e 100644 --- a/planner/core/physical_plan_test.go +++ b/planner/core/physical_plan_test.go @@ -1747,34 +1747,59 @@ func (s *testPlanSuite) TestIndexHint(c *C) { }{ // simple case { - sql: "select /*+ INDEX(t, c_d_e) */ * from t", + sql: "select /*+ USE_INDEX(t, c_d_e) */ * from t", best: "IndexLookUp(Index(t.c_d_e)[[NULL,+inf]], Table(t))", hasWarn: false, }, { - sql: "select /*+ INDEX(t, c_d_e) */ * from t t1", + sql: "select /*+ IGNORE_INDEX(t, c_d_e) */ c from t order by c", + best: "TableReader(Table(t))->Sort", + hasWarn: false, + }, + { + sql: "select /*+ USE_INDEX(t, c_d_e) */ * from t t1", best: "TableReader(Table(t))", hasWarn: false, }, { - sql: "select /*+ INDEX(t1, c_d_e) */ * from t t1", + sql: "select /*+ IGNORE_INDEX(t, c_d_e) */ t1.c from t t1 order by t1.c", + best: "IndexReader(Index(t.c_d_e)[[NULL,+inf]])", + hasWarn: false, + }, + { + sql: "select /*+ USE_INDEX(t1, c_d_e) */ * from t t1", best: "IndexLookUp(Index(t.c_d_e)[[NULL,+inf]], Table(t))", hasWarn: false, }, { - sql: "select /*+ INDEX(t1, c_d_e), INDEX(t2, f) */ * from t t1, t t2 where t1.a = t2.b", + sql: "select /*+ IGNORE_INDEX(t1, c_d_e) */ t1.c from t t1 order by t1.c", + best: "TableReader(Table(t))->Sort", + hasWarn: false, + }, + { + sql: "select /*+ USE_INDEX(t1, c_d_e), USE_INDEX(t2, f), HASH_JOIN(t1) */ * from t t1, t t2 where t1.a = t2.b", best: "LeftHashJoin{IndexLookUp(Index(t.c_d_e)[[NULL,+inf]], Table(t))->IndexLookUp(Index(t.f)[[NULL,+inf]], Table(t))}(test.t1.a,test.t2.b)", hasWarn: false, }, + { + sql: "select /*+ IGNORE_INDEX(t1, c_d_e), IGNORE_INDEX(t2, f), HASH_JOIN(t1) */ * from t t1, t t2 where t1.a = t2.b", + best: "LeftHashJoin{TableReader(Table(t))->TableReader(Table(t))}(test.t1.a,test.t2.b)", + hasWarn: false, + }, // test multiple indexes { - sql: "select /*+ INDEX(t, c_d_e, f, g) */ * from t order by f", + sql: "select /*+ USE_INDEX(t, c_d_e, f, g) */ * from t order by f", best: "IndexLookUp(Index(t.f)[[NULL,+inf]], Table(t))", hasWarn: false, }, // there will be a warning instead of error when index not exist { - sql: "select /*+ INDEX(t, no_such_index) */ * from t", + sql: "select /*+ USE_INDEX(t, no_such_index) */ * from t", + best: "TableReader(Table(t))", + hasWarn: true, + }, + { + sql: "select /*+ IGNORE_INDEX(t, no_such_index) */ * from t", best: "TableReader(Table(t))", hasWarn: true, }, @@ -1782,6 +1807,8 @@ func (s *testPlanSuite) TestIndexHint(c *C) { ctx := context.Background() for i, test := range tests { comment := Commentf("case:%v sql:%s", i, test) + se.GetSessionVars().StmtCtx.SetWarnings(nil) + stmt, err := s.ParseOneStmt(test.sql, "", "") c.Assert(err, IsNil, comment) From 499f232476d33f1c4b29f27926d78ba2cd811a97 Mon Sep 17 00:00:00 2001 From: foreyes Date: Wed, 11 Sep 2019 20:47:48 +0800 Subject: [PATCH 2/3] add more tests --- planner/core/physical_plan_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/planner/core/physical_plan_test.go b/planner/core/physical_plan_test.go index be03dc5af1f5e..b9a97d0897c0f 100644 --- a/planner/core/physical_plan_test.go +++ b/planner/core/physical_plan_test.go @@ -1803,6 +1803,27 @@ func (s *testPlanSuite) TestIndexHint(c *C) { best: "TableReader(Table(t))", hasWarn: true, }, + // use both use_index and ignore_index, same as index hints in sql. + { + sql: "select /*+ use_index(t, c_d_e), ignore_index(t, f)*/ c from t order by c", + best: "IndexReader(Index(t.c_d_e)[[NULL,+inf]])", + hasWarn: false, + }, + { + sql: "select /*+ use_index(t, f), ignore_index(t, f)*/ c from t order by c", + best: "TableReader(Table(t))->Sort", + hasWarn: false, + }, + { + sql: "select /*+ use_index(t, c_d_e), ignore_index(t, c_d_e)*/ c from t order by c", + best: "TableReader(Table(t))->Sort", + hasWarn: false, + }, + { + sql: "select /*+ use_index(t, c_d_e, f), ignore_index(t, c_d_e)*/ c from t order by c", + best: "IndexLookUp(Index(t.f)[[NULL,+inf]], Table(t))->Sort", + hasWarn: false, + }, } ctx := context.Background() for i, test := range tests { From 361815e717432a311e7f6600488eb194627960e8 Mon Sep 17 00:00:00 2001 From: foreyes Date: Thu, 12 Sep 2019 11:31:25 +0800 Subject: [PATCH 3/3] update parser --- go.mod | 4 +--- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index c9138a8167941..a885806e062ff 100644 --- a/go.mod +++ b/go.mod @@ -41,7 +41,7 @@ require ( github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e github.com/pingcap/kvproto v0.0.0-20190904075355-9a1bd6a31da2 github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596 - github.com/pingcap/parser v0.0.0-20190903084634-0daf3f706c76 + 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-20190806070524-16909e03435e @@ -75,5 +75,3 @@ require ( sourcegraph.com/sourcegraph/appdash v0.0.0-20180531100431-4c381bd170b4 sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67 ) - -replace github.com/pingcap/parser => github.com/foreyes/parser v0.0.0-20190906050255-855c28e0407b diff --git a/go.sum b/go.sum index cca9b0b050ab2..e69b36f02071e 100644 --- a/go.sum +++ b/go.sum @@ -45,8 +45,6 @@ github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25Kn github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 h1:clC1lXBpe2kTj2VHdaIu9ajZQe4kcEY9j0NsnDDBZ3o= github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= -github.com/foreyes/parser v0.0.0-20190906050255-855c28e0407b h1:uCOxDlOe79Ym3w8xHfKCNmuBVL7+Lrwo2n8t0AQUjxo= -github.com/foreyes/parser v0.0.0-20190906050255-855c28e0407b/go.mod h1:3lbW3qHt81o5eguDSsve9wLN8OtUzvdZrVQ/pngnIg0= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 h1:Mn26/9ZMNWSw9C9ERFA1PUxfmGpolnw2v0bKOREu5ew= @@ -166,6 +164,8 @@ github.com/pingcap/kvproto v0.0.0-20190904075355-9a1bd6a31da2/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-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=