forked from influxdata/flux
/
histogram.go
511 lines (442 loc) · 13.6 KB
/
histogram.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package universe
import (
"context"
"math"
"regexp"
"sort"
"github.com/InfluxCommunity/flux"
"github.com/InfluxCommunity/flux/codes"
"github.com/InfluxCommunity/flux/execute"
"github.com/InfluxCommunity/flux/internal/errors"
"github.com/InfluxCommunity/flux/interpreter"
"github.com/InfluxCommunity/flux/plan"
"github.com/InfluxCommunity/flux/runtime"
"github.com/InfluxCommunity/flux/semantic"
"github.com/InfluxCommunity/flux/values"
)
const HistogramKind = "histogram"
type HistogramOpSpec struct {
Column string `json:"column"`
UpperBoundColumn string `json:"upperBoundColumn"`
CountColumn string `json:"countColumn"`
Bins []float64 `json:"bins"`
Normalize bool `json:"normalize"`
}
func init() {
histogramSignature := runtime.MustLookupBuiltinType("universe", "histogram")
runtime.RegisterPackageValue("universe", HistogramKind, flux.MustValue(flux.FunctionValue(HistogramKind, CreateHistogramOpSpec, histogramSignature)))
runtime.RegisterPackageValue("universe", "linearBins", linearBins{})
runtime.RegisterPackageValue("universe", "logarithmicBins", logarithmicBins{})
plan.RegisterProcedureSpec(HistogramKind, newHistogramProcedure, HistogramKind)
execute.RegisterTransformation(HistogramKind, createHistogramTransformation)
}
func CreateHistogramOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
spec := new(HistogramOpSpec)
if col, ok, err := args.GetString("column"); err != nil {
return nil, err
} else if ok {
spec.Column = col
} else {
spec.Column = execute.DefaultValueColLabel
}
if col, ok, err := args.GetString("upperBoundColumn"); err != nil {
return nil, err
} else if ok {
spec.UpperBoundColumn = col
} else {
spec.UpperBoundColumn = DefaultUpperBoundColumnLabel
}
if col, ok, err := args.GetString("countColumn"); err != nil {
return nil, err
} else if ok {
spec.CountColumn = col
} else {
spec.CountColumn = execute.DefaultValueColLabel
}
binsArry, err := args.GetRequiredArray("bins", semantic.Float)
if err != nil {
return nil, err
}
spec.Bins, err = interpreter.ToFloatArray(binsArry)
if err != nil {
return nil, err
}
if normalize, ok, err := args.GetBool("normalize"); err != nil {
return nil, err
} else if ok {
spec.Normalize = normalize
}
return spec, nil
}
func (s *HistogramOpSpec) Kind() flux.OperationKind {
return HistogramKind
}
type HistogramProcedureSpec struct {
plan.DefaultCost
HistogramOpSpec
}
func newHistogramProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
spec, ok := qs.(*HistogramOpSpec)
if !ok {
return nil, errors.Newf(codes.Internal, "invalid spec type %T", qs)
}
return &HistogramProcedureSpec{
HistogramOpSpec: *spec,
}, nil
}
func (s *HistogramProcedureSpec) Kind() plan.ProcedureKind {
return HistogramKind
}
func (s *HistogramProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(HistogramProcedureSpec)
*ns = *s
if len(s.Bins) > 0 {
ns.Bins = make([]float64, len(s.Bins))
copy(ns.Bins, s.Bins)
}
return ns
}
func createHistogramTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*HistogramProcedureSpec)
if !ok {
return nil, nil, errors.Newf(codes.Internal, "invalid spec type %T", spec)
}
cache := execute.NewTableBuilderCache(a.Allocator())
d := execute.NewDataset(id, mode, cache)
t := NewHistogramTransformation(d, cache, s)
return t, d, nil
}
type histogramTransformation struct {
execute.ExecutionNode
d execute.Dataset
cache execute.TableBuilderCache
spec HistogramProcedureSpec
}
func NewHistogramTransformation(d execute.Dataset, cache execute.TableBuilderCache, spec *HistogramProcedureSpec) *histogramTransformation {
sort.Float64s(spec.Bins)
return &histogramTransformation{
d: d,
cache: cache,
spec: *spec,
}
}
func (t *histogramTransformation) RetractTable(id execute.DatasetID, key flux.GroupKey) error {
return t.d.RetractTable(key)
}
func (t *histogramTransformation) Process(id execute.DatasetID, tbl flux.Table) error {
builder, created := t.cache.TableBuilder(tbl.Key())
if !created {
return errors.Newf(codes.FailedPrecondition, "histogram found duplicate table with key: %v", tbl.Key())
}
valueIdx := execute.ColIdx(t.spec.Column, tbl.Cols())
if valueIdx < 0 {
return errors.Newf(codes.FailedPrecondition, "column %q is missing", t.spec.Column)
}
if col := tbl.Cols()[valueIdx]; col.Type != flux.TFloat {
return errors.Newf(codes.FailedPrecondition, "column %q must be a float got %v", t.spec.Column, col.Type)
}
err := execute.AddTableKeyCols(tbl.Key(), builder)
if err != nil {
return err
}
boundIdx, err := builder.AddCol(flux.ColMeta{
Label: t.spec.UpperBoundColumn,
Type: flux.TFloat,
})
if err != nil {
return err
}
countIdx, err := builder.AddCol(flux.ColMeta{
Label: t.spec.CountColumn,
Type: flux.TFloat,
})
if err != nil {
return err
}
totalRows := 0.0
counts := make([]float64, len(t.spec.Bins))
err = tbl.Do(func(cr flux.ColReader) error {
vs := cr.Floats(valueIdx)
totalRows += float64(vs.Len() - vs.NullN())
for i := 0; i < vs.Len(); i++ {
if vs.IsNull(i) {
continue
}
v := vs.Value(i)
idx := sort.Search(len(t.spec.Bins), func(i int) bool {
return v <= t.spec.Bins[i]
})
if idx >= len(t.spec.Bins) {
// Greater than highest bin, or not found
return errors.Newf(codes.OutOfRange, "found value greater than any bin, %d %d %f %f", idx, len(t.spec.Bins), v, t.spec.Bins[len(t.spec.Bins)-1])
}
// Increment counter
counts[idx]++
}
return nil
})
if err != nil {
return err
}
// Add records making counts cumulative
total := 0.0
for i, v := range counts {
if err := execute.AppendKeyValues(tbl.Key(), builder); err != nil {
return err
}
count := v + total
if t.spec.Normalize {
count /= totalRows
}
if err := builder.AppendFloat(countIdx, count); err != nil {
return err
}
if err := builder.AppendFloat(boundIdx, t.spec.Bins[i]); err != nil {
return err
}
total += v
}
return nil
}
func (t *histogramTransformation) UpdateWatermark(id execute.DatasetID, mark execute.Time) error {
return t.d.UpdateWatermark(mark)
}
func (t *histogramTransformation) UpdateProcessingTime(id execute.DatasetID, pt execute.Time) error {
return t.d.UpdateProcessingTime(pt)
}
func (t *histogramTransformation) Finish(id execute.DatasetID, err error) {
t.d.Finish(err)
}
// linearBins is a helper function for creating bins spaced linearly
type linearBins struct{}
func (b linearBins) Dynamic() values.Dynamic {
panic(values.UnexpectedKind(b.Type().Nature(), semantic.Dynamic))
}
var linearBinsType = runtime.MustLookupBuiltinType("universe", "linearBins")
func (b linearBins) Type() semantic.MonoType {
return linearBinsType
}
func (b linearBins) IsNull() bool {
return false
}
func (b linearBins) Str() string {
panic(values.UnexpectedKind(semantic.String, semantic.Function))
}
func (b linearBins) Bytes() []byte {
panic(values.UnexpectedKind(semantic.Function, semantic.Bytes))
}
func (b linearBins) Int() int64 {
panic(values.UnexpectedKind(semantic.Int, semantic.Function))
}
func (b linearBins) UInt() uint64 {
panic(values.UnexpectedKind(semantic.UInt, semantic.Function))
}
func (b linearBins) Float() float64 {
panic(values.UnexpectedKind(semantic.Float, semantic.Function))
}
func (b linearBins) Bool() bool {
panic(values.UnexpectedKind(semantic.Bool, semantic.Function))
}
func (b linearBins) Time() values.Time {
panic(values.UnexpectedKind(semantic.Time, semantic.Function))
}
func (b linearBins) Duration() values.Duration {
panic(values.UnexpectedKind(semantic.Duration, semantic.Function))
}
func (b linearBins) Regexp() *regexp.Regexp {
panic(values.UnexpectedKind(semantic.Regexp, semantic.Function))
}
func (b linearBins) Array() values.Array {
panic(values.UnexpectedKind(semantic.Array, semantic.Function))
}
func (b linearBins) Object() values.Object {
panic(values.UnexpectedKind(semantic.Object, semantic.Function))
}
func (b linearBins) Function() values.Function {
return b
}
func (b linearBins) Dict() values.Dictionary {
panic(values.UnexpectedKind(semantic.Dictionary, semantic.Function))
}
func (b linearBins) Vector() values.Vector {
panic(values.UnexpectedKind(semantic.Vector, semantic.Function))
}
func (b linearBins) Equal(rhs values.Value) bool {
if b.Type() != rhs.Type() {
return false
}
_, ok := rhs.(linearBins)
return ok
}
func (b linearBins) Retain() {}
func (b linearBins) Release() {}
func (b linearBins) HasSideEffect() bool {
return false
}
func (b linearBins) Call(ctx context.Context, args values.Object) (values.Value, error) {
startV, ok := args.Get("start")
if !ok {
return nil, errors.New(codes.Invalid, "start is required")
}
if startV.Type().Nature() != semantic.Float {
return nil, errors.New(codes.Invalid, "start must be a float")
}
widthV, ok := args.Get("width")
if !ok {
return nil, errors.New(codes.Invalid, "width is required")
}
if widthV.Type().Nature() != semantic.Float {
return nil, errors.New(codes.Invalid, "width must be a float")
}
countV, ok := args.Get("count")
if !ok {
return nil, errors.New(codes.Invalid, "count is required")
}
if countV.Type().Nature() != semantic.Int {
return nil, errors.New(codes.Invalid, "count must be an int")
}
infV, ok := args.Get("infinity")
if !ok {
infV = values.NewBool(true)
}
if infV.Type().Nature() != semantic.Bool {
return nil, errors.New(codes.Invalid, "infinity must be a bool")
}
start := startV.Float()
width := widthV.Float()
count := countV.Int()
inf := infV.Bool()
l := int(count)
if inf {
l++
}
elements := make([]values.Value, l)
bound := start
for i := 0; i < l; i++ {
elements[i] = values.NewFloat(bound)
bound += width
}
if inf {
elements[l-1] = values.NewFloat(math.Inf(1))
}
counts := values.NewArrayWithBacking(semantic.NewArrayType(semantic.BasicFloat), elements)
return counts, nil
}
// logarithmicBins is a helper function for creating bins spaced by an logarithmic factor.
type logarithmicBins struct{}
func (b logarithmicBins) Dynamic() values.Dynamic {
panic(values.UnexpectedKind(b.Type().Nature(), semantic.Dynamic))
}
var logarithmicBinsType = runtime.MustLookupBuiltinType("universe", "logarithmicBins")
func (b logarithmicBins) Type() semantic.MonoType {
return logarithmicBinsType
}
func (b logarithmicBins) IsNull() bool {
return false
}
func (b logarithmicBins) Str() string {
panic(values.UnexpectedKind(semantic.String, semantic.Function))
}
func (b logarithmicBins) Bytes() []byte {
panic(values.UnexpectedKind(semantic.Function, semantic.Bytes))
}
func (b logarithmicBins) Int() int64 {
panic(values.UnexpectedKind(semantic.Int, semantic.Function))
}
func (b logarithmicBins) UInt() uint64 {
panic(values.UnexpectedKind(semantic.UInt, semantic.Function))
}
func (b logarithmicBins) Float() float64 {
panic(values.UnexpectedKind(semantic.Float, semantic.Function))
}
func (b logarithmicBins) Bool() bool {
panic(values.UnexpectedKind(semantic.Bool, semantic.Function))
}
func (b logarithmicBins) Time() values.Time {
panic(values.UnexpectedKind(semantic.Time, semantic.Function))
}
func (b logarithmicBins) Duration() values.Duration {
panic(values.UnexpectedKind(semantic.Duration, semantic.Function))
}
func (b logarithmicBins) Regexp() *regexp.Regexp {
panic(values.UnexpectedKind(semantic.Regexp, semantic.Function))
}
func (b logarithmicBins) Array() values.Array {
panic(values.UnexpectedKind(semantic.Array, semantic.Function))
}
func (b logarithmicBins) Object() values.Object {
panic(values.UnexpectedKind(semantic.Object, semantic.Function))
}
func (b logarithmicBins) Function() values.Function {
return b
}
func (b logarithmicBins) Dict() values.Dictionary {
panic(values.UnexpectedKind(semantic.Dictionary, semantic.Function))
}
func (b logarithmicBins) Vector() values.Vector {
panic(values.UnexpectedKind(semantic.Vector, semantic.Function))
}
func (b logarithmicBins) Equal(rhs values.Value) bool {
if b.Type() != rhs.Type() {
return false
}
_, ok := rhs.(logarithmicBins)
return ok
}
func (b logarithmicBins) Retain() {}
func (b logarithmicBins) Release() {}
func (b logarithmicBins) HasSideEffect() bool {
return false
}
func (b logarithmicBins) Call(ctx context.Context, args values.Object) (values.Value, error) {
startV, ok := args.Get("start")
if !ok {
return nil, errors.New(codes.Invalid, "start is required")
}
if startV.Type().Nature() != semantic.Float {
return nil, errors.New(codes.Invalid, "start must be a float")
}
factorV, ok := args.Get("factor")
if !ok {
return nil, errors.New(codes.Invalid, "factor is required")
}
if factorV.Type().Nature() != semantic.Float {
return nil, errors.New(codes.Invalid, "factor must be a float")
}
countV, ok := args.Get("count")
if !ok {
return nil, errors.New(codes.Invalid, "count is required")
}
if countV.Type().Nature() != semantic.Int {
return nil, errors.New(codes.Invalid, "count must be an int")
}
infV, ok := args.Get("infinity")
if !ok {
infV = values.NewBool(true)
}
if infV.Type().Nature() != semantic.Bool {
return nil, errors.New(codes.Invalid, "infinity must be a bool")
}
start := startV.Float()
factor := factorV.Float()
count := countV.Int()
inf := infV.Bool()
l := int(count)
if inf {
l++
}
elements := make([]values.Value, l)
bound := start
for i := 0; i < l; i++ {
elements[i] = values.NewFloat(bound)
bound *= factor
}
if inf {
elements[l-1] = values.NewFloat(math.Inf(1))
}
counts := values.NewArrayWithBacking(semantic.NewArrayType(semantic.BasicFloat), elements)
return counts, nil
}