forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filtering_helpers.go
194 lines (168 loc) · 4.91 KB
/
filtering_helpers.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
package engine
import (
"fmt"
"strconv"
"github.com/influxdb/influxdb/parser"
"github.com/influxdb/influxdb/protocol"
)
func getExpressionValue(values []*parser.Value, fields []string, point *protocol.Point) ([]*protocol.FieldValue, error) {
fieldValues := []*protocol.FieldValue{}
for _, value := range values {
switch value.Type {
case parser.ValueFunctionCall:
return nil, fmt.Errorf("Cannot process function call %s in expression", value.Name)
case parser.ValueInt:
value, err := strconv.ParseInt(value.Name, 10, 64)
if err == nil {
fieldValues = append(fieldValues, &protocol.FieldValue{Int64Value: &value})
continue
}
// an error will happen only if the integer is out of range
fallthrough
case parser.ValueFloat:
value, _ := strconv.ParseFloat(value.Name, 64)
fieldValues = append(fieldValues, &protocol.FieldValue{DoubleValue: &value})
case parser.ValueBool:
value, _ := strconv.ParseBool(value.Name)
fieldValues = append(fieldValues, &protocol.FieldValue{BoolValue: &value})
case parser.ValueString:
fieldValues = append(fieldValues, &protocol.FieldValue{StringValue: &value.Name})
case parser.ValueRegex:
regex, _ := value.GetCompiledRegex()
regexStr := regex.String()
fieldValues = append(fieldValues, &protocol.FieldValue{StringValue: ®exStr})
case parser.ValueTableName, 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)
}
fieldValues = append(fieldValues, point.Values[fieldIdx])
case parser.ValueExpression:
v, err := GetValue(value, fields, point)
if err != nil {
return nil, err
}
fieldValues = append(fieldValues, v)
default:
return nil, fmt.Errorf("Cannot evaluate expression")
}
}
return fieldValues, nil
}
func matchesExpression(expr *parser.Value, fields []string, point *protocol.Point) (bool, error) {
leftValue, err := getExpressionValue(expr.Elems[:1], fields, point)
if err != nil {
return false, err
}
rightValue, err := getExpressionValue(expr.Elems[1:], fields, point)
if err != nil {
return false, err
}
operator := registeredOperators[expr.Name]
if operator == nil {
return false, fmt.Errorf("Invalid boolean operator %s", expr.Name)
}
ok, err := operator(leftValue[0], rightValue)
return ok == MATCH, err
}
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]struct{}, fields []string, point *protocol.Point) {
if _, ok := columns["*"]; ok {
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.SelectQuery, series *protocol.Series) (*protocol.Series, error) {
if query.GetWhereCondition() == nil {
return series, nil
}
columns := map[string]struct{}{}
if query.GetFromClause().Type == parser.FromClauseInnerJoin {
outer:
for t, cs := range query.GetResultColumns() {
for _, c := range cs {
// if this is a wildcard select, then drop all columns and
// just use '*'
if c == "*" {
columns = make(map[string]struct{}, 1)
columns[c] = struct{}{}
break outer
}
columns[t.Name+"."+c] = struct{}{}
}
}
} else {
for _, cs := range query.GetResultColumns() {
for _, c := range cs {
columns[c] = struct{}{}
}
}
}
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 _, ok := columns["*"]; !ok {
newFields := []string{}
for _, f := range series.Fields {
if _, ok := columns[f]; !ok {
continue
}
newFields = append(newFields, f)
}
series.Fields = newFields
}
return series, nil
}