-
Notifications
You must be signed in to change notification settings - Fork 153
/
sample.go
192 lines (161 loc) · 4.29 KB
/
sample.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
182
183
184
185
186
187
188
189
190
191
192
package universe
import (
"fmt"
"math/rand"
"github.com/apache/arrow/go/arrow/array"
"github.com/influxdata/flux"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/semantic"
)
const SampleKind = "sample"
type SampleOpSpec struct {
N int64 `json:"n"`
Pos int64 `json:"pos"`
execute.SelectorConfig
}
func init() {
sampleSignature := execute.SelectorSignature(
map[string]semantic.PolyType{
"n": semantic.Int,
"pos": semantic.Int,
},
[]string{"n"},
)
flux.RegisterPackageValue("universe", SampleKind, flux.FunctionValue(SampleKind, createSampleOpSpec, sampleSignature))
flux.RegisterOpSpec(SampleKind, newSampleOp)
plan.RegisterProcedureSpec(SampleKind, newSampleProcedure, SampleKind)
execute.RegisterTransformation(SampleKind, createSampleTransformation)
}
func createSampleOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
spec := new(SampleOpSpec)
n, err := args.GetRequiredInt("n")
if err != nil {
return nil, err
} else if n <= 0 {
return nil, fmt.Errorf("n must be a positive integer, but was %d", n)
}
spec.N = n
if pos, ok, err := args.GetInt("pos"); err != nil {
return nil, err
} else if ok {
if pos >= spec.N {
return nil, fmt.Errorf("pos must be less than n, but %d >= %d", pos, spec.N)
}
spec.Pos = pos
} else {
spec.Pos = -1
}
if err := spec.SelectorConfig.ReadArgs(args); err != nil {
return nil, err
}
return spec, nil
}
func newSampleOp() flux.OperationSpec {
return new(SampleOpSpec)
}
func (s *SampleOpSpec) Kind() flux.OperationKind {
return SampleKind
}
type SampleProcedureSpec struct {
N int64
Pos int64
execute.SelectorConfig
}
func newSampleProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*SampleOpSpec)
if !ok {
return nil, fmt.Errorf("invalid spec type %T", qs)
}
return &SampleProcedureSpec{
N: spec.N,
Pos: spec.Pos,
SelectorConfig: spec.SelectorConfig,
}, nil
}
func (s *SampleProcedureSpec) Kind() plan.ProcedureKind {
return SampleKind
}
func (s *SampleProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(SampleProcedureSpec)
ns.N = s.N
ns.Pos = s.Pos
ns.SelectorConfig = s.SelectorConfig
return ns
}
// TriggerSpec implements plan.TriggerAwareProcedureSpec
func (s *SampleProcedureSpec) TriggerSpec() plan.TriggerSpec {
return plan.NarrowTransformationTriggerSpec{}
}
type SampleSelector struct {
N int
Pos int
offset int
selected []int
}
func createSampleTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
ps, ok := spec.(*SampleProcedureSpec)
if !ok {
return nil, nil, fmt.Errorf("invalid spec type %T", ps)
}
ss := &SampleSelector{
N: int(ps.N),
Pos: int(ps.Pos),
}
t, d := execute.NewIndexSelectorTransformationAndDataset(id, mode, ss, ps.SelectorConfig, a.Allocator())
return t, d, nil
}
func (s *SampleSelector) reset() {
pos := s.Pos
if pos < 0 {
pos = rand.Intn(s.N)
}
s.offset = pos
}
func (s *SampleSelector) NewBoolSelector() execute.DoBoolIndexSelector {
s.reset()
return s
}
func (s *SampleSelector) NewIntSelector() execute.DoIntIndexSelector {
s.reset()
return s
}
func (s *SampleSelector) NewUIntSelector() execute.DoUIntIndexSelector {
s.reset()
return s
}
func (s *SampleSelector) NewFloatSelector() execute.DoFloatIndexSelector {
s.reset()
return s
}
func (s *SampleSelector) NewStringSelector() execute.DoStringIndexSelector {
s.reset()
return s
}
func (s *SampleSelector) selectSample(l int) []int {
var i int
s.selected = s.selected[0:0]
for i = s.offset; i < l; i += s.N {
s.selected = append(s.selected, i)
}
s.offset = i - l
return s.selected
}
func (s *SampleSelector) DoBool(vs *array.Boolean) []int {
return s.selectSample(vs.Len())
}
func (s *SampleSelector) DoInt(vs *array.Int64) []int {
return s.selectSample(vs.Len())
}
func (s *SampleSelector) DoUInt(vs *array.Uint64) []int {
return s.selectSample(vs.Len())
}
func (s *SampleSelector) DoFloat(vs *array.Float64) []int {
return s.selectSample(vs.Len())
}
func (s *SampleSelector) DoString(vs *array.Binary) []int {
return s.selectSample(vs.Len())
}