forked from brimdata/super
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagg.go
88 lines (82 loc) · 1.85 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package agg
import (
"fmt"
"github.com/brimdata/zed"
"github.com/brimdata/zed/pkg/anymath"
)
// A Pattern is a template for creating instances of aggregator functions.
// NewPattern returns a pattern of the type that should be created and
// an instance is created by simply invoking the pattern funtion.
type Pattern func() Function
// MaxValueSize is a limit on an individual aggregation value since sets
// and arrays could otherwise grow without limit leading to a single record
// value that cannot fit in memory.
const MaxValueSize = 20 * 1024 * 1024
type Function interface {
Consume(*zed.Value)
ConsumeAsPartial(*zed.Value)
Result(*zed.Context) *zed.Value
ResultAsPartial(*zed.Context) *zed.Value
}
func NewPattern(op string, hasarg bool) (Pattern, error) {
needarg := true
var pattern Pattern
switch op {
case "count":
needarg = false
pattern = func() Function {
var c Count
return &c
}
case "any":
pattern = func() Function {
return &Any{}
}
case "avg":
pattern = func() Function {
return &Avg{}
}
case "dcount":
pattern = func() Function {
return NewDCount()
}
case "fuse":
pattern = func() Function {
return newFuse()
}
case "sum":
pattern = func() Function {
return newMathReducer(anymath.Add)
}
case "min":
pattern = func() Function {
return newMathReducer(anymath.Min)
}
case "max":
pattern = func() Function {
return newMathReducer(anymath.Max)
}
case "union":
pattern = func() Function {
return newUnion()
}
case "collect":
pattern = func() Function {
return &Collect{}
}
case "and":
pattern = func() Function {
return &And{}
}
case "or":
pattern = func() Function {
return &Or{}
}
default:
return nil, fmt.Errorf("unknown aggregation function: %s", op)
}
if needarg && !hasarg {
return nil, fmt.Errorf("%s: argument required", op)
}
return pattern, nil
}