From 2ade4196230d97d109e5747868c3be332af7f93e Mon Sep 17 00:00:00 2001 From: pingcap-github-bot Date: Thu, 9 Apr 2020 09:57:09 +0800 Subject: [PATCH] mocktikv: avoid sorting multiple times with unstable order (#15898) (#16199) --- executor/executor_test.go | 12 ++++++++++++ store/mockstore/mocktikv/executor.go | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/executor/executor_test.go b/executor/executor_test.go index 30ec980bab880..229b5a879c3cb 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -4376,3 +4376,15 @@ func (s *testSuite1) TestPartitionHashCode(c *C) { } wg.Wait() } + +func (s *testSuite1) TestIssue15767(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test;") + tk.MustExec("drop table if exists tt;") + tk.MustExec("create table t(a int, b char);") + tk.MustExec("insert into t values (1,'s'),(2,'b'),(1,'c'),(2,'e'),(1,'a');") + 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.MustQuery("select b, count(*) from ( select b from t order by a limit 20 offset 2) as s group by b order by b;").Check(testkit.Rows("a 6", "c 7", "s 7")) +} diff --git a/store/mockstore/mocktikv/executor.go b/store/mockstore/mocktikv/executor.go index 7808e18cff554..522b23a2b64e3 100644 --- a/store/mockstore/mocktikv/executor.go +++ b/store/mockstore/mocktikv/executor.go @@ -562,6 +562,7 @@ func (e *topNExec) Next(ctx context.Context) (value [][]byte, err error) { return nil, errors.Trace(err) } if !hasMore { + sort.Sort(&e.heap.topNSorter) break } } @@ -570,7 +571,6 @@ func (e *topNExec) Next(ctx context.Context) (value [][]byte, err error) { if e.cursor >= len(e.heap.rows) { return nil, nil } - sort.Sort(&e.heap.topNSorter) row := e.heap.rows[e.cursor] e.cursor++