forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arithmetic_engine.go
84 lines (70 loc) · 1.74 KB
/
arithmetic_engine.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
package engine
import (
"strconv"
log "code.google.com/p/log4go"
"github.com/influxdb/influxdb/parser"
"github.com/influxdb/influxdb/protocol"
)
type ArithmeticEngine struct {
next Processor
names map[string]*parser.Value
}
func NewArithmeticEngine(query *parser.SelectQuery, next Processor) (*ArithmeticEngine, error) {
names := map[string]*parser.Value{}
for idx, v := range query.GetColumnNames() {
switch v.Type {
case parser.ValueSimpleName:
names[v.Name] = v
case parser.ValueFunctionCall:
names[v.Name] = v
case parser.ValueExpression:
if v.Alias != "" {
names[v.Alias] = v
} else {
names["expr"+strconv.Itoa(idx)] = v
}
}
}
return &ArithmeticEngine{
next: next,
names: names,
}, nil
}
func (ae *ArithmeticEngine) Yield(s *protocol.Series) (bool, error) {
if len(s.Points) == 0 {
return ae.next.Yield(s)
}
newSeries := &protocol.Series{
Name: s.Name,
}
// create the new column names
for name := range ae.names {
newSeries.Fields = append(newSeries.Fields, name)
}
for _, point := range s.Points {
newPoint := &protocol.Point{
Timestamp: point.Timestamp,
SequenceNumber: point.SequenceNumber,
}
for _, field := range newSeries.Fields {
value := ae.names[field]
v, err := GetValue(value, s.Fields, point)
if err != nil {
log.Error("Error in arithmetic computation: %s", err)
return false, err
}
newPoint.Values = append(newPoint.Values, v)
}
newSeries.Points = append(newSeries.Points, newPoint)
}
return ae.next.Yield(newSeries)
}
func (self *ArithmeticEngine) Close() error {
return self.next.Close()
}
func (self *ArithmeticEngine) Name() string {
return "Arithmetic Engine"
}
func (self *ArithmeticEngine) Next() Processor {
return self.next
}