forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boolean_operators.go
183 lines (159 loc) · 4.49 KB
/
boolean_operators.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
package engine
import (
"fmt"
"regexp"
"github.com/influxdb/influxdb/common"
"github.com/influxdb/influxdb/protocol"
)
type OperatorResult int
const (
MATCH OperatorResult = iota
NO_MATCH
INVALID
)
type oldBooleanOperation func(leftValue, rightValues *protocol.FieldValue) (OperatorResult, error)
type BooleanOperation func(leftValue *protocol.FieldValue, rightValues []*protocol.FieldValue) (OperatorResult, error)
func wrapOldBooleanOperation(operation oldBooleanOperation) BooleanOperation {
return func(leftValue *protocol.FieldValue, rightValues []*protocol.FieldValue) (OperatorResult, error) {
if len(rightValues) != 1 {
return INVALID, fmt.Errorf("Expected one value on the right side")
}
if leftValue == nil || rightValues[0] == nil {
return INVALID, nil
}
return operation(leftValue, rightValues[0])
}
}
var (
registeredOperators = map[string]BooleanOperation{}
)
func init() {
registeredOperators["="] = wrapOldBooleanOperation(EqualityOperator)
registeredOperators["<>"] = not(wrapOldBooleanOperation(EqualityOperator))
registeredOperators[">="] = wrapOldBooleanOperation(GreaterThanOrEqualOperator)
registeredOperators[">"] = wrapOldBooleanOperation(GreaterThanOperator)
registeredOperators["<"] = not(wrapOldBooleanOperation(GreaterThanOrEqualOperator))
registeredOperators["<="] = not(wrapOldBooleanOperation(GreaterThanOperator))
registeredOperators["=~"] = wrapOldBooleanOperation(RegexMatcherOperator)
registeredOperators["!~"] = not(wrapOldBooleanOperation(RegexMatcherOperator))
registeredOperators["in"] = InOperator
}
func not(op BooleanOperation) BooleanOperation {
return func(leftValue *protocol.FieldValue, rightValue []*protocol.FieldValue) (OperatorResult, error) {
ok, err := op(leftValue, rightValue)
switch ok {
case MATCH:
return NO_MATCH, err
case NO_MATCH:
return MATCH, err
default:
return INVALID, err
}
}
}
func EqualityOperator(leftValue, rightValue *protocol.FieldValue) (OperatorResult, error) {
v1, v2, cType := common.CoerceValues(leftValue, rightValue)
switch cType {
case common.TYPE_STRING:
if v1.(string) == v2.(string) {
return MATCH, nil
}
return NO_MATCH, nil
case common.TYPE_INT:
if v1.(int64) == v2.(int64) {
return MATCH, nil
}
return NO_MATCH, nil
case common.TYPE_DOUBLE:
if v1.(float64) == v2.(float64) {
return MATCH, nil
}
return NO_MATCH, nil
case common.TYPE_BOOL:
if v1.(bool) == v2.(bool) {
return MATCH, nil
}
return NO_MATCH, nil
default:
return INVALID, nil
}
}
func RegexMatcherOperator(leftValue, rightValue *protocol.FieldValue) (OperatorResult, error) {
v1, v2, cType := common.CoerceValues(leftValue, rightValue)
switch cType {
case common.TYPE_STRING:
// TODO: assume that the regex is valid
if ok, _ := regexp.MatchString(v2.(string), v1.(string)); ok {
return MATCH, nil
}
return NO_MATCH, nil
default:
return INVALID, nil
}
}
func GreaterThanOrEqualOperator(leftValue, rightValue *protocol.FieldValue) (OperatorResult, error) {
v1, v2, cType := common.CoerceValues(leftValue, rightValue)
switch cType {
case common.TYPE_STRING:
if v1.(string) >= v2.(string) {
return MATCH, nil
}
return NO_MATCH, nil
case common.TYPE_INT:
if v1.(int64) >= v2.(int64) {
return MATCH, nil
}
return NO_MATCH, nil
case common.TYPE_DOUBLE:
if v1.(float64) >= v2.(float64) {
return MATCH, nil
}
return NO_MATCH, nil
default:
return INVALID, nil
}
}
func GreaterThanOperator(leftValue, rightValue *protocol.FieldValue) (OperatorResult, error) {
v1, v2, cType := common.CoerceValues(leftValue, rightValue)
switch cType {
case common.TYPE_STRING:
if v1.(string) > v2.(string) {
return MATCH, nil
}
return NO_MATCH, nil
case common.TYPE_INT:
if v1.(int64) > v2.(int64) {
return MATCH, nil
}
return NO_MATCH, nil
case common.TYPE_DOUBLE:
if v1.(float64) > v2.(float64) {
return MATCH, nil
}
return NO_MATCH, nil
default:
return INVALID, nil
}
}
func InOperator(leftValue *protocol.FieldValue, rightValue []*protocol.FieldValue) (OperatorResult, error) {
for _, v := range rightValue {
v1, v2, cType := common.CoerceValues(leftValue, v)
var result bool
switch cType {
case common.TYPE_STRING:
result = v1.(string) == v2.(string)
case common.TYPE_INT:
result = v1.(int64) == v2.(int64)
case common.TYPE_DOUBLE:
result = v1.(float64) == v2.(float64)
case common.TYPE_BOOL:
result = v1.(bool) == v2.(bool)
default:
return INVALID, nil
}
if result {
return MATCH, nil
}
}
return NO_MATCH, nil
}