diff --git a/executor/executor_test.go b/executor/executor_test.go index 039e18cdf8d23..03ef8c4ec310f 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -1039,6 +1039,11 @@ func (s *testSuite) TestUnion(c *C) { tk.MustExec(`set @@tidb_max_chunk_size=2;`) tk.MustQuery(`select count(*) from (select t1.a, t1.b from t1 left join t2 on t1.a=t2.a union all select t1.a, t1.a from t1 left join t2 on t1.a=t2.a) tmp;`).Check(testkit.Rows("128")) tk.MustQuery(`select tmp.a, count(*) from (select t1.a, t1.b from t1 left join t2 on t1.a=t2.a union all select t1.a, t1.a from t1 left join t2 on t1.a=t2.a) tmp;`).Check(testkit.Rows("1 128")) + + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int, b int)") + tk.MustExec("insert into t value(1 ,2)") + tk.MustQuery("select a, b from (select a, 0 as d, b from t union all select a, 0 as d, b from t) test;").Check(testkit.Rows("1 2", "1 2")) } func (s *testSuite) TestIn(c *C) { diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 35653f2db8a8f..6f95472269905 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -678,7 +678,7 @@ func (b *planBuilder) buildProjection4Union(u *LogicalUnionAll) { } b.optFlag |= flagEliminateProjection proj := LogicalProjection{Exprs: exprs}.init(b.ctx) - proj.SetSchema(expression.NewSchema(unionCols...)) + proj.SetSchema(u.schema.Clone()) proj.SetChildren(child) u.children[childID] = proj }