Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plan: prune cor cols in gby items. #2568

Merged
merged 8 commits into from
Feb 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions plan/column_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,34 @@ func (p *Aggregation) PruneColumns(parentUsedCols []*expression.Column) {
selfUsedCols = append(selfUsedCols, expression.ExtractColumns(arg)...)
}
}
for _, expr := range p.GroupByItems {
selfUsedCols = append(selfUsedCols, expression.ExtractColumns(expr)...)
if len(p.GroupByItems) > 0 {
for i := len(p.GroupByItems) - 1; i >= 0; i-- {
cols := expression.ExtractColumns(p.GroupByItems[i])
if len(cols) == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will len(cols) be zero?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like group by '1' or group by a correlated column.

Copy link
Member

@shenli shenli Jan 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we do not need to "selfUsedCols = append(selfUsedCols, cols...)"

p.GroupByItems = append(p.GroupByItems[:i], p.GroupByItems[i+1:]...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just set p.GroupByItems[i] = expression.One? The line 103 can be removed.
The code would be mush simpler.
And add comments about why replace it to one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we set i to One, it will cost more to encode.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a very rare case, I think the saved cost doesn't worth the complexity.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it adds the complexity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writers always underestimate the complexity.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't agree

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please the provide a benchmark result difference for the reduced group by items, and estimate the frequency this case will be run.
Then give a few example in our projects that have lower (performance Improvement * frequency) / complexity.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doesn't need a bench test. If it has massive group by items, it must be very inefficient.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think that is complex, I think everything is complex, every optimization is complex, every sql should be written as best form by user.

} else {
selfUsedCols = append(selfUsedCols, cols...)
}
}
// If all the group by items are pruned, we should add a constant 1 to keep the correctness.
// Because `select count(*) from t` is different from `select count(*) from t group by 1`.
if len(p.GroupByItems) == 0 {
p.GroupByItems = []expression.Expression{expression.One}
}
}
child.PruneColumns(selfUsedCols)
}

// PruneColumns implements LogicalPlan interface.
func (p *Sort) PruneColumns(parentUsedCols []*expression.Column) {
child := p.children[0].(LogicalPlan)
for _, item := range p.ByItems {
parentUsedCols = append(parentUsedCols, expression.ExtractColumns(item.Expr)...)
for i := len(p.ByItems) - 1; i >= 0; i-- {
cols := expression.ExtractColumns(p.ByItems[i].Expr)
if len(cols) == 0 {
p.ByItems = append(p.ByItems[:i], p.ByItems[i+1:]...)
} else {
parentUsedCols = append(parentUsedCols, expression.ExtractColumns(p.ByItems[i].Expr)...)
}
}
child.PruneColumns(parentUsedCols)
p.SetSchema(p.children[0].Schema())
Expand Down
11 changes: 11 additions & 0 deletions plan/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,10 @@ func (s *testPlanSuite) TestPlanBuilder(c *C) {
sql: "select count(c) ,(select count(s.b) from t s where s.a = t.a) from t",
plan: "Apply{DataScan(t)->Aggr(count(test.t.c),firstrow(test.t.a))->DataScan(s)->Selection->Aggr(count(s.b))}->Projection->Projection",
},
{
sql: "select a from t where a in (select a from t s group by t.b)",
plan: "Join{DataScan(t)->DataScan(s)->Aggr(firstrow(s.a))->Projection}(test.t.a,a)->Projection",
},
{
// This will be resolved as in sub query.
sql: "select * from t where 10 in (((select b from t s where s.a = t.a)))",
Expand Down Expand Up @@ -1060,6 +1064,13 @@ func (s *testPlanSuite) TestColumnPruning(c *C) {
"TableScan_2": {"c", "d"},
},
},
{
sql: "select a from t where a in (select a from t s group by t.b)",
ans: map[string][]string{
"TableScan_1": {"a"},
"TableScan_2": {"a"},
},
},
}
for _, ca := range cases {
comment := Commentf("for %s", ca.sql)
Expand Down
3 changes: 3 additions & 0 deletions plan/physical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,9 @@ func (p *Sort) convert2PhysicalPlan(prop *requiredProperty) (*physicalPlanInfo,
if info != nil {
return info, nil
}
if len(p.ByItems) == 0 {
return p.children[0].(LogicalPlan).convert2PhysicalPlan(prop)
}
selfProp := &requiredProperty{
props: make([]*columnProp, 0, len(p.ByItems)),
}
Expand Down
6 changes: 6 additions & 0 deletions plan/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ func (s *testPlanSuite) TestPushDownOrderbyAndLimit(c *C) {
orderByItmes: "[]",
limit: "5",
},
{
sql: "select * from t order by 1 limit 5",
best: "Table(t)->Limit->Projection",
orderByItmes: "[]",
limit: "5",
},
{
sql: "select c from t order by c limit 5",
best: "Index(t.c_d_e)[[<nil>,+inf]]->Limit->Projection",
Expand Down