diff --git a/executor/executor_test.go b/executor/executor_test.go index c0da64c73acee..7c5841cc3bf80 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -1076,6 +1076,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 c8a1259f55ba7..2fc1730640956 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -712,7 +712,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 }