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

sql: simplify/optimize/fix the aggregate functions. #8680

Merged
merged 2 commits into from
Aug 20, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 10 additions & 16 deletions sql/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,31 +487,27 @@ func (v *extractAggregatesVisitor) VisitPre(expr parser.Expr) (recurse bool, new

switch t := expr.(type) {
case *parser.FuncExpr:
fn, err := t.Name.Normalize()
if err != nil {
v.err = err
return false, expr
}

if impl, ok := parser.Aggregates[strings.ToLower(fn.Function())]; ok {
if agg := t.GetAggregateConstructor(); agg != nil {
if len(t.Exprs) != 1 {
// Type checking has already run on these expressions thus
// if an aggregate function of the wrong arity gets here,
// something has gone really wrong.
panic(fmt.Sprintf("%q has %d arguments (expected 1)", fn, len(t.Exprs)))
panic(fmt.Sprintf("%q has %d arguments (expected 1)", t.Name, len(t.Exprs)))
}

argExpr := t.Exprs[0]

defer v.subAggregateVisitor.Reset()
parser.WalkExprConst(&v.subAggregateVisitor, t.Exprs[0])
parser.WalkExprConst(&v.subAggregateVisitor, argExpr)
if v.subAggregateVisitor.Aggregated {
v.err = fmt.Errorf("aggregate function calls cannot be nested under %s", t.Name)
return false, expr
}

f := &aggregateFuncHolder{
expr: t,
arg: t.Exprs[0].(parser.TypedExpr),
create: impl[0].AggregateFunc,
arg: argExpr.(parser.TypedExpr),
create: agg,
group: v.n,
buckets: make(map[string]parser.AggregateFunc),
}
Expand Down Expand Up @@ -603,7 +599,8 @@ func (a *aggregateFuncHolder) add(bucket []byte, d parser.Datum) error {
a.buckets[string(bucket)] = impl
}

return impl.Add(d)
impl.Add(d)
return nil
}

func (*aggregateFuncHolder) Variable() {}
Expand Down Expand Up @@ -632,10 +629,7 @@ func (a *aggregateFuncHolder) Eval(ctx *parser.EvalContext) (parser.Datum, error
found = a.create()
}

datum, err := found.Result()
if err != nil {
return nil, err
}
datum := found.Result()

// This is almost certainly the identity. Oh well.
return datum.Eval(ctx)
Expand Down
Loading