Skip to content

Commit

Permalink
planner: fix union statements order (#8214)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtunique committed Nov 15, 2018
1 parent ff0c436 commit 97e7f16
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 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
9 changes: 7 additions & 2 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 @@ -1469,6 +1469,11 @@ func (s *testPlanSuite) TestUnion(c *C) {
best: "Apply{UnionAll{UnionAll{Dual->Projection->Projection->Dual->Projection->Projection}->Aggr(firstrow(a))->Projection->Dual->Projection->Projection}->Dual->Projection->MaxOneRow}->Sort->Projection",
err: false,
},
{
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",
err: false,
},
}
for i, tt := range tests {
comment := Commentf("case:%v sql:%s", i, tt.sql)
Expand All @@ -1484,7 +1489,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 97e7f16

Please sign in to comment.