forked from influxdata/flux
/
max.go
181 lines (156 loc) · 4.17 KB
/
max.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package universe
import (
"github.com/InfluxCommunity/flux"
"github.com/InfluxCommunity/flux/array"
"github.com/InfluxCommunity/flux/codes"
"github.com/InfluxCommunity/flux/execute"
"github.com/InfluxCommunity/flux/internal/errors"
"github.com/InfluxCommunity/flux/plan"
"github.com/InfluxCommunity/flux/runtime"
)
const MaxKind = "max"
type MaxOpSpec struct {
execute.SelectorConfig
}
func init() {
maxSignature := runtime.MustLookupBuiltinType("universe", "max")
runtime.RegisterPackageValue("universe", MaxKind, flux.MustValue(flux.FunctionValue(MaxKind, CreateMaxOpSpec, maxSignature)))
plan.RegisterProcedureSpec(MaxKind, newMaxProcedure, MaxKind)
execute.RegisterTransformation(MaxKind, createMaxTransformation)
}
func CreateMaxOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
spec := new(MaxOpSpec)
if err := spec.SelectorConfig.ReadArgs(args); err != nil {
return nil, err
}
return spec, nil
}
func (s *MaxOpSpec) Kind() flux.OperationKind {
return MaxKind
}
type MaxProcedureSpec struct {
execute.SelectorConfig
}
func newMaxProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*MaxOpSpec)
if !ok {
return nil, errors.Newf(codes.Internal, "invalid spec type %T", qs)
}
return &MaxProcedureSpec{
SelectorConfig: spec.SelectorConfig,
}, nil
}
func (s *MaxProcedureSpec) Kind() plan.ProcedureKind {
return MaxKind
}
func (s *MaxProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(MaxProcedureSpec)
ns.SelectorConfig = s.SelectorConfig
return ns
}
// TriggerSpec implements plan.TriggerAwareProcedureSpec
func (s *MaxProcedureSpec) TriggerSpec() plan.TriggerSpec {
return plan.NarrowTransformationTriggerSpec{}
}
type MaxSelector struct {
set bool
rows []execute.Row
}
func createMaxTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
ps, ok := spec.(*MaxProcedureSpec)
if !ok {
return nil, nil, errors.Newf(codes.Internal, "invalid spec type %T", ps)
}
t, d := execute.NewRowSelectorTransformationAndDataset(id, mode, new(MaxSelector), ps.SelectorConfig, a.Allocator())
return t, d, nil
}
type MaxIntSelector struct {
MaxSelector
max int64
}
type MaxUIntSelector struct {
MaxSelector
max uint64
}
type MaxFloatSelector struct {
MaxSelector
max float64
}
type MaxTimeSelector struct {
MaxIntSelector
}
func (s *MaxSelector) NewTimeSelector() execute.DoTimeRowSelector {
return new(MaxTimeSelector)
}
func (s *MaxSelector) NewBoolSelector() execute.DoBoolRowSelector {
return nil
}
func (s *MaxSelector) NewIntSelector() execute.DoIntRowSelector {
return new(MaxIntSelector)
}
func (s *MaxSelector) NewUIntSelector() execute.DoUIntRowSelector {
return new(MaxUIntSelector)
}
func (s *MaxSelector) NewFloatSelector() execute.DoFloatRowSelector {
return new(MaxFloatSelector)
}
func (s *MaxSelector) NewStringSelector() execute.DoStringRowSelector {
return nil
}
func (s *MaxSelector) Rows() []execute.Row {
if !s.set {
return nil
}
return s.rows
}
func (s *MaxSelector) selectRow(idx int, cr flux.ColReader) {
// Capture row
if idx >= 0 {
s.rows = []execute.Row{execute.ReadRow(idx, cr)}
}
}
func (s *MaxTimeSelector) DoTime(vs *array.Int, cr flux.ColReader) {
s.MaxIntSelector.DoInt(vs, cr)
}
func (s *MaxIntSelector) DoInt(vs *array.Int, cr flux.ColReader) {
maxIdx := -1
for i := 0; i < vs.Len(); i++ {
if vs.IsValid(i) {
if v := vs.Value(i); !s.set || v > s.max {
s.set = true
s.max = v
maxIdx = i
}
}
}
s.selectRow(maxIdx, cr)
}
func (s *MaxUIntSelector) DoUInt(vs *array.Uint, cr flux.ColReader) {
maxIdx := -1
for i := 0; i < vs.Len(); i++ {
if vs.IsValid(i) {
if v := vs.Value(i); !s.set || v > s.max {
s.set = true
s.max = v
maxIdx = i
}
}
}
s.selectRow(maxIdx, cr)
}
func (s *MaxFloatSelector) DoFloat(vs *array.Float, cr flux.ColReader) {
maxIdx := -1
for i := 0; i < vs.Len(); i++ {
if vs.IsValid(i) {
if v := vs.Value(i); !s.set || v > s.max {
s.set = true
s.max = v
maxIdx = i
}
}
}
s.selectRow(maxIdx, cr)
}