forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filtering.go
145 lines (123 loc) · 3.49 KB
/
filtering.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
package datastore
import (
"fmt"
"parser"
"protocol"
"strconv"
)
func getExpressionValue(expr *parser.Expression, fields []string, point *protocol.Point) (*protocol.FieldValue, error) {
if value, ok := expr.GetLeftValue(); ok {
switch value.Type {
case parser.ValueFunctionCall:
return nil, fmt.Errorf("Cannot process function call %s in expression", value.Name)
case parser.ValueFloat:
value, _ := strconv.ParseFloat(value.Name, 64)
return &protocol.FieldValue{DoubleValue: &value}, nil
case parser.ValueInt:
value, _ := strconv.ParseInt(value.Name, 10, 64)
return &protocol.FieldValue{Int64Value: &value}, nil
case parser.ValueString, parser.ValueRegex:
return &protocol.FieldValue{StringValue: &value.Name}, nil
case parser.ValueSimpleName:
// TODO: optimize this so we don't have to lookup the column everytime
fieldIdx := -1
for idx, field := range fields {
if field == value.Name {
fieldIdx = idx
break
}
}
if fieldIdx == -1 {
return nil, fmt.Errorf("Cannot find column %s", value.Name)
}
return point.Values[fieldIdx], nil
}
}
return nil, fmt.Errorf("Cannot evaulate expression")
}
func matchesExpression(expr *parser.BoolExpression, fields []string, point *protocol.Point) (bool, error) {
leftValue, err := getExpressionValue(expr.Left, fields, point)
if err != nil {
return false, err
}
rightValue, err := getExpressionValue(expr.Right, fields, point)
if err != nil {
return false, err
}
operator := registeredOperators[expr.Operation]
return operator(leftValue, rightValue)
}
func matches(condition *parser.WhereCondition, fields []string, point *protocol.Point) (bool, error) {
if expr, ok := condition.GetBoolExpression(); ok {
return matchesExpression(expr, fields, point)
}
left, _ := condition.GetLeftWhereCondition()
leftResult, err := matches(left, fields, point)
if err != nil {
return false, err
}
// short circuit
if !leftResult && condition.Operation == "AND" ||
leftResult && condition.Operation == "OR" {
return leftResult, nil
}
return matches(condition.Right, fields, point)
}
func getColumns(values []*parser.Value, columns map[string]bool) {
for _, v := range values {
switch v.Type {
case parser.ValueSimpleName:
columns[v.Name] = true
case parser.ValueWildcard:
columns["*"] = true
return
case parser.ValueFunctionCall:
getColumns(v.Elems, columns)
}
}
}
func filterColumns(columns map[string]bool, fields []string, point *protocol.Point) {
if columns["*"] {
return
}
newValues := []*protocol.FieldValue{}
newFields := []string{}
for idx, f := range fields {
if _, ok := columns[f]; !ok {
continue
}
newValues = append(newValues, point.Values[idx])
newFields = append(newFields, f)
}
point.Values = newValues
}
func Filter(query *parser.Query, series *protocol.Series) (*protocol.Series, error) {
if query.GetWhereCondition() == nil {
return series, nil
}
columns := map[string]bool{}
getColumns(query.GetColumnNames(), columns)
points := series.Points
series.Points = nil
for _, point := range points {
ok, err := matches(query.GetWhereCondition(), series.Fields, point)
if err != nil {
return nil, err
}
if ok {
filterColumns(columns, series.Fields, point)
series.Points = append(series.Points, point)
}
}
if !columns["*"] {
newFields := []string{}
for _, f := range series.Fields {
if _, ok := columns[f]; !ok {
continue
}
newFields = append(newFields, f)
}
series.Fields = newFields
}
return series, nil
}