-
Notifications
You must be signed in to change notification settings - Fork 40
/
aggtdigest.go
179 lines (166 loc) · 5.46 KB
/
aggtdigest.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
// Copyright 2023 Sneller, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vm
import (
"encoding/binary"
"fmt"
"log"
"math"
"github.com/SnellerInc/sneller/internal/percentile"
)
// tDigestDataSize is the total number of bytes used by the tDigest Aggregation.
// 1st element (float32): total sum of weights;
// 2nd element (float32): maximum value (from the aggregated values);
// 3rd element (float32): minimum value (from the aggregated values);
// 4th element (uint32): number of centroids (range 0, 32).
// These four elements make 16 bytes, the remaining bytes contain upto 32 centroids;
// a centroid contains a 'mean' and a 'weight'. First 32 weights (float32) are stored,
// followed by 32 means (float32), if the positions are used (because there are not
// that many centroids), mean of +inf and weight of 1 is used. The remaining bytes are
// used as scratch by the aggregator code.
const tDigestDataSize = 16 + 13*64
// tDigestDS data-structure for tDigest
type tDigestDS []byte
func (t tDigestDS) putLen(len int) {
binary.LittleEndian.PutUint32(t[12:], uint32(len))
}
func (t tDigestDS) getLen() int {
return int(binary.LittleEndian.Uint32(t[12:]))
}
func (t tDigestDS) putWeightSum(sum float32) {
binary.LittleEndian.PutUint32(t[0:], math.Float32bits(sum))
}
func (t tDigestDS) getWeightSum() float32 {
return math.Float32frombits(binary.LittleEndian.Uint32(t[0:]))
}
func (t tDigestDS) putMeanMax(sum float32) {
binary.LittleEndian.PutUint32(t[4:], math.Float32bits(sum))
}
func (t tDigestDS) getMeanMax() float32 {
return math.Float32frombits(binary.LittleEndian.Uint32(t[4:]))
}
func (t tDigestDS) putMeanMin(sum float32) {
binary.LittleEndian.PutUint32(t[8:], math.Float32bits(sum))
}
func (t tDigestDS) getMeanMin() float32 {
return math.Float32frombits(binary.LittleEndian.Uint32(t[8:]))
}
func (t tDigestDS) putWeight(weight float32, idx int) {
offset := 16 + (0 * 64) + (idx * 4)
binary.LittleEndian.PutUint32(t[offset:], math.Float32bits(weight))
}
func (t tDigestDS) getWeight(idx int) float32 {
offset := 16 + (0 * 64) + (idx * 4)
return math.Float32frombits(binary.LittleEndian.Uint32(t[offset:]))
}
func (t tDigestDS) putMean(weight float32, idx int) {
offset := 16 + (2 * 64) + (idx * 4)
binary.LittleEndian.PutUint32(t[offset:], math.Float32bits(weight))
}
func (t tDigestDS) getMean(idx int) float32 {
offset := 16 + (2 * 64) + (idx * 4)
return math.Float32frombits(binary.LittleEndian.Uint32(t[offset:]))
}
func (t tDigestDS) clear() {
for i := 0; i < tDigestDataSize; i++ {
t[i] = 0
}
}
//lint:ignore U1000 kept for testing purposes
func (t tDigestDS) debugDump() string {
weightSum := t.getWeightSum()
meanMax := t.getMeanMax()
meanMin := t.getMeanMin()
lenIn := t.getLen()
if lenIn > 32 {
log.Printf("lenIn %v is too large", lenIn)
lenIn = 32
}
result := fmt.Sprintf("weightSum %v; meanMax %v; meanMin %v; lenIn %v\n", weightSum, meanMax, meanMin, lenIn)
for i := 0; i < lenIn; i++ {
result += fmt.Sprintf("mean %v; weight %v\n", t.getMean(i), t.getWeight(i))
}
return result
}
// tDigestInit initializes an aggregation buffer
func tDigestInit(data []byte) {
for i := range data {
data[i] = 0
}
}
func createTDigest(data tDigestDS) (*percentile.TDigest, error) {
lenIn := data.getLen()
if lenIn > 32 {
return nil, fmt.Errorf("while creating tDigest from data: number of centroids %v is too large (max 32)", lenIn)
}
t := percentile.TDigest{
Data: make(percentile.CentroidsT, lenIn),
TotalWeight: data.getWeightSum(),
Max: data.getMeanMax(),
Min: data.getMeanMin(),
}
for i := 0; i < lenIn; i++ {
t.Data[i].Weight = data.getWeight(i)
t.Data[i].Mean = data.getMean(i)
}
return &t, nil
}
// createDs fills data with the provided tDigest content
func createDs(tDigest *percentile.TDigest, data tDigestDS) {
nCentroids := tDigest.Data.Len()
data.putLen(nCentroids)
if nCentroids > 0 {
data.putWeightSum(tDigest.TotalWeight)
data.putMeanMax(tDigest.Max)
data.putMeanMin(tDigest.Min)
for i := 0; i < nCentroids; i++ {
data.putWeight(tDigest.Data[i].Weight, i)
data.putMean(tDigest.Data[i].Mean, i)
}
}
}
// tDigestMerge merges src with dst buffer
func tDigestMerge(dst, src tDigestDS) error {
lenSrc := src.getLen()
if lenSrc > 0 {
lenDst := dst.getLen()
if lenDst == 0 {
// source contains processed data while dst does not: only copy from src to dst
copy(dst[:tDigestDataSize], src[:tDigestDataSize])
} else {
// both source and dst contain processed data: merge both results into dst
t1, err1 := createTDigest(src)
if err1 != nil {
return err1
}
t2, err2 := createTDigest(dst)
if err2 != nil {
return err2
}
t1.Merge(t2, 16)
createDs(t1, dst)
}
src.clear()
}
// else: source is empty: do nothing
return nil
}
// calcPercentiles calculates approximate percentiles using the tDigest data
func calcPercentiles(data tDigestDS, p []float32) ([]float32, error) {
t, err := createTDigest(data)
if err != nil {
return nil, err
}
return t.Percentiles(p), nil
}