From 8937352257e43b32ad40ca19359568850b376356 Mon Sep 17 00:00:00 2001 From: HuaiyuXu <391585975@qq.com> Date: Tue, 24 Apr 2018 23:35:24 +0800 Subject: [PATCH] executor, plan: eliminate projection if the schema len is 0 (#6318) (#6342) --- executor/executor_test.go | 7 +++++++ plan/eliminate_projection.go | 3 +++ 2 files changed, 10 insertions(+) diff --git a/executor/executor_test.go b/executor/executor_test.go index 6cd648b821bf9..924590319b9a7 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -942,6 +942,13 @@ func (s *testSuite) TestUnion(c *C) { tk.MustExec("insert into t value ('2017-01-01'), ('2017-01-02')") r = tk.MustQuery("(select a from t where a < 0) union (select a from t where a > 0) order by a") r.Check(testkit.Rows("2017-01-01", "2017-01-02")) + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int)") + tk.MustExec("insert into t value(0),(0)") + tk.MustQuery("select 1 from (select a from t union all select a from t) tmp").Check(testkit.Rows("1", "1", "1", "1")) + tk.MustQuery("select 1 from (select a from t limit 1 union all select a from t limit 1) tmp").Check(testkit.Rows("1", "1")) + tk.MustQuery("select count(1) from (select a from t union all select a from t) tmp").Check(testkit.Rows("4")) } func (s *testSuite) TestIn(c *C) { diff --git a/plan/eliminate_projection.go b/plan/eliminate_projection.go index aa59a9e86b70a..009d83374833d 100644 --- a/plan/eliminate_projection.go +++ b/plan/eliminate_projection.go @@ -32,6 +32,9 @@ func canProjectionBeEliminatedLoose(p *LogicalProjection) bool { // canProjectionBeEliminatedStrict checks whether a projection can be eliminated, returns true if // the projection just copy its child's output. func canProjectionBeEliminatedStrict(p *PhysicalProjection) bool { + if p.Schema().Len() == 0 { + return true + } child := p.Children()[0] if p.Schema().Len() != child.Schema().Len() { return false