1 | /* | |
2 | * Copyright OpenSearch Contributors | |
3 | * SPDX-License-Identifier: Apache-2.0 | |
4 | */ | |
5 | ||
6 | ||
7 | package org.opensearch.sql.expression.function; | |
8 | ||
9 | import java.util.Arrays; | |
10 | import java.util.Collections; | |
11 | import java.util.List; | |
12 | import java.util.function.Function; | |
13 | import java.util.stream.Collectors; | |
14 | import lombok.experimental.UtilityClass; | |
15 | import org.apache.commons.lang3.tuple.Pair; | |
16 | import org.opensearch.sql.data.model.ExprValue; | |
17 | import org.opensearch.sql.data.model.ExprValueUtils; | |
18 | import org.opensearch.sql.data.type.ExprType; | |
19 | import org.opensearch.sql.expression.Expression; | |
20 | import org.opensearch.sql.expression.FunctionExpression; | |
21 | import org.opensearch.sql.expression.env.Environment; | |
22 | ||
23 | /** | |
24 | * Function Define Utility. | |
25 | */ | |
26 | @UtilityClass | |
27 | public class FunctionDSL { | |
28 | /** | |
29 | * Define overloaded function with implementation. | |
30 | * | |
31 | * @param functionName function name. | |
32 | * @param functions a list of function implementation. | |
33 | * @return FunctionResolver. | |
34 | */ | |
35 | public static DefaultFunctionResolver define(FunctionName functionName, | |
36 | SerializableFunction<FunctionName, Pair<FunctionSignature, | |
37 | FunctionBuilder>>... functions) { | |
38 |
1
1. define : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::define → KILLED |
return define(functionName, Arrays.asList(functions)); |
39 | } | |
40 | ||
41 | /** | |
42 | * Define overloaded function with implementation. | |
43 | * | |
44 | * @param functionName function name. | |
45 | * @param functions a list of function implementation. | |
46 | * @return FunctionResolver. | |
47 | */ | |
48 | public static DefaultFunctionResolver define(FunctionName functionName, List< | |
49 | SerializableFunction<FunctionName, Pair<FunctionSignature, FunctionBuilder>>> functions) { | |
50 | ||
51 | DefaultFunctionResolver.DefaultFunctionResolverBuilder builder | |
52 | = DefaultFunctionResolver.builder(); | |
53 | builder.functionName(functionName); | |
54 | for (Function<FunctionName, Pair<FunctionSignature, FunctionBuilder>> func : functions) { | |
55 | Pair<FunctionSignature, FunctionBuilder> functionBuilder = func.apply(functionName); | |
56 | builder.functionBundle(functionBuilder.getKey(), functionBuilder.getValue()); | |
57 | } | |
58 |
1
1. define : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::define → KILLED |
return builder.build(); |
59 | } | |
60 | ||
61 | /** | |
62 | * No Arg Function Implementation. | |
63 | * | |
64 | * @param function {@link ExprValue} based unary function. | |
65 | * @param returnType return type. | |
66 | * @return Unary Function Implementation. | |
67 | */ | |
68 | public static SerializableFunction<FunctionName, Pair<FunctionSignature, FunctionBuilder>> impl( | |
69 | SerializableNoArgFunction<ExprValue> function, | |
70 | ExprType returnType) { | |
71 | ||
72 |
1
1. impl : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::impl → KILLED |
return functionName -> { |
73 | FunctionSignature functionSignature = | |
74 | new FunctionSignature(functionName, Collections.emptyList()); | |
75 | FunctionBuilder functionBuilder = | |
76 |
1
1. lambda$impl$0 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$impl$0 → KILLED |
arguments -> new FunctionExpression(functionName, Collections.emptyList()) { |
77 | @Override | |
78 | public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) { | |
79 |
1
1. valueOf : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL$1::valueOf → KILLED |
return function.get(); |
80 | } | |
81 | ||
82 | @Override | |
83 | public ExprType type() { | |
84 |
1
1. type : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL$1::type → KILLED |
return returnType; |
85 | } | |
86 | ||
87 | @Override | |
88 | public String toString() { | |
89 |
1
1. toString : replaced return value with "" for org/opensearch/sql/expression/function/FunctionDSL$1::toString → KILLED |
return String.format("%s()", functionName); |
90 | } | |
91 | }; | |
92 |
1
1. lambda$impl$76573b3$1 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$impl$76573b3$1 → KILLED |
return Pair.of(functionSignature, functionBuilder); |
93 | }; | |
94 | } | |
95 | ||
96 | /** | |
97 | * Unary Function Implementation. | |
98 | * | |
99 | * @param function {@link ExprValue} based unary function. | |
100 | * @param returnType return type. | |
101 | * @param argsType argument type. | |
102 | * @return Unary Function Implementation. | |
103 | */ | |
104 | public static SerializableFunction<FunctionName, Pair<FunctionSignature, FunctionBuilder>> impl( | |
105 | SerializableFunction<ExprValue, ExprValue> function, | |
106 | ExprType returnType, | |
107 | ExprType argsType) { | |
108 | ||
109 |
1
1. impl : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::impl → KILLED |
return functionName -> { |
110 | FunctionSignature functionSignature = | |
111 | new FunctionSignature(functionName, Collections.singletonList(argsType)); | |
112 | FunctionBuilder functionBuilder = | |
113 |
1
1. lambda$impl$1 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$impl$1 → KILLED |
arguments -> new FunctionExpression(functionName, arguments) { |
114 | @Override | |
115 | public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) { | |
116 | ExprValue value = arguments.get(0).valueOf(valueEnv); | |
117 |
1
1. valueOf : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL$2::valueOf → KILLED |
return function.apply(value); |
118 | } | |
119 | ||
120 | @Override | |
121 | public ExprType type() { | |
122 |
1
1. type : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL$2::type → KILLED |
return returnType; |
123 | } | |
124 | ||
125 | @Override | |
126 | public String toString() { | |
127 |
1
1. toString : replaced return value with "" for org/opensearch/sql/expression/function/FunctionDSL$2::toString → KILLED |
return String.format("%s(%s)", functionName, |
128 | arguments.stream() | |
129 | .map(Object::toString) | |
130 | .collect(Collectors.joining(", "))); | |
131 | } | |
132 | }; | |
133 |
1
1. lambda$impl$156a0b98$1 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$impl$156a0b98$1 → KILLED |
return Pair.of(functionSignature, functionBuilder); |
134 | }; | |
135 | } | |
136 | ||
137 | /** | |
138 | * Binary Function Implementation. | |
139 | * | |
140 | * @param function {@link ExprValue} based unary function. | |
141 | * @param returnType return type. | |
142 | * @param args1Type argument type. | |
143 | * @param args2Type argument type. | |
144 | * @return Binary Function Implementation. | |
145 | */ | |
146 | public static SerializableFunction<FunctionName, Pair<FunctionSignature, FunctionBuilder>> impl( | |
147 | SerializableBiFunction<ExprValue, ExprValue, ExprValue> function, | |
148 | ExprType returnType, | |
149 | ExprType args1Type, | |
150 | ExprType args2Type) { | |
151 | ||
152 |
1
1. impl : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::impl → KILLED |
return functionName -> { |
153 | FunctionSignature functionSignature = | |
154 | new FunctionSignature(functionName, Arrays.asList(args1Type, args2Type)); | |
155 | FunctionBuilder functionBuilder = | |
156 |
1
1. lambda$impl$2 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$impl$2 → KILLED |
arguments -> new FunctionExpression(functionName, arguments) { |
157 | @Override | |
158 | public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) { | |
159 | ExprValue arg1 = arguments.get(0).valueOf(valueEnv); | |
160 | ExprValue arg2 = arguments.get(1).valueOf(valueEnv); | |
161 |
1
1. valueOf : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL$3::valueOf → KILLED |
return function.apply(arg1, arg2); |
162 | } | |
163 | ||
164 | @Override | |
165 | public ExprType type() { | |
166 |
1
1. type : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL$3::type → KILLED |
return returnType; |
167 | } | |
168 | ||
169 | @Override | |
170 | public String toString() { | |
171 |
1
1. toString : replaced return value with "" for org/opensearch/sql/expression/function/FunctionDSL$3::toString → KILLED |
return String.format("%s(%s, %s)", functionName, arguments.get(0).toString(), |
172 | arguments.get(1).toString()); | |
173 | } | |
174 | }; | |
175 |
1
1. lambda$impl$42d8bc60$1 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$impl$42d8bc60$1 → KILLED |
return Pair.of(functionSignature, functionBuilder); |
176 | }; | |
177 | } | |
178 | ||
179 | /** | |
180 | * Triple Function Implementation. | |
181 | * | |
182 | * @param function {@link ExprValue} based unary function. | |
183 | * @param returnType return type. | |
184 | * @param args1Type argument type. | |
185 | * @param args2Type argument type. | |
186 | * @return Binary Function Implementation. | |
187 | */ | |
188 | public static SerializableFunction<FunctionName, Pair<FunctionSignature, FunctionBuilder>> impl( | |
189 | SerializableTriFunction<ExprValue, ExprValue, ExprValue, ExprValue> function, | |
190 | ExprType returnType, | |
191 | ExprType args1Type, | |
192 | ExprType args2Type, | |
193 | ExprType args3Type) { | |
194 | ||
195 |
1
1. impl : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::impl → KILLED |
return functionName -> { |
196 | FunctionSignature functionSignature = | |
197 | new FunctionSignature(functionName, Arrays.asList(args1Type, args2Type, args3Type)); | |
198 | FunctionBuilder functionBuilder = | |
199 |
1
1. lambda$impl$3 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$impl$3 → KILLED |
arguments -> new FunctionExpression(functionName, arguments) { |
200 | @Override | |
201 | public ExprValue valueOf(Environment<Expression, ExprValue> valueEnv) { | |
202 | ExprValue arg1 = arguments.get(0).valueOf(valueEnv); | |
203 | ExprValue arg2 = arguments.get(1).valueOf(valueEnv); | |
204 | ExprValue arg3 = arguments.get(2).valueOf(valueEnv); | |
205 |
1
1. valueOf : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL$4::valueOf → KILLED |
return function.apply(arg1, arg2, arg3); |
206 | } | |
207 | ||
208 | @Override | |
209 | public ExprType type() { | |
210 |
1
1. type : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL$4::type → KILLED |
return returnType; |
211 | } | |
212 | ||
213 | @Override | |
214 | public String toString() { | |
215 |
1
1. toString : replaced return value with "" for org/opensearch/sql/expression/function/FunctionDSL$4::toString → KILLED |
return String.format("%s(%s, %s, %s)", functionName, arguments.get(0).toString(), |
216 | arguments.get(1).toString(), arguments.get(2).toString()); | |
217 | } | |
218 | }; | |
219 |
1
1. lambda$impl$18ff61a6$1 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$impl$18ff61a6$1 → KILLED |
return Pair.of(functionSignature, functionBuilder); |
220 | }; | |
221 | } | |
222 | ||
223 | /** | |
224 | * Wrapper the unary ExprValue function with default NULL and MISSING handling. | |
225 | */ | |
226 | public static SerializableFunction<ExprValue, ExprValue> nullMissingHandling( | |
227 | SerializableFunction<ExprValue, ExprValue> function) { | |
228 |
1
1. nullMissingHandling : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::nullMissingHandling → KILLED |
return value -> { |
229 |
1
1. lambda$nullMissingHandling$878069c8$1 : negated conditional → KILLED |
if (value.isMissing()) { |
230 |
1
1. lambda$nullMissingHandling$878069c8$1 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$nullMissingHandling$878069c8$1 → KILLED |
return ExprValueUtils.missingValue(); |
231 |
1
1. lambda$nullMissingHandling$878069c8$1 : negated conditional → KILLED |
} else if (value.isNull()) { |
232 |
1
1. lambda$nullMissingHandling$878069c8$1 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$nullMissingHandling$878069c8$1 → KILLED |
return ExprValueUtils.nullValue(); |
233 | } else { | |
234 |
1
1. lambda$nullMissingHandling$878069c8$1 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$nullMissingHandling$878069c8$1 → KILLED |
return function.apply(value); |
235 | } | |
236 | }; | |
237 | } | |
238 | ||
239 | /** | |
240 | * Wrapper the binary ExprValue function with default NULL and MISSING handling. | |
241 | */ | |
242 | public static SerializableBiFunction<ExprValue, ExprValue, ExprValue> nullMissingHandling( | |
243 | SerializableBiFunction<ExprValue, ExprValue, ExprValue> function) { | |
244 |
1
1. nullMissingHandling : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::nullMissingHandling → SURVIVED |
return (v1, v2) -> { |
245 |
2
1. lambda$nullMissingHandling$a5005281$1 : negated conditional → KILLED 2. lambda$nullMissingHandling$a5005281$1 : negated conditional → KILLED |
if (v1.isMissing() || v2.isMissing()) { |
246 |
1
1. lambda$nullMissingHandling$a5005281$1 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$nullMissingHandling$a5005281$1 → KILLED |
return ExprValueUtils.missingValue(); |
247 |
2
1. lambda$nullMissingHandling$a5005281$1 : negated conditional → KILLED 2. lambda$nullMissingHandling$a5005281$1 : negated conditional → KILLED |
} else if (v1.isNull() || v2.isNull()) { |
248 |
1
1. lambda$nullMissingHandling$a5005281$1 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$nullMissingHandling$a5005281$1 → KILLED |
return ExprValueUtils.nullValue(); |
249 | } else { | |
250 |
1
1. lambda$nullMissingHandling$a5005281$1 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$nullMissingHandling$a5005281$1 → KILLED |
return function.apply(v1, v2); |
251 | } | |
252 | }; | |
253 | } | |
254 | ||
255 | /** | |
256 | * Wrapper the triple ExprValue function with default NULL and MISSING handling. | |
257 | */ | |
258 | public SerializableTriFunction<ExprValue, ExprValue, ExprValue, ExprValue> nullMissingHandling( | |
259 | SerializableTriFunction<ExprValue, ExprValue, ExprValue, ExprValue> function) { | |
260 |
1
1. nullMissingHandling : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::nullMissingHandling → SURVIVED |
return (v1, v2, v3) -> { |
261 |
3
1. lambda$nullMissingHandling$7c3b465$1 : negated conditional → KILLED 2. lambda$nullMissingHandling$7c3b465$1 : negated conditional → KILLED 3. lambda$nullMissingHandling$7c3b465$1 : negated conditional → KILLED |
if (v1.isMissing() || v2.isMissing() || v3.isMissing()) { |
262 |
1
1. lambda$nullMissingHandling$7c3b465$1 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$nullMissingHandling$7c3b465$1 → KILLED |
return ExprValueUtils.missingValue(); |
263 |
3
1. lambda$nullMissingHandling$7c3b465$1 : negated conditional → KILLED 2. lambda$nullMissingHandling$7c3b465$1 : negated conditional → KILLED 3. lambda$nullMissingHandling$7c3b465$1 : negated conditional → KILLED |
} else if (v1.isNull() || v2.isNull() || v3.isNull()) { |
264 |
1
1. lambda$nullMissingHandling$7c3b465$1 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$nullMissingHandling$7c3b465$1 → KILLED |
return ExprValueUtils.nullValue(); |
265 | } else { | |
266 |
1
1. lambda$nullMissingHandling$7c3b465$1 : replaced return value with null for org/opensearch/sql/expression/function/FunctionDSL::lambda$nullMissingHandling$7c3b465$1 → KILLED |
return function.apply(v1, v2, v3); |
267 | } | |
268 | }; | |
269 | } | |
270 | } | |
Mutations | ||
38 |
1.1 |
|
58 |
1.1 |
|
72 |
1.1 |
|
76 |
1.1 |
|
79 |
1.1 |
|
84 |
1.1 |
|
89 |
1.1 |
|
92 |
1.1 |
|
109 |
1.1 |
|
113 |
1.1 |
|
117 |
1.1 |
|
122 |
1.1 |
|
127 |
1.1 |
|
133 |
1.1 |
|
152 |
1.1 |
|
156 |
1.1 |
|
161 |
1.1 |
|
166 |
1.1 |
|
171 |
1.1 |
|
175 |
1.1 |
|
195 |
1.1 |
|
199 |
1.1 |
|
205 |
1.1 |
|
210 |
1.1 |
|
215 |
1.1 |
|
219 |
1.1 |
|
228 |
1.1 |
|
229 |
1.1 |
|
230 |
1.1 |
|
231 |
1.1 |
|
232 |
1.1 |
|
234 |
1.1 |
|
244 |
1.1 |
|
245 |
1.1 2.2 |
|
246 |
1.1 |
|
247 |
1.1 2.2 |
|
248 |
1.1 |
|
250 |
1.1 |
|
260 |
1.1 |
|
261 |
1.1 2.2 3.3 |
|
262 |
1.1 |
|
263 |
1.1 2.2 3.3 |
|
264 |
1.1 |
|
266 |
1.1 |