/
count.go
129 lines (112 loc) · 3.37 KB
/
count.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package universe
import (
"github.com/influxdata/flux"
"github.com/influxdata/flux/array"
"github.com/influxdata/flux/codes"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/internal/errors"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/runtime"
)
const CountKind = "count"
type CountOpSpec struct {
execute.SimpleAggregateConfig
}
func init() {
countSignature := runtime.MustLookupBuiltinType("universe", "count")
runtime.RegisterPackageValue("universe", CountKind, flux.MustValue(flux.FunctionValue(CountKind, CreateCountOpSpec, countSignature)))
plan.RegisterProcedureSpec(CountKind, newCountProcedure, CountKind)
execute.RegisterTransformation(CountKind, createCountTransformation)
}
func CreateCountOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
s := new(CountOpSpec)
if err := s.SimpleAggregateConfig.ReadArgs(args); err != nil {
return nil, err
}
return s, nil
}
func (s *CountOpSpec) Kind() flux.OperationKind {
return CountKind
}
type CountProcedureSpec struct {
execute.SimpleAggregateConfig
}
func newCountProcedure(qs flux.OperationSpec, a plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*CountOpSpec)
if !ok {
return nil, errors.Newf(codes.Internal, "invalid spec type %T", qs)
}
return &CountProcedureSpec{
SimpleAggregateConfig: spec.SimpleAggregateConfig,
}, nil
}
func (s *CountProcedureSpec) Kind() plan.ProcedureKind {
return CountKind
}
func (s *CountProcedureSpec) Copy() plan.ProcedureSpec {
return &CountProcedureSpec{
SimpleAggregateConfig: s.SimpleAggregateConfig,
}
}
func (s *CountProcedureSpec) AggregateMethod() string {
return CountKind
}
func (s *CountProcedureSpec) ReAggregateSpec() plan.ProcedureSpec {
return new(SumProcedureSpec)
}
// TriggerSpec implements plan.TriggerAwareProcedureSpec
func (s *CountProcedureSpec) TriggerSpec() plan.TriggerSpec {
return plan.NarrowTransformationTriggerSpec{}
}
type CountAgg struct {
count int64
}
func createCountTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*CountProcedureSpec)
if !ok {
return nil, nil, errors.Newf(codes.Internal, "invalid spec type %T", spec)
}
return execute.NewSimpleAggregateTransformation(a.Context(), id, new(CountAgg), s.SimpleAggregateConfig, a.Allocator())
}
func (a *CountAgg) NewBoolAgg() execute.DoBoolAgg {
return new(CountAgg)
}
func (a *CountAgg) NewIntAgg() execute.DoIntAgg {
return new(CountAgg)
}
func (a *CountAgg) NewUIntAgg() execute.DoUIntAgg {
return new(CountAgg)
}
func (a *CountAgg) NewFloatAgg() execute.DoFloatAgg {
return new(CountAgg)
}
func (a *CountAgg) NewStringAgg() execute.DoStringAgg {
return new(CountAgg)
}
func (a *CountAgg) DoBool(vs *array.Boolean) {
a.count += int64(vs.Len())
}
func (a *CountAgg) DoUInt(vs *array.Uint) {
a.count += int64(vs.Len())
}
func (a *CountAgg) DoInt(vs *array.Int) {
a.count += int64(vs.Len())
}
func (a *CountAgg) DoFloat(vs *array.Float) {
a.count += int64(vs.Len())
}
func (a *CountAgg) DoString(vs *array.String) {
a.count += int64(vs.Len())
}
func (a *CountAgg) Type() flux.ColType {
return flux.TInt
}
func (a *CountAgg) ValueInt() int64 {
return a.count
}
func (a *CountAgg) IsNull() bool {
return false
}