Skip to content

Commit

Permalink
plan: support pushing agg across projection. (pingcap#2156)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanfei1991 authored and shenli committed Dec 2, 2016
1 parent fa4bdc7 commit 071caf9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
17 changes: 17 additions & 0 deletions plan/aggregation_push_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,23 @@ func (a *aggPushDownSolver) aggPushDown(p LogicalPlan) {
rChild.SetParents(join)
join.SetSchema(append(lChild.GetSchema().Clone(), rChild.GetSchema().Clone()...))
}
} else if proj, ok1 := child.(*Projection); ok1 {
// TODO: This optimization is not always reasonable. We have not supported pushing projection to kv layer yet,
// so we must do this optimization.
for i, gbyItem := range agg.GroupByItems {
agg.GroupByItems[i] = expression.ColumnSubstitute(gbyItem, proj.schema, proj.Exprs)
}
agg.collectGroupByColumns()
for _, aggFunc := range agg.AggFuncs {
newArgs := make([]expression.Expression, 0, len(aggFunc.GetArgs()))
for _, arg := range aggFunc.GetArgs() {
newArgs = append(newArgs, expression.ColumnSubstitute(arg, proj.schema, proj.Exprs))
}
aggFunc.SetArgs(newArgs)
}
projChild := proj.children[0]
agg.SetChildren(projChild)
projChild.SetParents(agg)
} else if union, ok1 := child.(*Union); ok1 {
pushedAgg := a.makeNewAgg(agg.AggFuncs, agg.groupByCols)
newChildren := make([]Plan, 0, len(union.children))
Expand Down
6 changes: 5 additions & 1 deletion plan/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,9 +675,13 @@ func (s *testPlanSuite) TestAggPushDown(c *C) {
sql: "select sum(a.a) from t a right join t b on a.c = b.c",
best: "Join{DataScan(a)->Aggr(sum(a.a),firstrow(a.c))->DataScan(b)}(a.c,b.c)->Aggr(sum(join_agg_0))->Projection",
},
{
sql: "select sum(a) from (select * from t) x",
best: "DataScan(t)->Aggr(sum(test.t.a))->Projection",
},
{
sql: "select sum(c1) from (select c c1, d c2 from t a union all select a c1, b c2 from t b union all select b c1, e c2 from t c) x group by c2",
best: "UnionAll{DataScan(a)->Projection->Aggr(sum(c1),firstrow(c2))->DataScan(b)->Projection->Aggr(sum(c1),firstrow(c2))->DataScan(c)->Projection->Aggr(sum(c1),firstrow(c2))}->Aggr(sum(join_agg_0))->Projection",
best: "UnionAll{DataScan(a)->Aggr(sum(a.c),firstrow(a.d))->DataScan(b)->Aggr(sum(b.a),firstrow(b.b))->DataScan(c)->Aggr(sum(c.b),firstrow(c.e))}->Aggr(sum(join_agg_0))->Projection",
},
}
for _, ca := range cases {
Expand Down

0 comments on commit 071caf9

Please sign in to comment.