From 77f7faf916f4188d12ab722212cceb552af53fee Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Wed, 26 Aug 2020 16:14:29 +0800 Subject: [PATCH] Fix error result union scan with apply (#19245) (#19298) Signed-off-by: ti-srebot --- executor/union_scan.go | 9 +++++++++ executor/union_scan_test.go | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/executor/union_scan.go b/executor/union_scan.go index 98d787d9a4663..52e3015ba2803 100644 --- a/executor/union_scan.go +++ b/executor/union_scan.go @@ -172,6 +172,15 @@ func (us *UnionScanExec) Next(ctx context.Context, req *chunk.Chunk) error { return nil } +// Close implements the Executor Close interface. +func (us *UnionScanExec) Close() error { + us.cursor4AddRows = 0 + us.cursor4SnapshotRows = 0 + us.addedRows = us.addedRows[:0] + us.snapshotRows = us.snapshotRows[:0] + return us.children[0].Close() +} + // getOneRow gets one result row from dirty table or child. func (us *UnionScanExec) getOneRow(ctx context.Context) ([]types.Datum, error) { for { diff --git a/executor/union_scan_test.go b/executor/union_scan_test.go index 72ec921d69469..7e1a594464480 100644 --- a/executor/union_scan_test.go +++ b/executor/union_scan_test.go @@ -280,3 +280,17 @@ func (s *testSuite4) TestForUpdateUntouchedIndex(c *C) { tk.MustExec("commit") tk.MustExec("admin check table t") } + +// See https://github.com/pingcap/tidb/issues/19136 +func (s *testSuite4) TestForApplyAndUnionScan(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + + tk.MustExec("create table t ( c_int int, c_str varchar(40), primary key(c_int, c_str) )") + tk.MustExec("begin") + tk.MustExec("insert into t values (1, 'amazing almeida'), (2, 'boring bardeen'), (3, 'busy wescoff')") + tk.MustQuery("select c_int, (select t1.c_int from t t1 where t1.c_int = 3 and t1.c_int > t.c_int order by t1.c_int limit 1) x from t").Check(testkit.Rows("1 3", "2 3", "3 ")) + tk.MustExec("commit") + tk.MustQuery("select c_int, (select t1.c_int from t t1 where t1.c_int = 3 and t1.c_int > t.c_int order by t1.c_int limit 1) x from t").Check(testkit.Rows("1 3", "2 3", "3 ")) +}