diff --git a/executor/executor_test.go b/executor/executor_test.go index bb97f1770acab..67168cf725b66 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -5378,3 +5378,15 @@ func (s *testSuite1) TestIssue15718(c *C) { tk.MustExec("insert into tt values(0, '2', null), (7, null, '1122'), (NULL, 'w', null), (NULL, '2', '3344'), (NULL, NULL, '0'), (7, 'f', '33');") tk.MustQuery("select a and b as d, a or c as e from tt;").Check(testkit.Rows("0 ", " 1", "0 ", " 1", " ", "0 1")) } + +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 5668707c644d9..c812d435fbaa6 100644 --- a/store/mockstore/mocktikv/executor.go +++ b/store/mockstore/mocktikv/executor.go @@ -567,6 +567,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 } } @@ -575,7 +576,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++