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
Conversation
/run-all-tests |
/run-all-tests |
1 similar comment
/run-all-tests |
@hanfei1991 @winoros PTAL |
plan/aggregation_eliminate.go
Outdated
sort := Sort{}.init(a.ctx) | ||
sort.ByItems = append(sort.ByItems, &ByItems{f.GetArgs()[0], desc}) | ||
sort.SetSchema(p.Schema().Clone()) | ||
sort.SetChildren(p.Children()...) |
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.
use setParentAndChildren
plan/aggregation_eliminate.go
Outdated
// Compose Sort operator. | ||
sort := Sort{}.init(a.ctx) | ||
sort.ByItems = append(sort.ByItems, &ByItems{f.GetArgs()[0], desc}) | ||
sort.SetSchema(p.Schema().Clone()) |
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.
the schema of sort should be set as its child's
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.
So as the limit?
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.
plan/physical_plans.go
Outdated
@@ -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()) | |||
|
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.
remove this
@hanfei1991 @winoros PTAL |
plan/logical_plan_builder.go
Outdated
@@ -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 |
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.
This optimize is too complex. If we have an aggregate, we set flag eliminateAgg. That's enough.
@coocood The CBO framework should consider this. |
plan/logical_plan_builder.go
Outdated
@@ -1491,6 +1505,9 @@ func (b *planBuilder) buildSelect(sel *ast.SelectStmt) LogicalPlan { | |||
if b.err != nil { | |||
return nil | |||
} | |||
if b.optFlag&flagAggEliminate == flagAggEliminate { |
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.
convert to topn will be no worse than original. it's ok not to do this
@hanfei1991 @winoros PTAL |
So we can add some complex plan test. |
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.
LGTM
@winoros Add a new test case. I will add more tests in tidb-test. |
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.
LGTM
@shenli plan test meets building fail. |
@hanfei1991 Fixed. |
This reverts commit 4b669b7.
For SQL like
select max(c) from t;
, we could optimize it toselect c from t order by c desc limit 1;
.For SQL like
select min(c) from t;
, we could optimize it toselect c from t order by c limit 1;
.