-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathaggregator.go
551 lines (473 loc) · 12.5 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
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
/*
* Copyright 2017-2018 Dgraph Labs, Inc. and Contributors
*
* 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 query
import (
"bytes"
"math"
"time"
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/types"
"github.com/dgraph-io/dgraph/x"
"github.com/pkg/errors"
)
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 *pb.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, errors.Wrapf(err, "Fail to convert from api.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)
}
_, 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)
if err != nil {
return false, err
}
isMore, err := types.Less(vb, va)
if err != nil {
return false, err
}
isEqual, err := types.Equal(va, vb)
if err != nil {
return false, err
}
switch ag {
case "<":
return isLess, nil
case ">":
return isMore, nil
case "<=":
return isLess || isEqual, nil
case ">=":
return isMore || isEqual, nil
case "==":
return isEqual, nil
case "!=":
return !isEqual, nil
}
return false, errors.Errorf("Invalid compare function %q", ag)
}
func ApplyIntAdd(a, b, c *types.Val) error {
c.Value = a.Value.(int64) + b.Value.(int64)
return nil
}
func ApplyFloatAdd(a, b, c *types.Val) error {
c.Value = a.Value.(float64) + b.Value.(float64)
return nil
}
func ApplyOtherAdd(a, b, c *types.Val) error {
return errors.Errorf("Wrong type encountered for func +")
}
func ApplyIntSub(a, b, c *types.Val) error {
c.Value = a.Value.(int64) - b.Value.(int64)
return nil
}
func ApplyFloatSub(a, b, c *types.Val) error {
c.Value = a.Value.(float64) - b.Value.(float64)
return nil
}
func ApplyOtherSub(a, b, c *types.Val) error {
return errors.Errorf("Wrong type encountered for func -")
}
func ApplyIntMul(a, b, c *types.Val) error {
c.Value = a.Value.(int64) * b.Value.(int64)
return nil
}
func ApplyFloatMul(a, b, c *types.Val) error {
c.Value = a.Value.(float64) * b.Value.(float64)
return nil
}
func ApplyOtherMul(a, b, c *types.Val) error {
return errors.Errorf("Wrong type encountered for func *")
}
func ApplyIntDiv(a, b, c *types.Val) error {
if b.Value.(int64) == 0 {
return errors.Errorf("Division by zero")
}
c.Value = a.Value.(int64) / b.Value.(int64)
return nil
}
func ApplyFloatDiv(a, b, c *types.Val) error {
if b.Value.(float64) == 0 {
return errors.Errorf("Division by zero")
}
c.Value = a.Value.(float64) / b.Value.(float64)
return nil
}
func ApplyOtherDiv(a, b, c *types.Val) error {
return errors.Errorf("Wrong type encountered for func /")
}
func ApplyIntMod(a, b, c *types.Val) error {
if b.Value.(int64) == 0 {
return errors.Errorf("Division by zero")
}
c.Value = a.Value.(int64) % b.Value.(int64)
return nil
}
func ApplyFloatMod(a, b, c *types.Val) error {
if b.Value.(float64) == 0 {
return errors.Errorf("Division by zero")
}
c.Value = math.Mod(a.Value.(float64), b.Value.(float64))
return nil
}
func ApplyOtherMod(a, b, c *types.Val) error {
return errors.Errorf("Wrong type encountered for func %s", "%")
}
func ApplyIntPow(a, b, c *types.Val) error {
c.Value = math.Pow(float64(a.Value.(int64)), float64(b.Value.(int64)))
c.Tid = types.FloatID
return nil
}
func ApplyFloatPow(a, b, c *types.Val) error {
c.Value = math.Pow(a.Value.(float64), b.Value.(float64))
return nil
}
func ApplyOtherPow(a, b, c *types.Val) error {
return errors.Errorf("Wrong type encountered for func ^")
}
func ApplyIntLog(a, b, c *types.Val) error {
c.Value = math.Log(float64(a.Value.(int64))) / math.Log(float64(b.Value.(int64)))
c.Tid = types.FloatID
return nil
}
func ApplyFloatLog(a, b, c *types.Val) error {
c.Value = math.Log(a.Value.(float64)) / math.Log(b.Value.(float64))
return nil
}
func ApplyOtherLog(a, b, c *types.Val) error {
return errors.Errorf("Wrong type encountered for func ln")
}
func ApplyMin(a, b, c *types.Val) error {
r, err := types.Less(*a, *b)
if err != nil {
return err
}
if r {
*c = *a
return nil
}
*c = *b
return nil
}
func ApplyMax(a, b, c *types.Val) error {
r, err := types.Less(*a, *b)
if err != nil {
return err
}
if r {
*c = *b
return nil
}
*c = *a
return nil
}
func ApplyLnInt(a, res *types.Val) error {
res.Value = math.Log(float64(a.Value.(int64)))
res.Tid = types.FloatID
return nil
}
func ApplyLnFloat(a, res *types.Val) error {
res.Value = math.Log(a.Value.(float64))
return nil
}
func ApplyLnOther(a, res *types.Val) error {
return errors.Errorf("Wrong type encountered for func ln")
}
func ApplyExpInt(a, res *types.Val) error {
res.Value = math.Exp(float64(a.Value.(int64)))
res.Tid = types.FloatID
return nil
}
func ApplyExpFloat(a, res *types.Val) error {
res.Value = math.Exp(a.Value.(float64))
return nil
}
func ApplyExpOther(a, res *types.Val) error {
return errors.Errorf("Wrong type encountered for func exp")
}
func ApplyNegInt(a, res *types.Val) error {
res.Value = -a.Value.(int64)
return nil
}
func ApplyNegFloat(a, res *types.Val) error {
res.Value = -a.Value.(float64)
return nil
}
func ApplyNegOther(a, res *types.Val) error {
return errors.Errorf("Wrong type encountered for func u-")
}
func ApplySqrtInt(a, res *types.Val) error {
res.Value = math.Sqrt(float64(a.Value.(int64)))
res.Tid = types.FloatID
return nil
}
func ApplySqrtFloat(a, res *types.Val) error {
res.Value = math.Sqrt(a.Value.(float64))
return nil
}
func ApplySqrtOther(a, res *types.Val) error {
return errors.Errorf("Wrong type encountered for func sqrt")
}
func ApplyFloorInt(a, res *types.Val) error {
res.Value = a.Value.(int64)
return nil
}
func ApplyFloorFloat(a, res *types.Val) error {
res.Value = math.Floor(a.Value.(float64))
return nil
}
func ApplyFloorOther(a, res *types.Val) error {
return errors.Errorf("Wrong type encountered for func floor")
}
func ApplyCeilInt(a, res *types.Val) error {
res.Value = a.Value.(int64)
return nil
}
func ApplyCeilFloat(a, res *types.Val) error {
res.Value = math.Ceil(a.Value.(float64))
return nil
}
func ApplyCeilOther(a, res *types.Val) error {
return errors.Errorf("Wrong type encountered for fun ceil")
}
func ApplySince(a, res *types.Val) error {
if a.Tid == types.DateTimeID {
a.Value = float64(time.Since(a.Value.(time.Time))) / 1000000000.0
a.Tid = types.FloatID
*res = *a
return nil
}
return errors.Errorf("Wrong type encountered for func since")
}
type MathUnaryFunction func(a, res *types.Val) error
type MathBinaryFunction func(a, b, res *types.Val) error
var supportedUnaryFunctions = map[string][]MathUnaryFunction{
"ln": {ApplyLnInt, ApplyLnFloat, ApplyLnOther},
"exp": {ApplyExpInt, ApplyExpFloat, ApplyExpOther},
"u-": {ApplyNegInt, ApplyNegFloat, ApplyNegOther},
"sqrt": {ApplySqrtInt, ApplySqrtFloat, ApplySqrtOther},
"floor": {ApplyFloorInt, ApplyFloorFloat, ApplyFloorOther},
"ceil": {ApplyCeilInt, ApplyCeilFloat, ApplyCeilOther},
"since": {ApplySince, ApplySince, ApplySince},
}
var supportedBinaryFunctions = map[string][]MathBinaryFunction{
"+": {ApplyIntAdd, ApplyFloatAdd, ApplyOtherAdd},
"-": {ApplyIntSub, ApplyFloatSub, ApplyOtherSub},
"*": {ApplyIntMul, ApplyFloatMul, ApplyOtherMul},
"/": {ApplyIntDiv, ApplyFloatDiv, ApplyOtherDiv},
"%": {ApplyIntMod, ApplyFloatMod, ApplyOtherMod},
"pow": {ApplyIntPow, ApplyFloatPow, ApplyOtherPow},
"logbase": {ApplyIntLog, ApplyFloatLog, ApplyOtherLog},
"min": {ApplyMin, ApplyMin, ApplyMin},
"max": {ApplyMax, ApplyMax, ApplyMax},
}
type valType int
const (
INT valType = iota
FLOAT
DEFAULT
)
func getValType(v *types.Val) valType {
var vBase valType
if v.Tid == types.IntID {
vBase = INT
} else if v.Tid == types.FloatID {
vBase = FLOAT
} else {
vBase = DEFAULT
}
return vBase
}
func (ag *aggregator) MatchType(vBase, vaBase *valType, v, va *types.Val) error {
if *vBase == *vaBase {
return nil
}
if *vBase == DEFAULT || *vaBase == DEFAULT {
return errors.Errorf("Wrong type encontered for func %s", ag.name)
}
// One of them is int and one is float
if *vBase == INT {
*vBase = FLOAT
v.Tid = types.FloatID
v.Value = float64(v.Value.(int64))
}
if *vaBase == INT {
*vaBase = FLOAT
va.Tid = types.FloatID
va.Value = float64(va.Value.(int64))
}
return 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
}
vBase := getValType(&v)
var res types.Val
if isUnary(ag.name) {
res.Tid = v.Tid
err := supportedUnaryFunctions[ag.name][vBase](&v, &res)
if err != nil {
return err
}
ag.result = res
return nil
}
if ag.result.Value == nil {
ag.result = v
return nil
}
va := ag.result
vaBase := getValType(&va)
if err := ag.MatchType(&vBase, &vaBase, &v, &va); err != nil {
return err
}
if function, ok := supportedBinaryFunctions[ag.name]; ok {
res.Tid = va.Tid
err := function[vaBase](&va, &v, &res)
if err != nil {
return err
}
ag.result = res
} else {
return errors.Errorf("Unhandled aggregator function %q", ag.name)
}
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)
}
// Skipping the else case since that means the pair cannot be summed.
res = va
default:
x.Fatalf("Unhandled aggregator function %v", ag.name)
}
ag.count++
ag.result = res
}
func (ag *aggregator) ValueMarshalled() (*pb.TaskValue, error) {
data := types.ValueForType(types.BinaryID)
ag.divideByCount()
res := &pb.TaskValue{ValType: ag.result.Tid.Enum(), 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
}