/
table.gen.go
4792 lines (4170 loc) · 121 KB
/
table.gen.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Generated by tmpl
// https://github.com/benbjohnson/tmpl
//
// DO NOT EDIT!
// Source: table.gen.go.tmpl
package storageflux
import (
"fmt"
"math"
"sync"
"github.com/influxdata/flux"
"github.com/influxdata/flux/array"
"github.com/influxdata/flux/arrow"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/interval"
"github.com/influxdata/flux/memory"
"github.com/influxdata/flux/values"
"github.com/influxdata/influxdb/kit/platform/errors"
"github.com/influxdata/influxdb/models"
storage "github.com/influxdata/influxdb/storage/reads"
"github.com/influxdata/influxdb/storage/reads/datatypes"
"github.com/influxdata/influxdb/tsdb/cursors"
)
//
// *********** Float ***********
//
type floatTable struct {
table
mu sync.Mutex
cur cursors.FloatArrayCursor
alloc memory.Allocator
}
func newFloatTable(
done chan struct{},
cur cursors.FloatArrayCursor,
bounds execute.Bounds,
key flux.GroupKey,
cols []flux.ColMeta,
tags models.Tags,
defs [][]byte,
cache *tagsCache,
alloc memory.Allocator,
) *floatTable {
t := &floatTable{
table: newTable(done, bounds, key, cols, defs, cache, alloc),
cur: cur,
}
t.readTags(tags)
t.init(t.advance)
return t
}
func (t *floatTable) Close() {
t.mu.Lock()
if t.cur != nil {
t.cur.Close()
t.cur = nil
}
t.mu.Unlock()
}
func (t *floatTable) Statistics() cursors.CursorStats {
t.mu.Lock()
defer t.mu.Unlock()
cur := t.cur
if cur == nil {
return cursors.CursorStats{}
}
cs := cur.Stats()
return cursors.CursorStats{
ScannedValues: cs.ScannedValues,
ScannedBytes: cs.ScannedBytes,
}
}
func (t *floatTable) Do(f func(flux.ColReader) error) error {
return t.do(f, t.advance)
}
func (t *floatTable) advance() bool {
a := t.cur.Next()
l := a.Len()
if l == 0 {
return false
}
// Retrieve the buffer for the data to avoid allocating
// additional slices. If the buffer is still being used
// because the references were retained, then we will
// allocate a new buffer.
cr := t.allocateBuffer(l)
cr.cols[timeColIdx] = arrow.NewInt(a.Timestamps, t.alloc)
cr.cols[valueColIdx] = t.toArrowBuffer(a.Values)
t.appendTags(cr)
t.appendBounds(cr)
return true
}
// window table
type floatWindowTable struct {
floatTable
arr *cursors.FloatArray
windowBounds interval.Bounds
idxInArr int
createEmpty bool
timeColumn string
window interval.Window
}
func newFloatWindowTable(
done chan struct{},
cur cursors.FloatArrayCursor,
bounds execute.Bounds,
window interval.Window,
createEmpty bool,
timeColumn string,
key flux.GroupKey,
cols []flux.ColMeta,
tags models.Tags,
defs [][]byte,
cache *tagsCache,
alloc memory.Allocator,
) *floatWindowTable {
t := &floatWindowTable{
floatTable: floatTable{
table: newTable(done, bounds, key, cols, defs, cache, alloc),
cur: cur,
},
window: window,
createEmpty: createEmpty,
timeColumn: timeColumn,
}
if t.createEmpty {
start := int64(bounds.Start)
t.windowBounds = window.GetLatestBounds(values.Time(start))
}
t.readTags(tags)
t.init(t.advance)
return t
}
func (t *floatWindowTable) Do(f func(flux.ColReader) error) error {
return t.do(f, t.advance)
}
// createNextBufferTimes will read the timestamps from the array
// cursor and construct the values for the next buffer.
func (t *floatWindowTable) createNextBufferTimes() (start, stop *array.Int, ok bool) {
startB := arrow.NewIntBuilder(t.alloc)
stopB := arrow.NewIntBuilder(t.alloc)
if t.createEmpty {
// There are no more windows when the start time is greater
// than or equal to the stop time.
if startT := int64(t.windowBounds.Start()); startT >= int64(t.bounds.Stop) {
return nil, nil, false
}
// Create a buffer with the buffer size.
// TODO(jsternberg): Calculate the exact size with max points as the maximum.
startB.Resize(storage.MaxPointsPerBlock)
stopB.Resize(storage.MaxPointsPerBlock)
for ; ; t.windowBounds = t.window.NextBounds(t.windowBounds) {
startT, stopT := t.getWindowBoundsFor(t.windowBounds)
if startT >= int64(t.bounds.Stop) {
break
}
startB.Append(startT)
stopB.Append(stopT)
}
start = startB.NewIntArray()
stop = stopB.NewIntArray()
return start, stop, true
}
// Retrieve the next buffer so we can copy the timestamps.
if !t.nextBuffer() {
return nil, nil, false
}
// Copy over the timestamps from the next buffer and adjust
// times for the boundaries.
startB.Resize(len(t.arr.Timestamps))
stopB.Resize(len(t.arr.Timestamps))
for _, stopT := range t.arr.Timestamps {
bounds := t.window.PrevBounds(t.window.GetLatestBounds(values.Time(stopT)))
startT, stopT := t.getWindowBoundsFor(bounds)
startB.Append(startT)
stopB.Append(stopT)
}
start = startB.NewIntArray()
stop = stopB.NewIntArray()
return start, stop, true
}
func (t *floatWindowTable) getWindowBoundsFor(bounds interval.Bounds) (int64, int64) {
beg := int64(bounds.Start())
end := int64(bounds.Stop())
if beg < int64(t.bounds.Start) {
beg = int64(t.bounds.Start)
}
if end > int64(t.bounds.Stop) {
end = int64(t.bounds.Stop)
}
return beg, end
}
// nextAt will retrieve the next value that can be used with
// the given stop timestamp. If no values can be used with the timestamp,
// it will return the default value and false.
func (t *floatWindowTable) nextAt(ts int64) (v float64, ok bool) {
if !t.nextBuffer() {
return
} else if !t.isInWindow(ts, t.arr.Timestamps[t.idxInArr]) {
return
}
v, ok = t.arr.Values[t.idxInArr], true
t.idxInArr++
return v, ok
}
// isInWindow will check if the given time at stop can be used within
// the window stop time for ts. The ts may be a truncated stop time
// because of a restricted boundary while stop will be the true
// stop time returned by storage.
func (t *floatWindowTable) isInWindow(ts int64, stop int64) bool {
// This method checks if the stop time is a valid stop time for
// that interval. This calculation is different from the calculation
// of the window itself. For example, for a 10 second window that
// starts at 20 seconds, we would include points between [20, 30).
// The stop time for this interval would be 30, but because the stop
// time can be truncated, valid stop times range from anywhere between
// (20, 30]. The storage engine will always produce 30 as the end time
// but we may have truncated the stop time because of the boundary
// and this is why we are checking for this range instead of checking
// if the two values are equal.
start := int64(t.window.PrevBounds(t.window.GetLatestBounds(values.Time(stop))).Start())
return start < ts && ts <= stop
}
// nextBuffer will ensure the array cursor is filled
// and will return true if there is at least one value
// that can be read from it.
func (t *floatWindowTable) nextBuffer() bool {
// Discard the current array cursor if we have
// exceeded it.
if t.arr != nil && t.idxInArr >= t.arr.Len() {
t.arr = nil
}
// Retrieve the next array cursor if needed.
if t.arr == nil {
arr := t.cur.Next()
if arr.Len() == 0 {
return false
}
t.arr, t.idxInArr = arr, 0
}
return true
}
// appendValues will scan the timestamps and append values
// that match those timestamps from the buffer.
func (t *floatWindowTable) appendValues(intervals []int64, appendValue func(v float64), appendNull func()) {
for i := 0; i < len(intervals); i++ {
if v, ok := t.nextAt(intervals[i]); ok {
appendValue(v)
continue
}
appendNull()
}
}
func (t *floatWindowTable) advance() bool {
if !t.nextBuffer() {
return false
}
// Create the timestamps for the next window.
start, stop, ok := t.createNextBufferTimes()
if !ok {
return false
}
values := t.mergeValues(stop.Int64Values())
// Retrieve the buffer for the data to avoid allocating
// additional slices. If the buffer is still being used
// because the references were retained, then we will
// allocate a new buffer.
cr := t.allocateBuffer(stop.Len())
if t.timeColumn != "" {
switch t.timeColumn {
case execute.DefaultStopColLabel:
cr.cols[timeColIdx] = stop
start.Release()
case execute.DefaultStartColLabel:
cr.cols[timeColIdx] = start
stop.Release()
}
cr.cols[valueColIdx] = values
t.appendBounds(cr)
} else {
cr.cols[startColIdx] = start
cr.cols[stopColIdx] = stop
cr.cols[valueColIdxWithoutTime] = values
}
t.appendTags(cr)
return true
}
// This table implementation will not have any empty windows.
type floatWindowSelectorTable struct {
floatTable
timeColumn string
window interval.Window
}
func newFloatWindowSelectorTable(
done chan struct{},
cur cursors.FloatArrayCursor,
bounds execute.Bounds,
window interval.Window,
timeColumn string,
key flux.GroupKey,
cols []flux.ColMeta,
tags models.Tags,
defs [][]byte,
cache *tagsCache,
alloc memory.Allocator,
) *floatWindowSelectorTable {
t := &floatWindowSelectorTable{
floatTable: floatTable{
table: newTable(done, bounds, key, cols, defs, cache, alloc),
cur: cur,
},
window: window,
timeColumn: timeColumn,
}
t.readTags(tags)
t.init(t.advance)
return t
}
func (t *floatWindowSelectorTable) Do(f func(flux.ColReader) error) error {
return t.do(f, t.advance)
}
func (t *floatWindowSelectorTable) advance() bool {
arr := t.cur.Next()
if arr.Len() == 0 {
return false
}
cr := t.allocateBuffer(arr.Len())
switch t.timeColumn {
case execute.DefaultStartColLabel:
cr.cols[timeColIdx] = t.startTimes(arr)
t.appendBounds(cr)
case execute.DefaultStopColLabel:
cr.cols[timeColIdx] = t.stopTimes(arr)
t.appendBounds(cr)
default:
cr.cols[startColIdx] = t.startTimes(arr)
cr.cols[stopColIdx] = t.stopTimes(arr)
cr.cols[timeColIdx] = arrow.NewInt(arr.Timestamps, t.alloc)
}
cr.cols[valueColIdx] = t.toArrowBuffer(arr.Values)
t.appendTags(cr)
return true
}
func (t *floatWindowSelectorTable) startTimes(arr *cursors.FloatArray) *array.Int {
start := arrow.NewIntBuilder(t.alloc)
start.Resize(arr.Len())
rangeStart := int64(t.bounds.Start)
for _, v := range arr.Timestamps {
if windowStart := int64(t.window.GetLatestBounds(values.Time(v)).Start()); windowStart < rangeStart {
start.Append(rangeStart)
} else {
start.Append(windowStart)
}
}
return start.NewIntArray()
}
func (t *floatWindowSelectorTable) stopTimes(arr *cursors.FloatArray) *array.Int {
stop := arrow.NewIntBuilder(t.alloc)
stop.Resize(arr.Len())
rangeStop := int64(t.bounds.Stop)
for _, v := range arr.Timestamps {
if windowStop := int64(t.window.GetLatestBounds(values.Time(v)).Stop()); windowStop > rangeStop {
stop.Append(rangeStop)
} else {
stop.Append(windowStop)
}
}
return stop.NewIntArray()
}
// This table implementation may contain empty windows
// in addition to non-empty windows.
type floatEmptyWindowSelectorTable struct {
floatTable
arr *cursors.FloatArray
idx int
rangeStart int64
rangeStop int64
windowBounds interval.Bounds
timeColumn string
window interval.Window
}
func newFloatEmptyWindowSelectorTable(
done chan struct{},
cur cursors.FloatArrayCursor,
bounds execute.Bounds,
window interval.Window,
timeColumn string,
key flux.GroupKey,
cols []flux.ColMeta,
tags models.Tags,
defs [][]byte,
cache *tagsCache,
alloc memory.Allocator,
) *floatEmptyWindowSelectorTable {
rangeStart := int64(bounds.Start)
rangeStop := int64(bounds.Stop)
t := &floatEmptyWindowSelectorTable{
floatTable: floatTable{
table: newTable(done, bounds, key, cols, defs, cache, alloc),
cur: cur,
},
arr: cur.Next(),
rangeStart: rangeStart,
rangeStop: rangeStop,
windowBounds: window.GetLatestBounds(values.Time(rangeStart)),
window: window,
timeColumn: timeColumn,
}
t.readTags(tags)
t.init(t.advance)
return t
}
func (t *floatEmptyWindowSelectorTable) Do(f func(flux.ColReader) error) error {
return t.do(f, t.advance)
}
func (t *floatEmptyWindowSelectorTable) advance() bool {
if t.arr.Len() == 0 {
return false
}
values := t.arrowBuilder()
values.Resize(storage.MaxPointsPerBlock)
var cr *colReader
switch t.timeColumn {
case execute.DefaultStartColLabel:
start := t.startTimes(values)
cr = t.allocateBuffer(start.Len())
cr.cols[timeColIdx] = start
t.appendBounds(cr)
case execute.DefaultStopColLabel:
stop := t.stopTimes(values)
cr = t.allocateBuffer(stop.Len())
cr.cols[timeColIdx] = stop
t.appendBounds(cr)
default:
start, stop, time := t.startStopTimes(values)
cr = t.allocateBuffer(time.Len())
cr.cols[startColIdx] = start
cr.cols[stopColIdx] = stop
cr.cols[timeColIdx] = time
}
cr.cols[valueColIdx] = values.NewFloatArray()
t.appendTags(cr)
return true
}
func (t *floatEmptyWindowSelectorTable) startTimes(builder *array.FloatBuilder) *array.Int {
start := arrow.NewIntBuilder(t.alloc)
start.Resize(storage.MaxPointsPerBlock)
for int64(t.windowBounds.Start()) < t.rangeStop {
// The first window should start at the
// beginning of the time range.
if int64(t.windowBounds.Start()) < t.rangeStart {
start.Append(t.rangeStart)
} else {
start.Append(int64(t.windowBounds.Start()))
}
var v int64
if t.arr.Len() == 0 {
v = math.MaxInt64
} else {
v = t.arr.Timestamps[t.idx]
}
// If the current timestamp falls within the
// current window, append the value to the
// builder, otherwise append a null value.
if int64(t.windowBounds.Start()) <= v && v < int64(t.windowBounds.Stop()) {
t.append(builder, t.arr.Values[t.idx])
t.idx++
} else {
builder.AppendNull()
}
t.windowBounds = t.window.NextBounds(t.windowBounds)
// If the current array is non-empty and has
// been read in its entirety, call Next().
if t.arr.Len() > 0 && t.idx == t.arr.Len() {
t.arr = t.cur.Next()
t.idx = 0
}
if start.Len() == storage.MaxPointsPerBlock {
break
}
}
return start.NewIntArray()
}
func (t *floatEmptyWindowSelectorTable) stopTimes(builder *array.FloatBuilder) *array.Int {
stop := arrow.NewIntBuilder(t.alloc)
stop.Resize(storage.MaxPointsPerBlock)
for int64(t.windowBounds.Start()) < t.rangeStop {
// The last window should stop at the end of
// the time range.
if int64(t.windowBounds.Stop()) > t.rangeStop {
stop.Append(t.rangeStop)
} else {
stop.Append(int64(t.windowBounds.Stop()))
}
var v int64
if t.arr.Len() == 0 {
v = math.MaxInt64
} else {
v = t.arr.Timestamps[t.idx]
}
// If the current timestamp falls within the
// current window, append the value to the
// builder, otherwise append a null value.
if int64(t.windowBounds.Start()) <= v && v < int64(t.windowBounds.Stop()) {
t.append(builder, t.arr.Values[t.idx])
t.idx++
} else {
builder.AppendNull()
}
t.windowBounds = t.window.NextBounds(t.windowBounds)
// If the current array is non-empty and has
// been read in its entirety, call Next().
if t.arr.Len() > 0 && t.idx == t.arr.Len() {
t.arr = t.cur.Next()
t.idx = 0
}
if stop.Len() == storage.MaxPointsPerBlock {
break
}
}
return stop.NewIntArray()
}
func (t *floatEmptyWindowSelectorTable) startStopTimes(builder *array.FloatBuilder) (*array.Int, *array.Int, *array.Int) {
start := arrow.NewIntBuilder(t.alloc)
start.Resize(storage.MaxPointsPerBlock)
stop := arrow.NewIntBuilder(t.alloc)
stop.Resize(storage.MaxPointsPerBlock)
time := arrow.NewIntBuilder(t.alloc)
time.Resize(storage.MaxPointsPerBlock)
for int64(t.windowBounds.Start()) < t.rangeStop {
// The first window should start at the
// beginning of the time range.
if int64(t.windowBounds.Start()) < t.rangeStart {
start.Append(t.rangeStart)
} else {
start.Append(int64(t.windowBounds.Start()))
}
// The last window should stop at the end of
// the time range.
if int64(t.windowBounds.Stop()) > t.rangeStop {
stop.Append(t.rangeStop)
} else {
stop.Append(int64(t.windowBounds.Stop()))
}
var v int64
if t.arr.Len() == 0 {
v = math.MaxInt64
} else {
v = t.arr.Timestamps[t.idx]
}
// If the current timestamp falls within the
// current window, append the value to the
// builder, otherwise append a null value.
if int64(t.windowBounds.Start()) <= v && v < int64(t.windowBounds.Stop()) {
time.Append(v)
t.append(builder, t.arr.Values[t.idx])
t.idx++
} else {
time.AppendNull()
builder.AppendNull()
}
t.windowBounds = t.window.NextBounds(t.windowBounds)
// If the current array is non-empty and has
// been read in its entirety, call Next().
if t.arr.Len() > 0 && t.idx == t.arr.Len() {
t.arr = t.cur.Next()
t.idx = 0
}
if time.Len() == storage.MaxPointsPerBlock {
break
}
}
return start.NewIntArray(), stop.NewIntArray(), time.NewIntArray()
}
// group table
type floatGroupTable struct {
table
mu sync.Mutex
gc storage.GroupCursor
cur cursors.FloatArrayCursor
}
func newFloatGroupTable(
done chan struct{},
gc storage.GroupCursor,
cur cursors.FloatArrayCursor,
bounds execute.Bounds,
key flux.GroupKey,
cols []flux.ColMeta,
tags models.Tags,
defs [][]byte,
cache *tagsCache,
alloc memory.Allocator,
) *floatGroupTable {
t := &floatGroupTable{
table: newTable(done, bounds, key, cols, defs, cache, alloc),
gc: gc,
cur: cur,
}
t.readTags(tags)
t.init(t.advance)
return t
}
func (t *floatGroupTable) Close() {
t.mu.Lock()
if t.cur != nil {
t.cur.Close()
t.cur = nil
}
if t.gc != nil {
t.gc.Close()
t.gc = nil
}
t.mu.Unlock()
}
func (t *floatGroupTable) Do(f func(flux.ColReader) error) error {
return t.do(f, t.advance)
}
func (t *floatGroupTable) advance() bool {
if t.cur == nil {
// For group aggregates, we will try to get all the series and all table buffers within those series
// all at once and merge them into one row when this advance() function is first called.
// At the end of this process, t.advanceCursor() already returns false and t.cur becomes nil.
// But we still need to return true to indicate that there is data to be returned.
// The second time when we call this advance(), t.cur is already nil, so we directly return false.
return false
}
var arr *cursors.FloatArray
var len int
for {
arr = t.cur.Next()
len = arr.Len()
if len > 0 {
break
}
if !t.advanceCursor() {
return false
}
}
// handle the group without aggregate case
if t.gc.Aggregate() == nil {
// Retrieve the buffer for the data to avoid allocating
// additional slices. If the buffer is still being used
// because the references were retained, then we will
// allocate a new buffer.
colReader := t.allocateBuffer(len)
colReader.cols[timeColIdx] = arrow.NewInt(arr.Timestamps, t.alloc)
colReader.cols[valueColIdx] = t.toArrowBuffer(arr.Values)
t.appendTags(colReader)
t.appendBounds(colReader)
return true
}
aggregate, err := makeFloatAggregateAccumulator(t.gc.Aggregate().Type)
if err != nil {
t.err = err
return false
}
aggregate.AccumulateFirst(arr.Timestamps, arr.Values, t.tags)
for {
arr = t.cur.Next()
if arr.Len() > 0 {
aggregate.AccumulateMore(arr.Timestamps, arr.Values, t.tags)
continue
}
if !t.advanceCursor() {
break
}
}
timestamp, value, tags := aggregate.Result()
colReader := t.allocateBuffer(1)
if IsSelector(t.gc.Aggregate()) {
colReader.cols[timeColIdx] = arrow.NewInt([]int64{timestamp}, t.alloc)
colReader.cols[valueColIdx] = t.toArrowBuffer([]float64{value})
} else {
colReader.cols[valueColIdxWithoutTime] = t.toArrowBuffer([]float64{value})
}
t.appendTheseTags(colReader, tags)
t.appendBounds(colReader)
return true
}
type FloatAggregateAccumulator interface {
// AccumulateFirst receives an initial array of items to select from.
// It selects an item and stores the state. Afterwards, more data can
// be supplied with AccumulateMore and the results can be requested at
// any time. Without a call to AccumulateFirst the results are not
// defined.
AccumulateFirst(timestamps []int64, values []float64, tags [][]byte)
// AccumulateMore receives additional array elements to select from.
AccumulateMore(timestamps []int64, values []float64, tags [][]byte)
// Result returns the item selected from the data received so far.
Result() (int64, float64, [][]byte)
}
// The selector method takes a ( timestamp, value ) pair, a
// ( []timestamp, []value ) pair, and a starting index. It applies the selector
// to the single value and the array, starting at the supplied index. It
// returns -1 if the single value is selected and a non-negative value if an
// item from the array is selected.
type floatSelectorMethod func(int64, float64, []int64, []float64, int) int
// The selector accumulator tracks currently-selected item.
type floatSelectorAccumulator struct {
selector floatSelectorMethod
ts int64
v float64
tags [][]byte
}
func (a *floatSelectorAccumulator) AccumulateFirst(timestamps []int64, values []float64, tags [][]byte) {
index := a.selector(timestamps[0], values[0], timestamps, values, 1)
if index < 0 {
a.ts = timestamps[0]
a.v = values[0]
} else {
a.ts = timestamps[index]
a.v = values[index]
}
a.tags = make([][]byte, len(tags))
copy(a.tags, tags)
}
func (a *floatSelectorAccumulator) AccumulateMore(timestamps []int64, values []float64, tags [][]byte) {
index := a.selector(a.ts, a.v, timestamps, values, 0)
if index >= 0 {
a.ts = timestamps[index]
a.v = values[index]
if len(tags) > cap(a.tags) {
a.tags = make([][]byte, len(tags))
} else {
a.tags = a.tags[:len(tags)]
}
copy(a.tags, tags)
}
}
func (a *floatSelectorAccumulator) Result() (int64, float64, [][]byte) {
return a.ts, a.v, a.tags
}
// The aggregate method takes a value, an array of values, and a starting
// index, applies an aggregate operation over the value and the array, starting
// at the given index, and returns the result.
type floatAggregateMethod func(float64, []float64, int) float64
type floatAggregateAccumulator struct {
aggregate floatAggregateMethod
accum float64
// For pure aggregates it doesn't matter what we return for tags, but
// we need to satisfy the interface. We will just return the most
// recently seen tags.
tags [][]byte
}
func (a *floatAggregateAccumulator) AccumulateFirst(timestamps []int64, values []float64, tags [][]byte) {
a.accum = a.aggregate(values[0], values, 1)
a.tags = tags
}
func (a *floatAggregateAccumulator) AccumulateMore(timestamps []int64, values []float64, tags [][]byte) {
a.accum = a.aggregate(a.accum, values, 0)
a.tags = tags
}
// For group aggregates (non-selectors), the timestamp is always math.MaxInt64.
// their final result does not contain _time, so this timestamp value can be
// anything and it won't matter.
func (a *floatAggregateAccumulator) Result() (int64, float64, [][]byte) {
return math.MaxInt64, a.accum, a.tags
}
// makeFloatAggregateAccumulator returns the interface implementation for
// aggregating returned points within the same group. The incoming points are
// the ones returned for each series and the struct returned here will
// aggregate the aggregates.
func makeFloatAggregateAccumulator(agg datatypes.Aggregate_AggregateType) (FloatAggregateAccumulator, error) {
switch agg {
case datatypes.Aggregate_AggregateTypeFirst:
return &floatSelectorAccumulator{selector: selectorFirstGroupsFloat}, nil
case datatypes.Aggregate_AggregateTypeLast:
return &floatSelectorAccumulator{selector: selectorLastGroupsFloat}, nil
case datatypes.Aggregate_AggregateTypeCount:
return nil, &errors.Error{
Code: errors.EInvalid,
Msg: "unsupported for aggregate count: Float",
}
case datatypes.Aggregate_AggregateTypeSum:
return &floatAggregateAccumulator{aggregate: aggregateSumGroupsFloat}, nil
case datatypes.Aggregate_AggregateTypeMin:
return &floatSelectorAccumulator{selector: selectorMinGroupsFloat}, nil
case datatypes.Aggregate_AggregateTypeMax:
return &floatSelectorAccumulator{selector: selectorMaxGroupsFloat}, nil
default:
return nil, &errors.Error{
Code: errors.EInvalid,
Msg: fmt.Sprintf("unknown/unimplemented aggregate type: %v", agg),
}
}
}
func selectorMinGroupsFloat(ts int64, v float64, timestamps []int64, values []float64, i int) int {
index := -1
for ; i < len(values); i++ {
if v > values[i] {
index = i
v = values[i]
}
}
return index
}
func selectorMaxGroupsFloat(ts int64, v float64, timestamps []int64, values []float64, i int) int {
index := -1
for ; i < len(values); i++ {
if v < values[i] {
index = i
v = values[i]
}
}
return index
}
func aggregateSumGroupsFloat(sum float64, values []float64, i int) float64 {
for ; i < len(values); i++ {
sum += values[i]
}
return sum
}
func selectorFirstGroupsFloat(ts int64, v float64, timestamps []int64, values []float64, i int) int {
index := -1
for ; i < len(values); i++ {
if ts > timestamps[i] {
index = i
ts = timestamps[i]
}
}
return index
}
func selectorLastGroupsFloat(ts int64, v float64, timestamps []int64, values []float64, i int) int {
index := -1
for ; i < len(values); i++ {
if ts < timestamps[i] {
index = i
ts = timestamps[i]
}
}
return index
}
func (t *floatGroupTable) advanceCursor() bool {
t.cur.Close()
t.cur = nil
for t.gc.Next() {
cur := t.gc.Cursor()
if cur == nil {
continue
}
if typedCur, ok := cur.(cursors.FloatArrayCursor); !ok {
// TODO(sgc): error or skip?
cur.Close()
t.err = &errors.Error{
Code: errors.EInvalid,
Err: &GroupCursorError{
typ: "float",
cursor: cur,
},
}
return false
} else {
t.readTags(t.gc.Tags())
t.cur = typedCur
return true
}
}
return false
}
func (t *floatGroupTable) Statistics() cursors.CursorStats {
if t.cur == nil {
return cursors.CursorStats{}
}
cs := t.cur.Stats()
return cursors.CursorStats{
ScannedValues: cs.ScannedValues,
ScannedBytes: cs.ScannedBytes,
}