Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: fix index join error when join key is ENUM or SET (#19235) #19413

Merged
merged 4 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions executor/index_lookup_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (e *IndexLookUpJoin) getFinishedTask(ctx context.Context) (*lookUpJoinTask,
select {
case task = <-e.resultCh:
case <-ctx.Done():
return nil, nil
return nil, ctx.Err()
}
if task == nil {
return nil, nil
Expand All @@ -308,7 +308,7 @@ func (e *IndexLookUpJoin) getFinishedTask(ctx context.Context) (*lookUpJoinTask,
return nil, err
}
case <-ctx.Done():
return nil, nil
return nil, ctx.Err()
}

e.task = task
Expand Down Expand Up @@ -545,6 +545,9 @@ func (iw *innerWorker) constructDatumLookupKey(task *lookUpJoinTask, rowIdx int)
if terror.ErrorEqual(err, types.ErrOverflow) {
return nil, nil
}
if terror.ErrorEqual(err, types.ErrTruncated) && (innerColType.Tp == mysql.TypeSet || innerColType.Tp == mysql.TypeEnum) {
return nil, nil
}
return nil, err
}
cmp, err := outerValue.CompareDatum(sc, &innerValue)
Expand Down Expand Up @@ -606,6 +609,11 @@ func (iw *innerWorker) fetchInnerResults(ctx context.Context, task *lookUpJoinTa
innerResult.GetMemTracker().SetLabel(innerResultLabel)
innerResult.GetMemTracker().AttachTo(task.memTracker)
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
err := Next(ctx, innerExec, iw.executorChk)
if err != nil {
return err
Expand Down
39 changes: 39 additions & 0 deletions executor/index_lookup_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package executor_test

import (
"context"
"fmt"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/util/testkit"
Expand Down Expand Up @@ -179,3 +180,41 @@ func (s *testSuite2) TestIndexJoinMultiCondition(c *C) {
tk.MustExec("insert into t2 values (0,1), (0,2), (0,3)")
tk.MustQuery("select /*+ TIDB_INLJ(t1) */ count(*) from t1, t2 where t1.a = t2.a and t1.b < t2.b").Check(testkit.Rows("3"))
}

func (s *testSuite2) TestIndexJoinEnumSetIssue19233(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec("drop table if exists i;")
tk.MustExec("drop table if exists p1;")
tk.MustExec("drop table if exists p2;")
tk.MustExec(`CREATE TABLE p1 (type enum('HOST_PORT') NOT NULL, UNIQUE KEY (type)) ;`)
tk.MustExec(`CREATE TABLE p2 (type set('HOST_PORT') NOT NULL, UNIQUE KEY (type)) ;`)
tk.MustExec(`CREATE TABLE i (objectType varchar(64) NOT NULL);`)
tk.MustExec(`insert into i values ('SWITCH');`)
tk.MustExec(`create table t like i;`)
tk.MustExec(`insert into t values ('HOST_PORT');`)
tk.MustExec(`insert into t select * from t;`)
tk.MustExec(`insert into t select * from t;`)
tk.MustExec(`insert into t select * from t;`)
tk.MustExec(`insert into t select * from t;`)
tk.MustExec(`insert into t select * from t;`)
tk.MustExec(`insert into t select * from t;`)

tk.MustExec(`insert into i select * from t;`)

tk.MustExec(`insert into p1 values('HOST_PORT');`)
tk.MustExec(`insert into p2 values('HOST_PORT');`)
for _, table := range []string{"p1", "p2"} {
for _, hint := range []string{"TIDB_INLJ"} {
sql := fmt.Sprintf(`select /*+ %s(%s) */ * from i, %s where i.objectType = %s.type;`, hint, table, table, table)
rows := tk.MustQuery(sql).Rows()
c.Assert(len(rows), Equals, 64)
for i := 0; i < len(rows); i++ {
c.Assert(fmt.Sprint(rows[i][0]), Equals, "HOST_PORT")
}
rows = tk.MustQuery("show warnings").Rows()
c.Assert(len(rows), Equals, 0)
}
}
}
10 changes: 3 additions & 7 deletions types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package types

import (
"context"
"fmt"
"math"
"sort"
Expand All @@ -30,8 +29,6 @@ import (
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/hack"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
)

// Kind constants.
Expand Down Expand Up @@ -1296,8 +1293,7 @@ func (d *Datum) convertToMysqlEnum(sc *stmtctx.StatementContext, target *FieldTy
e, err = ParseEnumValue(target.Elems, uintDatum.GetUint64())
}
if err != nil {
logutil.Logger(context.Background()).Error("convert to MySQL enum failed", zap.Error(err))
err = errors.Trace(ErrTruncated)
err = errors.Wrap(ErrTruncated, "convert to MySQL enum failed: "+err.Error())
}
ret.SetValue(e)
return ret, err
Expand All @@ -1322,10 +1318,10 @@ func (d *Datum) convertToMysqlSet(sc *stmtctx.StatementContext, target *FieldTyp
}

if err != nil {
return invalidConv(d, target.Tp)
err = errors.Wrap(ErrTruncated, "convert to MySQL set failed: "+err.Error())
}
ret.SetValue(s)
return ret, nil
return ret, err
}

func (d *Datum) convertToMysqlJSON(sc *stmtctx.StatementContext, target *FieldType) (ret Datum, err error) {
Expand Down