forked from dgraph-io/dgraph
/
aggregator.go
344 lines (322 loc) · 7.9 KB
/
aggregator.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
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package query
import (
"bytes"
"math"
"time"
"gopkg.in/adibiarsotp/dgraph.v83/protos"
"gopkg.in/adibiarsotp/dgraph.v83/types"
"gopkg.in/adibiarsotp/dgraph.v83/x"
)
type aggregator struct {
name string
result types.Val
count int // used when we need avergae.
}
func isUnary(f string) bool {
return f == "ln" || f == "exp" || f == "u-" || f == "sqrt" ||
f == "floor" || f == "ceil" || f == "since"
}
func isBinaryBoolean(f string) bool {
return f == "<" || f == ">" || f == "<=" || f == ">=" ||
f == "==" || f == "!="
}
func isTernary(f string) bool {
return f == "cond"
}
func isBinary(f string) bool {
return f == "+" || f == "*" || f == "-" || f == "/" || f == "%" ||
f == "max" || f == "min" || f == "logbase" || f == "pow"
}
func convertTo(from *protos.TaskValue) (types.Val, error) {
vh, _ := getValue(from)
if bytes.Equal(from.Val, x.Nilbyte) {
return vh, ErrEmptyVal
}
va, err := types.Convert(vh, vh.Tid)
if err != nil {
return vh, x.Wrapf(err, "Fail to convert from protos.Value to types.Val")
}
return va, err
}
func compareValues(ag string, va, vb types.Val) (bool, error) {
if !isBinaryBoolean(ag) {
x.Fatalf("Function %v is not binary boolean", ag)
}
isLess, err := types.Less(va, vb)
if err != nil {
//Try to convert values.
if va.Tid == types.IntID {
va.Tid = types.FloatID
va.Value = float64(va.Value.(int64))
} else if vb.Tid == types.IntID {
vb.Tid = types.FloatID
vb.Value = float64(vb.Value.(int64))
} else {
return false, err
}
}
isLess, err = types.Less(va, vb)
isMore, err := types.Less(vb, va)
if err != nil {
return false, err
}
switch ag {
case "<":
return isLess, nil
case ">":
return isMore, nil
case "<=":
return isLess && !isMore, nil
case ">=":
return isMore && !isLess, nil
case "==":
return !isMore && !isLess, nil
case "!=":
return isMore || isLess, nil
default:
return false, x.Errorf("Invalid compare function %v", ag)
}
return false, nil
}
func (ag *aggregator) ApplyVal(v types.Val) error {
if v.Value == nil {
// If the value is missing, treat it as 0.
v.Value = int64(0)
v.Tid = types.IntID
}
var isIntOrFloat bool
var l float64
if v.Tid == types.IntID {
l = float64(v.Value.(int64))
v.Value = l
v.Tid = types.FloatID
isIntOrFloat = true
} else if v.Tid == types.FloatID {
l = v.Value.(float64)
isIntOrFloat = true
}
// If its not int or float, keep the type.
var res types.Val
if isUnary(ag.name) {
switch ag.name {
case "ln":
if !isIntOrFloat {
return x.Errorf("Wrong type encountered for func %v", ag.name)
}
v.Value = math.Log(l)
res = v
case "exp":
if !isIntOrFloat {
return x.Errorf("Wrong type encountered for func %v", ag.name)
}
v.Value = math.Exp(l)
res = v
case "u-":
if !isIntOrFloat {
return x.Errorf("Wrong type encountered for func %v", ag.name)
}
v.Value = -l
res = v
case "sqrt":
if !isIntOrFloat {
return x.Errorf("Wrong type encountered for func %v", ag.name)
}
v.Value = math.Sqrt(l)
res = v
case "floor":
if !isIntOrFloat {
return x.Errorf("Wrong type encountered for func %v", ag.name)
}
v.Value = math.Floor(l)
res = v
case "ceil":
if !isIntOrFloat {
return x.Errorf("Wrong type encountered for func %v", ag.name)
}
v.Value = math.Ceil(l)
res = v
case "since":
if v.Tid == types.DateTimeID {
v.Value = float64(time.Since(v.Value.(time.Time))) / 1000000000.0
v.Tid = types.FloatID
} else {
return x.Errorf("Wrong type encountered for func %v", ag.name)
}
res = v
}
ag.result = res
return nil
}
if ag.result.Value == nil {
ag.result = v
return nil
}
va := ag.result
if va.Tid != types.IntID && va.Tid != types.FloatID {
isIntOrFloat = false
}
switch ag.name {
case "+":
if !isIntOrFloat {
return x.Errorf("Wrong type encountered for func %v", ag.name)
}
va.Value = va.Value.(float64) + l
res = va
case "-":
if !isIntOrFloat {
return x.Errorf("Wrong type encountered for func %v", ag.name)
}
va.Value = va.Value.(float64) - l
res = va
case "*":
if !isIntOrFloat {
return x.Errorf("Wrong type encountered for func %v", ag.name)
}
va.Value = va.Value.(float64) * l
res = va
case "/":
if !isIntOrFloat {
return x.Errorf("Wrong type encountered for func %v %v %v", ag.name, va.Tid, v.Tid)
}
va.Value = va.Value.(float64) / l
res = va
case "%":
if !isIntOrFloat {
return x.Errorf("Wrong type encountered for func %v", ag.name)
}
va.Value = math.Mod(va.Value.(float64), l)
res = va
case "pow":
if !isIntOrFloat {
return x.Errorf("Wrong type encountered for func %v", ag.name)
}
va.Value = math.Pow(va.Value.(float64), l)
res = va
case "logbase":
if l == 1 {
return nil
}
if !isIntOrFloat {
return x.Errorf("Wrong type encountered for func %v", ag.name)
}
va.Value = math.Log(va.Value.(float64)) / math.Log(l)
res = va
case "min":
r, err := types.Less(va, v)
if err == nil && !r {
res = v
} else {
res = va
}
case "max":
r, err := types.Less(va, v)
if err == nil && r {
res = v
} else {
res = va
}
default:
return x.Errorf("Unhandled aggregator function %v", ag.name)
}
ag.result = res
return nil
}
func (ag *aggregator) Apply(val types.Val) {
if ag.result.Value == nil {
ag.result = val
ag.count++
return
}
va := ag.result
vb := val
var res types.Val
switch ag.name {
case "min":
r, err := types.Less(va, vb)
if err == nil && !r {
res = vb
} else {
res = va
}
case "max":
r, err := types.Less(va, vb)
if err == nil && r {
res = vb
} else {
res = va
}
case "sum", "avg":
if va.Tid == types.IntID && vb.Tid == types.IntID {
va.Value = va.Value.(int64) + vb.Value.(int64)
} else if va.Tid == types.FloatID && vb.Tid == types.FloatID {
va.Value = va.Value.(float64) + vb.Value.(float64)
} else {
// This pair cannot be summed. So pass.
}
res = va
default:
x.Fatalf("Unhandled aggregator function %v", ag.name)
}
ag.count++
ag.result = res
}
func (ag *aggregator) ValueMarshalled() (*protos.TaskValue, error) {
data := types.ValueForType(types.BinaryID)
ag.divideByCount()
res := &protos.TaskValue{ValType: int32(ag.result.Tid), Val: x.Nilbyte}
if ag.result.Value == nil {
return res, nil
}
// We'll divide it by the count if it's an avg aggregator.
err := types.Marshal(ag.result, &data)
if err != nil {
return res, err
}
res.Val = data.Value.([]byte)
return res, nil
}
func (ag *aggregator) divideByCount() {
if ag.name != "avg" || ag.count == 0 || ag.result.Value == nil {
return
}
var v float64
if ag.result.Tid == types.IntID {
v = float64(ag.result.Value.(int64))
} else if ag.result.Tid == types.FloatID {
v = ag.result.Value.(float64)
}
ag.result.Tid = types.FloatID
ag.result.Value = v / float64(ag.count)
}
func (ag *aggregator) Value() (types.Val, error) {
if ag.result.Value == nil {
return ag.result, ErrEmptyVal
}
ag.divideByCount()
if ag.result.Tid == types.FloatID {
if math.IsInf(ag.result.Value.(float64), 1) {
ag.result.Value = math.MaxFloat64
} else if math.IsInf(ag.result.Value.(float64), -1) {
ag.result.Value = -1 * math.MaxFloat64
} else if math.IsNaN(ag.result.Value.(float64)) {
ag.result.Value = 0.0
}
}
return ag.result, nil
}