diff --git a/executor/batch_point_get.go b/executor/batch_point_get.go index 50329caec87ed..b2b8ff82a9594 100644 --- a/executor/batch_point_get.go +++ b/executor/batch_point_get.go @@ -19,6 +19,7 @@ import ( "github.com/pingcap/failpoint" "github.com/pingcap/parser/model" + "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/store/tikv" @@ -231,12 +232,21 @@ func (e *BatchPointGetExec) initialize(ctx context.Context) error { failpoint.InjectContext(ctx, "batchPointGetRepeatableReadTest-step2", nil) }) } else if e.keepOrder { - sort.Slice(e.handles, func(i int, j int) bool { + less := func(i int, j int) bool { if e.desc { return e.handles[i] > e.handles[j] } return e.handles[i] < e.handles[j] - }) + } + if e.tblInfo.PKIsHandle && mysql.HasUnsignedFlag(e.tblInfo.GetPkColInfo().Flag) { + less = func(i int, j int) bool { + if e.desc { + return uint64(e.handles[i]) > uint64(e.handles[j]) + } + return uint64(e.handles[i]) < uint64(e.handles[j]) + } + } + sort.Slice(e.handles, less) } keys := make([]kv.Key, len(e.handles)) diff --git a/executor/batch_point_get_test.go b/executor/batch_point_get_test.go index 732c45ee2fc3f..e53c608647052 100644 --- a/executor/batch_point_get_test.go +++ b/executor/batch_point_get_test.go @@ -138,3 +138,15 @@ func (s *testBatchPointGetSuite) TestBatchPointGetCache(c *C) { tk.MustQuery("SELECT id, token FROM test.customers WHERE id IN (28)") tk.MustQuery("SELECT id, token FROM test.customers WHERE id IN (28, 29);").Check(testkit.Rows("28 07j", "29 03j")) } + +func (s *testBatchPointGetSuite) TestBatchPointGetUnsignedHandleWithSort(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t2") + tk.MustExec("create table t2 (id bigint(20) unsigned, primary key(id))") + tk.MustExec("insert into t2 values (8738875760185212610)") + tk.MustExec("insert into t2 values (9814441339970117597)") + tk.MustExec("insert into t2 values (1)") + tk.MustQuery("select id from t2 where id in (8738875760185212610, 1, 9814441339970117597) order by id").Check(testkit.Rows("1", "8738875760185212610", "9814441339970117597")) + tk.MustQuery("select id from t2 where id in (8738875760185212610, 1, 9814441339970117597) order by id desc").Check(testkit.Rows("9814441339970117597", "8738875760185212610", "1")) +}