Skip to content

Commit

Permalink
planner: fix union statements order (#8214) (#8318)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtunique authored and zz-jason committed Nov 15, 2018
1 parent b4e5843 commit 464a3e6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
3 changes: 2 additions & 1 deletion plan/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,8 @@ func (b *planBuilder) buildUnion(union *ast.UnionStmt) LogicalPlan {
if unionDistinctPlan != nil {
unionDistinctPlan = b.buildDistinct(unionDistinctPlan, unionDistinctPlan.Schema().Len())
if len(allSelectPlans) > 0 {
allSelectPlans = append(allSelectPlans, unionDistinctPlan)
// Can't change the statements order in order to get the correct column info.
allSelectPlans = append([]LogicalPlan{unionDistinctPlan}, allSelectPlans...)
}
}

Expand Down
8 changes: 4 additions & 4 deletions plan/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,7 @@ func (s *testPlanSuite) TestUnion(c *C) {
},
{
sql: "select a from t union select a from t union all select a from t",
best: "UnionAll{DataScan(t)->Projection->UnionAll{DataScan(t)->Projection->DataScan(t)->Projection}->Aggr(firstrow(t.a))->Projection}",
best: "UnionAll{UnionAll{DataScan(t)->Projection->DataScan(t)->Projection}->Aggr(firstrow(t.a))->Projection->DataScan(t)->Projection}",
err: false,
},
{
Expand All @@ -1461,12 +1461,12 @@ func (s *testPlanSuite) TestUnion(c *C) {
},
{
sql: "select * from (select 1 as a union select 1 union all select 2) t order by a",
best: "UnionAll{UnionAll{Dual->Projection->Projection->Dual->Projection->Projection}->Aggr(firstrow(a))->Projection->Dual->Projection->Projection}->Projection->Sort",
best: "UnionAll{UnionAll{Dual->Projection->Dual->Projection}->Aggr(firstrow(a))->Projection->Dual->Projection}->Projection->Sort",
err: false,
},
{
sql: "select * from (select 1 as a union select 1 union all select 2) t order by (select a)",
best: "Apply{UnionAll{UnionAll{Dual->Projection->Projection->Dual->Projection->Projection}->Aggr(firstrow(a))->Projection->Dual->Projection->Projection}->Dual->Projection->MaxOneRow}->Sort->Projection",
best: "Apply{UnionAll{UnionAll{Dual->Projection->Dual->Projection}->Aggr(firstrow(a))->Projection->Dual->Projection}->Dual->Projection->MaxOneRow}->Sort->Projection",
err: false,
},
}
Expand All @@ -1484,7 +1484,7 @@ func (s *testPlanSuite) TestUnion(c *C) {
plan := builder.build(stmt)
if tt.err {
c.Assert(builder.err, NotNil)
return
continue
}
c.Assert(builder.err, IsNil)
p := plan.(LogicalPlan)
Expand Down

0 comments on commit 464a3e6

Please sign in to comment.