From 89a95dcf70c4309fdf4603e737a77b8812ad85d6 Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Wed, 15 Jul 2020 02:57:34 +0800 Subject: [PATCH] planner: don't put the handle column twice into the index column --- executor/join_test.go | 11 +++++++++++ planner/core/logical_plans.go | 13 +++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/executor/join_test.go b/executor/join_test.go index 86577c42dc9e3..bedd4572d4ab4 100644 --- a/executor/join_test.go +++ b/executor/join_test.go @@ -2090,3 +2090,14 @@ func (s *testSuiteJoinSerial) TestIssue18070(c *C) { err = tk.QueryToErr("select /*+ inl_merge_join(t1)*/ * from t1 join t2 on t1.a = t2.a;") c.Assert(strings.Contains(err.Error(), "Out Of Memory Quota!"), IsTrue) } + +func (s *testSuiteJoin1) TestIssue18564(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t1, t2") + tk.MustExec("create table t1(a int, b int, primary key(a), index idx(b,a));") + tk.MustExec("create table t2(a int, b int, primary key(a), index idx(b,a));") + tk.MustExec("insert into t1 values(1, 1)") + tk.MustExec("insert into t2 values(1, 1)") + tk.MustQuery("select /*+ INL_JOIN(t1) */ * from t1 FORCE INDEX (idx) join t2 on t1.b=t2.b and t1.a = t2.a").Check(testkit.Rows("1 1 1 1")) +} diff --git a/planner/core/logical_plans.go b/planner/core/logical_plans.go index 7a9e49ff4e999..cd77af26c8d7c 100644 --- a/planner/core/logical_plans.go +++ b/planner/core/logical_plans.go @@ -813,8 +813,17 @@ func (ds *DataSource) fillIndexPath(path *util.AccessPath, conds []expression.Ex if !path.Index.Unique && !path.Index.Primary && len(path.Index.Columns) == len(path.IdxCols) { handleCol := ds.getPKIsHandleCol() if handleCol != nil && !mysql.HasUnsignedFlag(handleCol.RetType.Flag) { - path.IdxCols = append(path.IdxCols, handleCol) - path.IdxColLens = append(path.IdxColLens, types.UnspecifiedLength) + alreadyHandle := false + for _, col := range path.IdxCols { + if col.ID == model.ExtraHandleID || col.Equal(nil, handleCol) { + alreadyHandle = true + } + } + // Don't add one column twice to the index. May cause unexpected errors. + if !alreadyHandle { + path.IdxCols = append(path.IdxCols, handleCol) + path.IdxColLens = append(path.IdxColLens, types.UnspecifiedLength) + } } } if len(path.IdxCols) != 0 {