-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Changes from all commits
9c9a940
22c9173
fb94fb0
067e86d
dc7694a
1459c87
4a0e564
a773469
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
p.GroupByItems = append(p.GroupByItems[:i], p.GroupByItems[i+1:]...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about just set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we set i to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it adds the complexity. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Writers always underestimate the complexity. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't agree There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...)"