1 | /* | |
2 | * Copyright OpenSearch Contributors | |
3 | * SPDX-License-Identifier: Apache-2.0 | |
4 | */ | |
5 | ||
6 | ||
7 | package org.opensearch.sql.expression.operator.predicate; | |
8 | ||
9 | import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_FALSE; | |
10 | import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_MISSING; | |
11 | import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_NULL; | |
12 | import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_TRUE; | |
13 | import static org.opensearch.sql.data.type.ExprCoreType.BOOLEAN; | |
14 | import static org.opensearch.sql.data.type.ExprCoreType.INTEGER; | |
15 | import static org.opensearch.sql.data.type.ExprCoreType.STRING; | |
16 | ||
17 | import com.google.common.collect.ImmutableTable; | |
18 | import com.google.common.collect.Table; | |
19 | import java.util.stream.Collectors; | |
20 | import lombok.experimental.UtilityClass; | |
21 | import org.opensearch.sql.data.model.ExprBooleanValue; | |
22 | import org.opensearch.sql.data.model.ExprValue; | |
23 | import org.opensearch.sql.data.type.ExprCoreType; | |
24 | import org.opensearch.sql.expression.function.BuiltinFunctionName; | |
25 | import org.opensearch.sql.expression.function.BuiltinFunctionRepository; | |
26 | import org.opensearch.sql.expression.function.DefaultFunctionResolver; | |
27 | import org.opensearch.sql.expression.function.FunctionDSL; | |
28 | import org.opensearch.sql.utils.OperatorUtils; | |
29 | ||
30 | /** | |
31 | * The definition of binary predicate function | |
32 | * and, Accepts two Boolean values and produces a Boolean. | |
33 | * or, Accepts two Boolean values and produces a Boolean. | |
34 | * xor, Accepts two Boolean values and produces a Boolean. | |
35 | * equalTo, Compare the left expression and right expression and produces a Boolean. | |
36 | */ | |
37 | @UtilityClass | |
38 | public class BinaryPredicateOperator { | |
39 | /** | |
40 | * Register Binary Predicate Function. | |
41 | * | |
42 | * @param repository {@link BuiltinFunctionRepository}. | |
43 | */ | |
44 | public static void register(BuiltinFunctionRepository repository) { | |
45 |
1
1. register : removed call to org/opensearch/sql/expression/function/BuiltinFunctionRepository::register → SURVIVED |
repository.register(and()); |
46 |
1
1. register : removed call to org/opensearch/sql/expression/function/BuiltinFunctionRepository::register → SURVIVED |
repository.register(or()); |
47 |
1
1. register : removed call to org/opensearch/sql/expression/function/BuiltinFunctionRepository::register → SURVIVED |
repository.register(xor()); |
48 |
1
1. register : removed call to org/opensearch/sql/expression/function/BuiltinFunctionRepository::register → KILLED |
repository.register(equal()); |
49 |
1
1. register : removed call to org/opensearch/sql/expression/function/BuiltinFunctionRepository::register → SURVIVED |
repository.register(notEqual()); |
50 |
1
1. register : removed call to org/opensearch/sql/expression/function/BuiltinFunctionRepository::register → SURVIVED |
repository.register(less()); |
51 |
1
1. register : removed call to org/opensearch/sql/expression/function/BuiltinFunctionRepository::register → SURVIVED |
repository.register(lte()); |
52 |
1
1. register : removed call to org/opensearch/sql/expression/function/BuiltinFunctionRepository::register → KILLED |
repository.register(greater()); |
53 |
1
1. register : removed call to org/opensearch/sql/expression/function/BuiltinFunctionRepository::register → SURVIVED |
repository.register(gte()); |
54 |
1
1. register : removed call to org/opensearch/sql/expression/function/BuiltinFunctionRepository::register → SURVIVED |
repository.register(like()); |
55 |
1
1. register : removed call to org/opensearch/sql/expression/function/BuiltinFunctionRepository::register → SURVIVED |
repository.register(notLike()); |
56 |
1
1. register : removed call to org/opensearch/sql/expression/function/BuiltinFunctionRepository::register → SURVIVED |
repository.register(regexp()); |
57 | } | |
58 | ||
59 | /** | |
60 | * The and logic. | |
61 | * A B A AND B | |
62 | * TRUE TRUE TRUE | |
63 | * TRUE FALSE FALSE | |
64 | * TRUE NULL NULL | |
65 | * TRUE MISSING MISSING | |
66 | * FALSE FALSE FALSE | |
67 | * FALSE NULL FALSE | |
68 | * FALSE MISSING FALSE | |
69 | * NULL NULL NULL | |
70 | * NULL MISSING MISSING | |
71 | * MISSING MISSING MISSING | |
72 | */ | |
73 | private static Table<ExprValue, ExprValue, ExprValue> andTable = | |
74 | new ImmutableTable.Builder<ExprValue, ExprValue, ExprValue>() | |
75 | .put(LITERAL_TRUE, LITERAL_TRUE, LITERAL_TRUE) | |
76 | .put(LITERAL_TRUE, LITERAL_FALSE, LITERAL_FALSE) | |
77 | .put(LITERAL_TRUE, LITERAL_NULL, LITERAL_NULL) | |
78 | .put(LITERAL_TRUE, LITERAL_MISSING, LITERAL_MISSING) | |
79 | .put(LITERAL_FALSE, LITERAL_FALSE, LITERAL_FALSE) | |
80 | .put(LITERAL_FALSE, LITERAL_NULL, LITERAL_FALSE) | |
81 | .put(LITERAL_FALSE, LITERAL_MISSING, LITERAL_FALSE) | |
82 | .put(LITERAL_NULL, LITERAL_NULL, LITERAL_NULL) | |
83 | .put(LITERAL_NULL, LITERAL_MISSING, LITERAL_MISSING) | |
84 | .put(LITERAL_MISSING, LITERAL_MISSING, LITERAL_MISSING) | |
85 | .build(); | |
86 | ||
87 | /** | |
88 | * The or logic. | |
89 | * A B A AND B | |
90 | * TRUE TRUE TRUE | |
91 | * TRUE FALSE TRUE | |
92 | * TRUE NULL TRUE | |
93 | * TRUE MISSING TRUE | |
94 | * FALSE FALSE FALSE | |
95 | * FALSE NULL NULL | |
96 | * FALSE MISSING MISSING | |
97 | * NULL NULL NULL | |
98 | * NULL MISSING NULL | |
99 | * MISSING MISSING MISSING | |
100 | */ | |
101 | private static Table<ExprValue, ExprValue, ExprValue> orTable = | |
102 | new ImmutableTable.Builder<ExprValue, ExprValue, ExprValue>() | |
103 | .put(LITERAL_TRUE, LITERAL_TRUE, LITERAL_TRUE) | |
104 | .put(LITERAL_TRUE, LITERAL_FALSE, LITERAL_TRUE) | |
105 | .put(LITERAL_TRUE, LITERAL_NULL, LITERAL_TRUE) | |
106 | .put(LITERAL_TRUE, LITERAL_MISSING, LITERAL_TRUE) | |
107 | .put(LITERAL_FALSE, LITERAL_FALSE, LITERAL_FALSE) | |
108 | .put(LITERAL_FALSE, LITERAL_NULL, LITERAL_NULL) | |
109 | .put(LITERAL_FALSE, LITERAL_MISSING, LITERAL_MISSING) | |
110 | .put(LITERAL_NULL, LITERAL_NULL, LITERAL_NULL) | |
111 | .put(LITERAL_NULL, LITERAL_MISSING, LITERAL_NULL) | |
112 | .put(LITERAL_MISSING, LITERAL_MISSING, LITERAL_MISSING) | |
113 | .build(); | |
114 | ||
115 | /** | |
116 | * The xor logic. | |
117 | * A B A AND B | |
118 | * TRUE TRUE FALSE | |
119 | * TRUE FALSE TRUE | |
120 | * TRUE NULL TRUE | |
121 | * TRUE MISSING TRUE | |
122 | * FALSE FALSE FALSE | |
123 | * FALSE NULL NULL | |
124 | * FALSE MISSING MISSING | |
125 | * NULL NULL NULL | |
126 | * NULL MISSING NULL | |
127 | * MISSING MISSING MISSING | |
128 | */ | |
129 | private static Table<ExprValue, ExprValue, ExprValue> xorTable = | |
130 | new ImmutableTable.Builder<ExprValue, ExprValue, ExprValue>() | |
131 | .put(LITERAL_TRUE, LITERAL_TRUE, LITERAL_FALSE) | |
132 | .put(LITERAL_TRUE, LITERAL_FALSE, LITERAL_TRUE) | |
133 | .put(LITERAL_TRUE, LITERAL_NULL, LITERAL_TRUE) | |
134 | .put(LITERAL_TRUE, LITERAL_MISSING, LITERAL_TRUE) | |
135 | .put(LITERAL_FALSE, LITERAL_FALSE, LITERAL_FALSE) | |
136 | .put(LITERAL_FALSE, LITERAL_NULL, LITERAL_NULL) | |
137 | .put(LITERAL_FALSE, LITERAL_MISSING, LITERAL_MISSING) | |
138 | .put(LITERAL_NULL, LITERAL_NULL, LITERAL_NULL) | |
139 | .put(LITERAL_NULL, LITERAL_MISSING, LITERAL_NULL) | |
140 | .put(LITERAL_MISSING, LITERAL_MISSING, LITERAL_MISSING) | |
141 | .build(); | |
142 | ||
143 | private static DefaultFunctionResolver and() { | |
144 |
1
1. and : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::and → KILLED |
return FunctionDSL.define(BuiltinFunctionName.AND.getName(), FunctionDSL |
145 |
1
1. lambda$and$95048fc1$1 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$and$95048fc1$1 → KILLED |
.impl((v1, v2) -> lookupTableFunction(v1, v2, andTable), BOOLEAN, BOOLEAN, |
146 | BOOLEAN)); | |
147 | } | |
148 | ||
149 | private static DefaultFunctionResolver or() { | |
150 |
1
1. or : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::or → KILLED |
return FunctionDSL.define(BuiltinFunctionName.OR.getName(), FunctionDSL |
151 |
1
1. lambda$or$95048fc1$1 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$or$95048fc1$1 → KILLED |
.impl((v1, v2) -> lookupTableFunction(v1, v2, orTable), BOOLEAN, BOOLEAN, |
152 | BOOLEAN)); | |
153 | } | |
154 | ||
155 | private static DefaultFunctionResolver xor() { | |
156 |
1
1. xor : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::xor → KILLED |
return FunctionDSL.define(BuiltinFunctionName.XOR.getName(), FunctionDSL |
157 |
1
1. lambda$xor$95048fc1$1 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$xor$95048fc1$1 → KILLED |
.impl((v1, v2) -> lookupTableFunction(v1, v2, xorTable), BOOLEAN, BOOLEAN, |
158 | BOOLEAN)); | |
159 | } | |
160 | ||
161 | private static DefaultFunctionResolver equal() { | |
162 |
1
1. equal : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::equal → KILLED |
return FunctionDSL.define(BuiltinFunctionName.EQUAL.getName(), |
163 | ExprCoreType.coreTypes().stream() | |
164 |
1
1. lambda$equal$0 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$equal$0 → KILLED |
.map(type -> FunctionDSL.impl( |
165 |
1
1. lambda$equal$95048fc1$1 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$equal$95048fc1$1 → KILLED |
FunctionDSL.nullMissingHandling((v1, v2) -> ExprBooleanValue.of(v1.equals(v2))), |
166 | BOOLEAN, type, type)) | |
167 | .collect( | |
168 | Collectors.toList())); | |
169 | } | |
170 | ||
171 | private static DefaultFunctionResolver notEqual() { | |
172 |
1
1. notEqual : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::notEqual → KILLED |
return FunctionDSL |
173 | .define(BuiltinFunctionName.NOTEQUAL.getName(), ExprCoreType.coreTypes().stream() | |
174 |
1
1. lambda$notEqual$1 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$notEqual$1 → KILLED |
.map(type -> FunctionDSL |
175 | .impl( | |
176 | FunctionDSL | |
177 |
2
1. lambda$notEqual$95048fc1$1 : negated conditional → KILLED 2. lambda$notEqual$95048fc1$1 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$notEqual$95048fc1$1 → KILLED |
.nullMissingHandling((v1, v2) -> ExprBooleanValue.of(!v1.equals(v2))), |
178 | BOOLEAN, | |
179 | type, | |
180 | type)) | |
181 | .collect( | |
182 | Collectors.toList())); | |
183 | } | |
184 | ||
185 | private static DefaultFunctionResolver less() { | |
186 |
1
1. less : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::less → KILLED |
return FunctionDSL |
187 | .define(BuiltinFunctionName.LESS.getName(), ExprCoreType.coreTypes().stream() | |
188 |
1
1. lambda$less$2 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$less$2 → KILLED |
.map(type -> FunctionDSL |
189 | .impl(FunctionDSL | |
190 |
3
1. lambda$less$95048fc1$1 : changed conditional boundary → KILLED 2. lambda$less$95048fc1$1 : negated conditional → KILLED 3. lambda$less$95048fc1$1 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$less$95048fc1$1 → KILLED |
.nullMissingHandling((v1, v2) -> ExprBooleanValue.of(v1.compareTo(v2) < 0)), |
191 | BOOLEAN, | |
192 | type, type)) | |
193 | .collect( | |
194 | Collectors.toList())); | |
195 | } | |
196 | ||
197 | private static DefaultFunctionResolver lte() { | |
198 |
1
1. lte : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lte → KILLED |
return FunctionDSL |
199 | .define(BuiltinFunctionName.LTE.getName(), ExprCoreType.coreTypes().stream() | |
200 |
1
1. lambda$lte$3 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$lte$3 → KILLED |
.map(type -> FunctionDSL |
201 | .impl( | |
202 | FunctionDSL | |
203 | .nullMissingHandling( | |
204 |
3
1. lambda$lte$95048fc1$1 : changed conditional boundary → KILLED 2. lambda$lte$95048fc1$1 : negated conditional → KILLED 3. lambda$lte$95048fc1$1 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$lte$95048fc1$1 → KILLED |
(v1, v2) -> ExprBooleanValue.of(v1.compareTo(v2) <= 0)), |
205 | BOOLEAN, | |
206 | type, type)) | |
207 | .collect( | |
208 | Collectors.toList())); | |
209 | } | |
210 | ||
211 | private static DefaultFunctionResolver greater() { | |
212 |
1
1. greater : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::greater → KILLED |
return FunctionDSL |
213 | .define(BuiltinFunctionName.GREATER.getName(), ExprCoreType.coreTypes().stream() | |
214 |
1
1. lambda$greater$4 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$greater$4 → KILLED |
.map(type -> FunctionDSL |
215 | .impl(FunctionDSL | |
216 |
3
1. lambda$greater$95048fc1$1 : changed conditional boundary → KILLED 2. lambda$greater$95048fc1$1 : negated conditional → KILLED 3. lambda$greater$95048fc1$1 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$greater$95048fc1$1 → KILLED |
.nullMissingHandling((v1, v2) -> ExprBooleanValue.of(v1.compareTo(v2) > 0)), |
217 | BOOLEAN, type, type)) | |
218 | .collect( | |
219 | Collectors.toList())); | |
220 | } | |
221 | ||
222 | private static DefaultFunctionResolver gte() { | |
223 |
1
1. gte : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::gte → KILLED |
return FunctionDSL |
224 | .define(BuiltinFunctionName.GTE.getName(), ExprCoreType.coreTypes().stream() | |
225 |
1
1. lambda$gte$5 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$gte$5 → KILLED |
.map(type -> FunctionDSL |
226 | .impl( | |
227 | FunctionDSL.nullMissingHandling( | |
228 |
3
1. lambda$gte$95048fc1$1 : changed conditional boundary → KILLED 2. lambda$gte$95048fc1$1 : negated conditional → KILLED 3. lambda$gte$95048fc1$1 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$gte$95048fc1$1 → KILLED |
(v1, v2) -> ExprBooleanValue.of(v1.compareTo(v2) >= 0)), |
229 | BOOLEAN, | |
230 | type, type)) | |
231 | .collect( | |
232 | Collectors.toList())); | |
233 | } | |
234 | ||
235 | private static DefaultFunctionResolver like() { | |
236 |
1
1. like : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::like → KILLED |
return FunctionDSL.define(BuiltinFunctionName.LIKE.getName(), FunctionDSL |
237 | .impl(FunctionDSL.nullMissingHandling(OperatorUtils::matches), BOOLEAN, STRING, | |
238 | STRING)); | |
239 | } | |
240 | ||
241 | private static DefaultFunctionResolver regexp() { | |
242 |
1
1. regexp : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::regexp → KILLED |
return FunctionDSL.define(BuiltinFunctionName.REGEXP.getName(), FunctionDSL |
243 | .impl(FunctionDSL.nullMissingHandling(OperatorUtils::matchesRegexp), | |
244 | INTEGER, STRING, STRING)); | |
245 | } | |
246 | ||
247 | private static DefaultFunctionResolver notLike() { | |
248 |
1
1. notLike : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::notLike → KILLED |
return FunctionDSL.define(BuiltinFunctionName.NOT_LIKE.getName(), FunctionDSL |
249 | .impl(FunctionDSL.nullMissingHandling( | |
250 |
1
1. lambda$notLike$95048fc1$1 : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lambda$notLike$95048fc1$1 → KILLED |
(v1, v2) -> UnaryPredicateOperator.not(OperatorUtils.matches(v1, v2))), |
251 | BOOLEAN, | |
252 | STRING, | |
253 | STRING)); | |
254 | } | |
255 | ||
256 | private static ExprValue lookupTableFunction(ExprValue arg1, ExprValue arg2, | |
257 | Table<ExprValue, ExprValue, ExprValue> table) { | |
258 |
1
1. lookupTableFunction : negated conditional → KILLED |
if (table.contains(arg1, arg2)) { |
259 |
1
1. lookupTableFunction : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lookupTableFunction → KILLED |
return table.get(arg1, arg2); |
260 | } else { | |
261 |
1
1. lookupTableFunction : replaced return value with null for org/opensearch/sql/expression/operator/predicate/BinaryPredicateOperator::lookupTableFunction → KILLED |
return table.get(arg2, arg1); |
262 | } | |
263 | } | |
264 | } | |
Mutations | ||
45 |
1.1 |
|
46 |
1.1 |
|
47 |
1.1 |
|
48 |
1.1 |
|
49 |
1.1 |
|
50 |
1.1 |
|
51 |
1.1 |
|
52 |
1.1 |
|
53 |
1.1 |
|
54 |
1.1 |
|
55 |
1.1 |
|
56 |
1.1 |
|
144 |
1.1 |
|
145 |
1.1 |
|
150 |
1.1 |
|
151 |
1.1 |
|
156 |
1.1 |
|
157 |
1.1 |
|
162 |
1.1 |
|
164 |
1.1 |
|
165 |
1.1 |
|
172 |
1.1 |
|
174 |
1.1 |
|
177 |
1.1 2.2 |
|
186 |
1.1 |
|
188 |
1.1 |
|
190 |
1.1 2.2 3.3 |
|
198 |
1.1 |
|
200 |
1.1 |
|
204 |
1.1 2.2 3.3 |
|
212 |
1.1 |
|
214 |
1.1 |
|
216 |
1.1 2.2 3.3 |
|
223 |
1.1 |
|
225 |
1.1 |
|
228 |
1.1 2.2 3.3 |
|
236 |
1.1 |
|
242 |
1.1 |
|
248 |
1.1 |
|
250 |
1.1 |
|
258 |
1.1 |
|
259 |
1.1 |
|
261 |
1.1 |