Skip to content

Commit 950d9f5

Browse files
committed
Add decay tests to VerifierTests checking nullability of all function arguments
1 parent 88f9bbd commit 950d9f5

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,12 @@ public enum Cap {
12891289
/**
12901290
* Support for the options field of CATEGORIZE.
12911291
*/
1292-
CATEGORIZE_OPTIONS;
1292+
CATEGORIZE_OPTIONS,
1293+
1294+
/**
1295+
* Decay function for custom scoring
1296+
*/
1297+
DECAY_FUNCTION;
12931298

12941299
private final boolean enabled;
12951300

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/Decay.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,38 +177,38 @@ protected TypeResolution resolveType() {
177177

178178
// Spatial decay
179179
if (isSpatialPoint(valueDataType)) {
180-
TypeResolution originResolution = isType(origin, DataType::isSpatialPoint, sourceText(), SECOND, "spatial point");
180+
TypeResolution originResolution = isNotNull(origin, sourceText(), SECOND).and(isType(origin, DataType::isSpatialPoint, sourceText(), SECOND, "spatial point"));
181181
if (originResolution.unresolved()) {
182182
return originResolution;
183183
}
184184

185185
// For a spatial decay the scale should be a distance unit string (e.g. "100km")
186-
TypeResolution scaleResolution = isType(scale, DataType::isString, sourceText(), THIRD, "keyword or text");
186+
TypeResolution scaleResolution = isNotNull(scale, sourceText(), THIRD).and(isType(scale, DataType::isString, sourceText(), THIRD, "keyword or text"));
187187
if (scaleResolution.unresolved()) {
188188
return scaleResolution;
189189
}
190190
}
191191
// Temporal decay
192192
else if (isMillisOrNanos(valueDataType)) {
193-
TypeResolution originResolution = isType(origin, DataType::isMillisOrNanos, sourceText(), SECOND, "datetime or date_nanos");
193+
TypeResolution originResolution = isNotNull(origin, sourceText(), SECOND).and(isType(origin, DataType::isMillisOrNanos, sourceText(), SECOND, "datetime or date_nanos"));
194194
if (originResolution.unresolved()) {
195195
return originResolution;
196196
}
197197

198198
// For a temporal decay the scale should be a time value string (e.g. "5h")
199-
TypeResolution scaleResolution = isType(scale, DataType::isString, sourceText(), THIRD, "date_period or time_duration");
199+
TypeResolution scaleResolution = isNotNull(scale, sourceText(), THIRD).and(isType(scale, DataType::isString, sourceText(), THIRD, "date_period or time_duration"));
200200
if (scaleResolution.unresolved()) {
201201
return scaleResolution;
202202
}
203203
}
204204
// Numeric decay
205205
else {
206-
TypeResolution originResolution = isNumeric(origin, sourceText(), SECOND);
206+
TypeResolution originResolution = isNotNull(origin, sourceText(), SECOND).and(isNumeric(origin, sourceText(), SECOND));
207207
if (originResolution.unresolved()) {
208208
return originResolution;
209209
}
210210

211-
TypeResolution scaleResolution = isNumeric(scale, sourceText(), THIRD);
211+
TypeResolution scaleResolution = isNotNull(scale, sourceText(), THIRD).and(isNumeric(scale, sourceText(), THIRD));
212212
if (scaleResolution.unresolved()) {
213213
return scaleResolution;
214214
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/analysis/VerifierTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,43 @@ public void testRemoteLookupJoinIsDisabled() {
23462346

23472347
}
23482348

2349+
public void testDecayFunctionNullArgs(){
2350+
assumeTrue("Decay function not enabled", EsqlCapabilities.Cap.DECAY_FUNCTION.isEnabled());
2351+
2352+
// First arg cannot be null
2353+
assertEquals(
2354+
"2:23: first argument of [decay(null, origin, scale, 0, 0.5, \"linear\")] cannot be null, received [null]",
2355+
error("row origin = 10, scale = 10\n"
2356+
+ "| eval decay_result = decay(null, origin, scale, 0, 0.5, \"linear\")")
2357+
);
2358+
2359+
// Second arg cannot be null
2360+
assertEquals(
2361+
"2:23: second argument of [decay(value, null, scale, 0, 0.5, \"linear\")] cannot be null, received [null]",
2362+
error("row value = 10, scale = 10\n"
2363+
+ "| eval decay_result = decay(value, null, scale, 0, 0.5, \"linear\")")
2364+
);
2365+
2366+
// Third arg cannot be null
2367+
assertEquals(
2368+
"2:23: third argument of [decay(value, origin, null, 0, 0.5, \"linear\")] cannot be null, received [null]",
2369+
error("row value = 10, origin = 10\n"
2370+
+ "| eval decay_result = decay(value, origin, null, 0, 0.5, \"linear\")")
2371+
);
2372+
2373+
// Fourth arg can be null
2374+
query("row value = 10, origin = 10, scale = 10\n"
2375+
+ "| eval decay_result = decay(value, origin, scale, null, 0.5, \"linear\")");
2376+
2377+
// Fifth arg can be null
2378+
query("row value = 10, origin = 10, scale = 10\n"
2379+
+ "| eval decay_result = decay(value, origin, scale, 0, null, \"linear\")");
2380+
2381+
// Sixth arg can be null
2382+
query("row value = 10, origin = 10, scale = 10\n"
2383+
+ "| eval decay_result = decay(value, origin, scale, 0, 0.5, null)");
2384+
}
2385+
23492386
private void checkFullTextFunctionsInStats(String functionInvocation) {
23502387
query("from test | stats c = max(id) where " + functionInvocation, fullTextAnalyzer);
23512388
query("from test | stats c = max(id) where " + functionInvocation + " or length(title) > 10", fullTextAnalyzer);

0 commit comments

Comments
 (0)