From 9667eca8e4ebd7e4fa0dbf98da833c6cd45c11fa Mon Sep 17 00:00:00 2001 From: Zhang Jian Date: Tue, 14 Aug 2018 12:44:07 +0800 Subject: [PATCH] executor: MaxOneRow operator should keep its promise (#7375) --- executor/executor.go | 12 +++++++++--- executor/executor_test.go | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/executor/executor.go b/executor/executor.go index 45578f21e6bad..0a5cf7c39d96b 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -899,11 +899,17 @@ func (e *MaxOneRowExec) Next(ctx context.Context, chk *chunk.Chunk) error { chk.AppendNull(i) } return nil - } else if num == 1 { - return nil + } else if num != 1 { + return errors.New("subquery returns more than 1 row") + } + + childChunk := e.children[0].newChunk() + err = e.children[0].Next(ctx, childChunk) + if childChunk.NumRows() != 0 { + return errors.New("subquery returns more than 1 row") } - return errors.New("subquery returns more than 1 row") + return nil } // UnionExec pulls all it's children's result and returns to its parent directly. diff --git a/executor/executor_test.go b/executor/executor_test.go index 0fae51a6ccc6b..e2e53470e550e 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -3068,3 +3068,23 @@ func (s *testSuite) TestUpdateJoin(c *C) { tk.MustQuery("select k, v from t5").Check(testkit.Rows("0 0")) } + +func (s *testSuite) TestMaxOneRow(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec(`use test`) + tk.MustExec(`drop table if exists t1`) + tk.MustExec(`drop table if exists t2`) + tk.MustExec(`create table t1(a double, b double);`) + tk.MustExec(`create table t2(a double, b double);`) + tk.MustExec(`insert into t1 values(1, 1), (2, 2), (3, 3);`) + tk.MustExec(`insert into t2 values(0, 0);`) + tk.MustExec(`set @@tidb_max_chunk_size=1;`) + rs, err := tk.Exec(`select (select t1.a from t1 where t1.a > t2.a) as a from t2;`) + c.Assert(err, IsNil) + + err = rs.Next(context.TODO(), rs.NewChunk()) + c.Assert(err.Error(), Equals, "subquery returns more than 1 row") + + err = rs.Close() + c.Assert(err, IsNil) +}