Skip to content

Commit

Permalink
plan: use the inferred type as the column type in the schema (#7624)
Browse files Browse the repository at this point in the history
  • Loading branch information
zz-jason committed Sep 12, 2018
1 parent cb03f2b commit 7de83a3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
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)
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

0 comments on commit 7de83a3

Please sign in to comment.