-
Notifications
You must be signed in to change notification settings - Fork 67
/
agg.go
70 lines (61 loc) · 1.62 KB
/
agg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package expr
import (
"github.com/brimdata/zed"
"github.com/brimdata/zed/runtime/expr/agg"
)
type Aggregator struct {
pattern agg.Pattern
expr Evaluator
where Evaluator
}
func NewAggregator(op string, expr Evaluator, where Evaluator) (*Aggregator, error) {
pattern, err := agg.NewPattern(op, expr != nil)
if err != nil {
return nil, err
}
if expr == nil {
// Count is the only that has no argument so we just return
// true so it counts each value encountered.
expr = &Literal{zed.True}
}
return &Aggregator{
pattern: pattern,
expr: expr,
where: where,
}, nil
}
func (a *Aggregator) NewFunction() agg.Function {
return a.pattern()
}
func (a *Aggregator) Apply(zctx *zed.Context, ectx Context, f agg.Function, this *zed.Value) {
if a.where != nil {
if val, ok := EvalBool(zctx, ectx, this, a.where); !ok || !val.Bool() {
// XXX Issue #3401: do something with "where" errors.
return
}
}
v := a.expr.Eval(ectx, this)
if !v.IsMissing() {
f.Consume(v)
}
}
// NewAggregatorExpr returns an Evaluator from agg. The returned Evaluator
// retains the same functionality of the aggregation only it returns it's
// current state every time a new value is consumed.
func NewAggregatorExpr(agg *Aggregator) Evaluator {
return &aggregatorExpr{agg: agg}
}
type aggregatorExpr struct {
agg *Aggregator
fn agg.Function
zctx *zed.Context
}
var _ Evaluator = (*aggregatorExpr)(nil)
func (s *aggregatorExpr) Eval(ectx Context, val *zed.Value) *zed.Value {
if s.fn == nil {
s.fn = s.agg.NewFunction()
s.zctx = zed.NewContext() //XXX
}
s.agg.Apply(s.zctx, ectx, s.fn, val)
return s.fn.Result(s.zctx)
}