From aa08e5ed8dd69ec430c2549aff2ba93349ae2ef6 Mon Sep 17 00:00:00 2001 From: Jian Zhang Date: Mon, 13 Aug 2018 16:37:34 +0800 Subject: [PATCH 1/2] executor: MaxOneRow operator should keep its promise --- executor/executor.go | 12 +++++++++--- 1 file changed, 9 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. From 9ef1cc8e792005cba9483bad8c6aff08b153abeb Mon Sep 17 00:00:00 2001 From: Jian Zhang Date: Tue, 14 Aug 2018 11:18:22 +0800 Subject: [PATCH 2/2] add test --- executor/executor_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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) +}