Skip to content

Commit

Permalink
Merge pull request pingcap#876 from pingcap/shenli/agg-cleanup
Browse files Browse the repository at this point in the history
*: Aggregate related code clean up
  • Loading branch information
qiuyesuifeng committed Jan 23, 2016
2 parents b343ccd + e7be09a commit ccf4efb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions ast/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,14 +421,14 @@ func (n *AggregateFuncExpr) Update() error {
return n.updateCount()
case AggFuncFirstRow:
return n.updateFirstRow()
case AggFuncGroupConcat:
return n.updateGroupConcat()
case AggFuncMax:
return n.updateMaxMin(true)
case AggFuncMin:
return n.updateMaxMin(false)
case AggFuncSum, AggFuncAvg:
return n.updateSum()
case AggFuncGroupConcat:
return n.updateGroupConcat()
}
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions optimizer/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const (
CodeMultiWildCard terror.ErrCode = 3
CodeUnsupported terror.ErrCode = 4
CodeInvalidGroupFuncUse terror.ErrCode = 5
CodeIllegalReference terror.ErrCode = 6
)

// Optimizer base errors.
Expand All @@ -128,6 +129,7 @@ var (
ErrMultiWildCard = terror.ClassOptimizer.New(CodeMultiWildCard, "wildcard field exist more than once")
ErrUnSupported = terror.ClassOptimizer.New(CodeUnsupported, "unsupported")
ErrInvalidGroupFuncUse = terror.ClassOptimizer.New(CodeInvalidGroupFuncUse, "Invalid use of group function")
ErrIllegalReference = terror.ClassOptimizer.New(CodeIllegalReference, "Illegal reference")
)

func init() {
Expand All @@ -136,6 +138,7 @@ func init() {
CodeSameColumns: mysql.ErrOperandColumns,
CodeMultiWildCard: mysql.ErrParse,
CodeInvalidGroupFuncUse: mysql.ErrInvalidGroupFuncUse,
CodeIllegalReference: mysql.ErrIllegalReference,
}
terror.ErrClassToMySQLCodes[terror.ClassOptimizer] = mySQLErrCodes
}
15 changes: 8 additions & 7 deletions optimizer/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (nr *nameResolver) Enter(inNode ast.Node) (outNode ast.Node, skipChildren b
if nr.currentContext().inGroupBy {
// make sure item is not aggregate function
if ast.HasAggFlag(v.Expr) {
nr.Err = errors.New("group by cannot contain aggregate function")
nr.Err = ErrInvalidGroupFuncUse
return inNode, true
}
}
Expand Down Expand Up @@ -326,12 +326,13 @@ func (nr *nameResolver) resolveColumnNameInContext(ctx *resolverContext, cn *ast
return true
}
found := nr.resolveColumnInResultFields(ctx, cn, ctx.fieldList)
if !found || nr.Err != nil {
return found
}
if _, ok := cn.Refer.Expr.(*ast.AggregateFuncExpr); ok {
nr.Err = errors.New("Groupby identifier can not refer to aggregate function.")
if nr.Err == nil && found {
// Check if resolved refer is an aggregate function expr.
if _, ok := cn.Refer.Expr.(*ast.AggregateFuncExpr); ok {
nr.Err = ErrIllegalReference.Gen("Reference '%s' not supported (reference to group function)", cn.Name.Name.O)
}
}
return found
}
// Resolve from table first, then from select list.
found := nr.resolveColumnInTableSources(cn, ctx.tables)
Expand All @@ -353,7 +354,7 @@ func (nr *nameResolver) resolveColumnNameInContext(ctx *resolverContext, cn *ast
cn.Refer = r
}
if _, ok := cn.Refer.Expr.(*ast.AggregateFuncExpr); ok {
nr.Err = errors.New("Groupby identifier can not refer to aggregate function.")
nr.Err = ErrIllegalReference.Gen("Reference '%s' not supported (reference to group function)", cn.Name.Name.O)
}
return true
}
Expand Down

0 comments on commit ccf4efb

Please sign in to comment.