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: use the inferred type as the column type in the schema #7624

Merged
merged 8 commits into from
Sep 12, 2018
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
11 changes: 11 additions & 0 deletions executor/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,14 @@ func (s *testSuite) TestBuildProjBelowAgg(c *C) {
"3 3 15 6,6,6 7",
"4 3 18 7,7,7 8"))
}

func (s *testSuite) TestFirstRowEnum(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec(`use test;`)
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a enum('a', 'b'));`)
tk.MustExec(`insert into t values('a');`)
tk.MustQuery(`select a from t group by a;`).Check(testkit.Rows(
`a`,
))
}
4 changes: 3 additions & 1 deletion plan/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ func (b *planBuilder) buildAggregation(p LogicalPlan, aggFuncList []*ast.Aggrega
for _, col := range p.Schema().Columns {
newFunc := aggregation.NewAggFuncDesc(b.ctx, ast.AggFuncFirstRow, []expression.Expression{col}, false)
plan4Agg.AggFuncs = append(plan4Agg.AggFuncs, newFunc)
schema4Agg.Append(col)
newCol, _ := col.Clone().(*expression.Column)
Copy link
Member

Choose a reason for hiding this comment

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

So one column occurs in agg and agg' child. The tp of this two is different?

Copy link
Member Author

Choose a reason for hiding this comment

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

Possibly. Actually, they are two columns if the type inferred after NewAggFuncDesc() is different from the origin type.

Copy link
Member

Choose a reason for hiding this comment

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

Since they are different, why unique id is the same one.

newCol.RetType = newFunc.RetTp
schema4Agg.Append(newCol)
}
plan4Agg.SetChildren(p)
plan4Agg.GroupByItems = gbyItems
Expand Down
4 changes: 3 additions & 1 deletion plan/rule_aggregation_push_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,10 @@ func (a *aggregationOptimizer) makeNewAgg(ctx sessionctx.Context, aggFuncs []*ag
}
for _, gbyCol := range gbyCols {
firstRow := aggregation.NewAggFuncDesc(agg.ctx, ast.AggFuncFirstRow, []expression.Expression{gbyCol}, false)
newCol, _ := gbyCol.Clone().(*expression.Column)
newCol.RetType = firstRow.RetTp
newAggFuncDescs = append(newAggFuncDescs, firstRow)
schema.Append(gbyCol)
schema.Append(newCol)
}
agg.AggFuncs = newAggFuncDescs
agg.SetSchema(schema)
Expand Down
11 changes: 9 additions & 2 deletions plan/rule_decorrelate.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,19 @@ func (s *decorrelateSolver) optimize(p LogicalPlan) (LogicalPlan, error) {
agg.SetSchema(apply.Schema())
agg.GroupByItems = expression.Column2Exprs(outerPlan.Schema().Keys[0])
newAggFuncs := make([]*aggregation.AggFuncDesc, 0, apply.Schema().Len())
for _, col := range outerPlan.Schema().Columns {

outerColsInSchema := make([]*expression.Column, 0, outerPlan.Schema().Len())
for i, col := range outerPlan.Schema().Columns {
first := aggregation.NewAggFuncDesc(agg.ctx, ast.AggFuncFirstRow, []expression.Expression{col}, false)
newAggFuncs = append(newAggFuncs, first)

outerCol, _ := outerPlan.Schema().Columns[i].Clone().(*expression.Column)
outerCol.RetType = first.RetTp
outerColsInSchema = append(outerColsInSchema, outerCol)
}
newAggFuncs = append(newAggFuncs, agg.AggFuncs...)
agg.AggFuncs = newAggFuncs
apply.SetSchema(expression.MergeSchema(outerPlan.Schema(), innerPlan.Schema()))
apply.SetSchema(expression.MergeSchema(expression.NewSchema(outerColsInSchema...), innerPlan.Schema()))
np, err := s.optimize(p)
if err != nil {
return nil, errors.Trace(err)
Expand Down Expand Up @@ -229,6 +235,7 @@ func (s *decorrelateSolver) optimize(p LogicalPlan) (LogicalPlan, error) {
newFunc := aggregation.NewAggFuncDesc(apply.ctx, ast.AggFuncFirstRow, []expression.Expression{clonedCol}, false)
agg.AggFuncs = append(agg.AggFuncs, newFunc)
agg.schema.Append(clonedCol.(*expression.Column))
agg.schema.Columns[agg.schema.Len()-1].RetType = newFunc.RetTp
}
// If group by cols don't contain the join key, add it into this.
if agg.getGbyColIndex(eqCond.GetArgs()[1].(*expression.Column)) == -1 {
Expand Down