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: covert max/min to Limit + Sort operators #5105

Merged
merged 18 commits into from Nov 16, 2017
Merged

plan: covert max/min to Limit + Sort operators #5105

merged 18 commits into from Nov 16, 2017

Conversation

shenli
Copy link
Member

@shenli shenli commented Nov 14, 2017

For SQL like select max(c) from t;, we could optimize it to select c from t order by c desc limit 1;.
For SQL like select min(c) from t;, we could optimize it to select c from t order by c limit 1;.

@shenli shenli added the sig/planner SIG: Planner label Nov 14, 2017
@shenli
Copy link
Member Author

shenli commented Nov 14, 2017

/run-all-tests

@shenli
Copy link
Member Author

shenli commented Nov 14, 2017

/run-all-tests

1 similar comment
@shenli
Copy link
Member Author

shenli commented Nov 15, 2017

/run-all-tests

@shenli
Copy link
Member Author

shenli commented Nov 15, 2017

@hanfei1991 @winoros PTAL

sort := Sort{}.init(a.ctx)
sort.ByItems = append(sort.ByItems, &ByItems{f.GetArgs()[0], desc})
sort.SetSchema(p.Schema().Clone())
sort.SetChildren(p.Children()...)
Copy link
Member

Choose a reason for hiding this comment

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

use setParentAndChildren

// Compose Sort operator.
sort := Sort{}.init(a.ctx)
sort.ByItems = append(sort.ByItems, &ByItems{f.GetArgs()[0], desc})
sort.SetSchema(p.Schema().Clone())
Copy link
Member

Choose a reason for hiding this comment

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

the schema of sort should be set as its child's

Copy link
Member Author

Choose a reason for hiding this comment

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

So as the limit?

Copy link
Member Author

Choose a reason for hiding this comment

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

@@ -562,6 +562,7 @@ func buildJoinSchema(joinType JoinType, join Plan, outerID int) *expression.Sche
return newSchema
}
return expression.MergeSchema(join.Children()[outerID].Schema(), join.Children()[1-outerID].Schema())

Copy link
Member

Choose a reason for hiding this comment

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

remove this

@shenli
Copy link
Member Author

shenli commented Nov 15, 2017

@hanfei1991 @winoros PTAL

@@ -63,14 +63,24 @@ func (p *LogicalAggregation) collectGroupByColumns() {
func (b *planBuilder) buildAggregation(p LogicalPlan, aggFuncList []*ast.AggregateFuncExpr, gbyItems []expression.Expression) (LogicalPlan, map[int]int) {
b.optFlag = b.optFlag | flagBuildKeyInfo
b.optFlag = b.optFlag | flagAggregationOptimize
eliminateAgg := len(gbyItems) == 0
Copy link
Member

Choose a reason for hiding this comment

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

This optimize is too complex. If we have an aggregate, we set flag eliminateAgg. That's enough.

@shenli
Copy link
Member Author

shenli commented Nov 16, 2017

@coocood The CBO framework should consider this.

@@ -1491,6 +1505,9 @@ func (b *planBuilder) buildSelect(sel *ast.SelectStmt) LogicalPlan {
if b.err != nil {
return nil
}
if b.optFlag&flagAggEliminate == flagAggEliminate {
Copy link
Member

Choose a reason for hiding this comment

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

convert to topn will be no worse than original. it's ok not to do this

@shenli
Copy link
Member Author

shenli commented Nov 16, 2017

@hanfei1991 @winoros PTAL

@coocood coocood changed the title Covert max/min to Limit + Sort operators plan: covert max/min to Limit + Sort operators Nov 16, 2017
@winoros
Copy link
Member

winoros commented Nov 16, 2017

So we can add some complex plan test.

Copy link
Member

@hanfei1991 hanfei1991 left a comment

Choose a reason for hiding this comment

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

LGTM

@shenli
Copy link
Member Author

shenli commented Nov 16, 2017

@winoros Add a new test case. I will add more tests in tidb-test.

winoros
winoros previously approved these changes Nov 16, 2017
Copy link
Member

@winoros winoros left a comment

Choose a reason for hiding this comment

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

LGTM

@hanfei1991
Copy link
Member

@shenli plan test meets building fail.

@shenli
Copy link
Member Author

shenli commented Nov 16, 2017

@hanfei1991 Fixed.

@hanfei1991 hanfei1991 merged commit 4b669b7 into pingcap:master Nov 16, 2017
winoros added a commit to winoros/tidb that referenced this pull request Nov 16, 2017
winoros added a commit that referenced this pull request Nov 16, 2017
winoros pushed a commit to winoros/tidb that referenced this pull request Dec 27, 2017
@shenli shenli deleted the maxmin2 branch July 27, 2018 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/planner SIG: Planner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants