forked from lytics/hll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigquery.go
291 lines (255 loc) · 6.26 KB
/
bigquery.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
package hll
import (
"encoding/binary"
"fmt"
"io"
"math/bits"
"github.com/golang/protobuf/proto"
)
const DefaultBigqueryP = 15
const DefaultBigqueryPPrime = 20
func getField(buf *proto.Buffer) (uint64, uint64, error) {
x, err := buf.DecodeVarint()
if err != nil {
return 0, 0, err
}
f := x >> 3 & 0b111
t := x & 0b111
return f, t, nil
}
func NewHllFromBigquery(data []byte) (*Hll, error) {
if len(data) == 0 {
return NewHll(DefaultBigqueryP, DefaultBigqueryPPrime), nil
}
var typ, numValues, encodingVersion, valueType, sparseSize, precision, sparsePrecision uint64
var normalData, sparseData []byte
buf := proto.NewBuffer(data)
for len(data) > 0 {
f, t, err := getField(buf)
if err == io.ErrUnexpectedEOF {
break
} else if err != nil {
return nil, err
}
if f == 1 {
if t != 0 {
return nil, fmt.Errorf("unexpected type: %d", t)
}
typ, err = buf.DecodeVarint()
if err != nil {
return nil, err
}
} else if f == 2 {
if t != 0 {
return nil, fmt.Errorf("unexpected type: %d", t)
}
numValues, err = buf.DecodeVarint()
if err != nil {
return nil, err
}
} else if f == 3 {
if t != 0 {
return nil, fmt.Errorf("unexpected type: %d", t)
}
encodingVersion, err = buf.DecodeVarint()
if err != nil {
return nil, err
}
} else if f == 4 {
if t != 0 {
return nil, fmt.Errorf("unexpected type: %d", t)
}
valueType, err = buf.DecodeVarint()
if err != nil {
return nil, err
}
} else {
_, err := buf.DecodeVarint() // ignore size
if err != nil {
return nil, err
}
for len(data) > 0 {
f, t, err := getField(buf)
if err == io.ErrUnexpectedEOF {
break
} else if err != nil {
return nil, err
}
if f == 2 { // SPARSE_SIZE_TAG
if t != 0 {
return nil, fmt.Errorf("unexpected type: %d", t)
}
sparseSize, err = buf.DecodeVarint()
if err != nil {
return nil, err
}
} else if f == 3 {
if t != 0 {
return nil, fmt.Errorf("unexpected type: %d", t)
}
precision, err = buf.DecodeVarint()
if err != nil {
return nil, err
}
} else if f == 4 {
if t != 0 {
return nil, fmt.Errorf("unexpected type: %d", t)
}
sparsePrecision, err = buf.DecodeVarint()
if err != nil {
return nil, err
}
} else if f == 5 {
normalData, err = buf.DecodeRawBytes(true)
if err != nil {
return nil, err
}
} else if f == 6 {
sparseData, err = buf.DecodeRawBytes(true)
if err != nil {
return nil, err
}
} else {
fmt.Println(f, t)
break
}
}
break
}
}
if typ != 112 {
return nil, fmt.Errorf("unexpected type: %d", typ)
}
if encodingVersion != 2 {
return nil, fmt.Errorf("unsupported encodingVersion: %d", encodingVersion)
}
h := NewHll(uint(precision), uint(sparsePrecision))
if len(normalData) > 0 {
if len(sparseData) > 0 {
return nil, fmt.Errorf("cna not have both normal and sparse data")
}
h.switchToNormal()
for idx, r := range normalData {
h.bigM.Set(uint64(idx), r)
}
} else {
h.sparseList.buf = sparseData
h.sparseList.numElements = sparseSize
it := h.sparseList.GetIterator()
for {
last, ok := it()
if !ok {
break
}
h.sparseList.lastVal = last
}
}
// We ignore the number of values the aggregator has seen as we don't keep track of this.
_ = numValues
// We ignore valueType, it is the type of data that was put into the aggregator.
// See: https://github.com/google/zetasketch/blob/a2f2692fae8cf61103330f9f70e696c4ba8b94b0/java/com/google/zetasketch/HyperLogLogPlusPlus.java#L442-L459
_ = valueType
return h, nil
}
const K0 uint64 = 0xa5b85c5e198ed849
const K1 uint64 = 0x8d58ac26afe12e47
const K2 uint64 = 0xc47b6e9e3a970ed3
const K3 uint64 = 0xc6a4a7935bd1e995
func load64(b []byte, offset int) uint64 {
return binary.LittleEndian.Uint64(b[offset:])
}
func load64Safely(b []byte, offset, length int) uint64 {
result := uint64(0)
limit := 8
if length < limit {
limit = length
}
for i := 0; i < limit; i++ {
// Shift value left while iterating logically through the array.
result |= uint64(b[offset+i]&0xFF) << (i * 8)
}
return result
}
func rotateRight(x uint64, k int) uint64 {
return bits.RotateLeft64(x, -k)
}
func shiftMix(val uint64) uint64 {
return val ^ (val >> 47)
}
func hash128to64(high, low uint64) uint64 {
a := (low ^ high) * K3
a ^= (a >> 47)
b := (high ^ a) * K3
b ^= (b >> 47)
b *= K3
return b
}
func hashLength33To64(bytes []byte, offset, length int) uint64 {
z := load64(bytes, offset+24)
a := load64(bytes, offset) + (uint64(length)+load64(bytes, offset+length-16))*K0
b := rotateRight(a+z, 52)
c := rotateRight(a, 37)
a += load64(bytes, offset+8)
c += rotateRight(a, 7)
a += load64(bytes, offset+16)
vf := a + z
vs := b + rotateRight(a, 31) + c
a = load64(bytes, offset+16) + load64(bytes, offset+length-32)
z = load64(bytes, offset+length-8)
b = rotateRight(a+z, 52)
c = rotateRight(a, 37)
a += load64(bytes, offset+length-24)
c += rotateRight(a, 7)
a += load64(bytes, offset+length-16)
wf := a + z
ws := b + rotateRight(a, 31) + c
r := shiftMix((vf+ws)*K2 + (wf+vs)*K0)
return shiftMix(r*K0+vs) * K2
}
func murmurHash64WithSeed(bytes []byte, offset, length int, seed uint64) uint64 {
mul := K3
topBit := 0x7
lengthAligned := length & (^topBit)
lengthRemainder := length & topBit
hash := seed ^ (uint64(length) * mul)
for i := 0; i < lengthAligned; i += 8 {
loaded := load64(bytes, offset+i)
data := shiftMix(loaded*mul) * mul
hash ^= data
hash *= mul
}
if lengthRemainder != 0 {
data := load64Safely(bytes, offset+lengthAligned, lengthRemainder)
hash ^= data
hash *= mul
}
hash = shiftMix(hash) * mul
hash = shiftMix(hash)
return hash
}
func BigQueryHash(s string) uint64 {
bytes := []byte(s)
offset := 0
length := len(bytes)
var result uint64
if length <= 32 {
result = murmurHash64WithSeed(bytes, offset, length, K0^K1^K2)
} else if length <= 64 {
result = hashLength33To64(bytes, offset, length)
} else {
panic("unsupported input length")
}
u := K0
if length >= 8 {
u = load64(bytes, offset)
}
v := K0
if length >= 9 {
v = load64(bytes, offset+length-8)
}
result = hash128to64(result+v, u)
if result == 0 || result == 1 {
return result + ^uint64(1)
}
return result
}