Skip to content

Commit d28f2ea

Browse files
committed
Simplify tests
1 parent 6ea4995 commit d28f2ea

File tree

2 files changed

+74
-109
lines changed
  • x-pack/plugin/esql/src

2 files changed

+74
-109
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public String functionName() {
379379
private TypeResolution resolveFields() {
380380
return fields.stream()
381381
.map(
382-
(Expression field) -> isNotNull(field, sourceText(), FIRST).and(
382+
(Expression field) -> isNotNull(field, sourceText(), SECOND).and(
383383
isType(
384384
field,
385385
FIELD_DATA_TYPES::contains,

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

Lines changed: 73 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,8 @@ public void testFieldBasedFullTextFunctions() throws Exception {
12261226
testFieldBasedFunctionNotAllowedAfterCommands("KNN", "function", "knn(vector, [1, 2, 3])");
12271227
}
12281228

1229-
public void testFieldBasedFunctionNotAllowedAfterCommands(String functionName, String functionType, String functionInvocation) throws Exception {
1229+
public void testFieldBasedFunctionNotAllowedAfterCommands(String functionName, String functionType, String functionInvocation)
1230+
throws Exception {
12301231
assertThat(
12311232
error("from test | limit 10 | where " + functionInvocation),
12321233
containsString("[" + functionName + "] " + functionType + " cannot be used after LIMIT")
@@ -1242,22 +1243,20 @@ public void testFieldBasedFunctionNotAllowedAfterCommands(String functionName, S
12421243
public void testFieldBasedWithNonIndexedColumn(String functionName, String functionInvocation, String functionType) {
12431244
assertThat(
12441245
error("from test | eval text = substring(first_name, 1) | where " + functionInvocation),
1245-
containsString("[" + functionName + "] " + functionType + " cannot operate on [text], which is not a field from an index mapping")
1246+
containsString(
1247+
"[" + functionName + "] " + functionType + " cannot operate on [text], which is not a field from an index mapping"
1248+
)
12461249
);
12471250
assertThat(
12481251
error("from test | eval text=concat(first_name, last_name) | where " + functionInvocation),
1249-
containsString("[" + functionName + "] " + functionType + " cannot operate on [text], which is not a field from an index mapping")
1252+
containsString(
1253+
"[" + functionName + "] " + functionType + " cannot operate on [text], which is not a field from an index mapping"
1254+
)
12501255
);
12511256
var keywordInvocation = functionInvocation.replace("text", "text::keyword");
12521257
String keywordError = error("row n = null | eval text = n + 5 | where " + keywordInvocation);
1253-
assertThat(
1254-
keywordError,
1255-
containsString("[" + functionName + "] " + functionType + " cannot operate on")
1256-
);
1257-
assertThat(
1258-
keywordError,
1259-
containsString("which is not a field from an index mapping")
1260-
);
1258+
assertThat(keywordError, containsString("[" + functionName + "] " + functionType + " cannot operate on"));
1259+
assertThat(keywordError, containsString("which is not a field from an index mapping"));
12611260
}
12621261

12631262
public void testQueryStringFunctionsNotAllowedAfterCommands() throws Exception {
@@ -1346,12 +1345,15 @@ public void testFullTextFunctionsOnlyAllowedInWhere() throws Exception {
13461345
checkFullTextFunctionsOnlyAllowedInWhere(":", "first_name:\"Anna\"", "operator");
13471346
checkFullTextFunctionsOnlyAllowedInWhere("QSTR", "qstr(\"Anna\")", "function");
13481347
checkFullTextFunctionsOnlyAllowedInWhere("KQL", "kql(\"Anna\")", "function");
1349-
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
1350-
checkFullTextFunctionsOnlyAllowedInWhere("KNN", "knn(vector, [1, 2, 3])", "function");
1351-
}
13521348
if (EsqlCapabilities.Cap.TERM_FUNCTION.isEnabled()) {
13531349
checkFullTextFunctionsOnlyAllowedInWhere("Term", "term(first_name, \"Anna\")", "function");
13541350
}
1351+
if (EsqlCapabilities.Cap.MULTI_MATCH_FUNCTION.isEnabled()) {
1352+
checkFullTextFunctionsOnlyAllowedInWhere("MultiMatch", "multi_match(\"Anna\", first_name, last_name)", "function");
1353+
}
1354+
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
1355+
checkFullTextFunctionsOnlyAllowedInWhere("KNN", "knn(vector, [1, 2, 3])", "function");
1356+
}
13551357
}
13561358

13571359
private void checkFullTextFunctionsOnlyAllowedInWhere(String functionName, String functionInvocation, String functionType)
@@ -1368,32 +1370,14 @@ private void checkFullTextFunctionsOnlyAllowedInWhere(String functionName, Strin
13681370
"1:47: [" + functionName + "] " + functionType + " is only supported in WHERE and STATS commands",
13691371
error("from test | stats max_salary = max(salary) by " + functionInvocation)
13701372
);
1371-
if( "KQL".equals(functionName) || "QSTR".equals(functionName)) {
1373+
if ("KQL".equals(functionName) || "QSTR".equals(functionName)) {
13721374
assertEquals(
13731375
"1:9: [" + functionName + "] " + functionType + " is only supported in WHERE and STATS commands",
13741376
error("row a = " + functionInvocation)
13751377
);
13761378
}
13771379
}
13781380

1379-
public void testQueryStringFunctionArgNotNullOrConstant() throws Exception {
1380-
assertEquals(
1381-
"1:19: first argument of [qstr(first_name)] must be a constant, received [first_name]",
1382-
error("from test | where qstr(first_name)")
1383-
);
1384-
assertEquals("1:19: first argument of [qstr(null)] cannot be null, received [null]", error("from test | where qstr(null)"));
1385-
// Other value types are tested in QueryStringFunctionTests
1386-
}
1387-
1388-
public void testKqlFunctionArgNotNullOrConstant() throws Exception {
1389-
assertEquals(
1390-
"1:19: argument of [kql(first_name)] must be a constant, received [first_name]",
1391-
error("from test | where kql(first_name)")
1392-
);
1393-
assertEquals("1:19: argument of [kql(null)] cannot be null, received [null]", error("from test | where kql(null)"));
1394-
// Other value types are tested in KqlFunctionTests
1395-
}
1396-
13971381
public void testFullTextFunctionsDisjunctions() {
13981382
checkWithFullTextFunctionsDisjunctions("match(last_name, \"Smith\")");
13991383
checkWithFullTextFunctionsDisjunctions("multi_match(\"Smith\", first_name, last_name)");
@@ -1520,62 +1504,22 @@ private void checkFullTextFunctionsWithNonBooleanFunctions(String functionName,
15201504
);
15211505
}
15221506

1523-
public void testMatchFunctionArgNotConstant() throws Exception {
1524-
assertEquals(
1525-
"1:19: second argument of [match(first_name, first_name)] must be a constant, received [first_name]",
1526-
error("from test | where match(first_name, first_name)")
1527-
);
1528-
assertEquals(
1529-
"1:59: second argument of [match(first_name, query)] must be a constant, received [query]",
1530-
error("from test | eval query = concat(\"first\", \" name\") | where match(first_name, query)")
1531-
);
1532-
// Other value types are tested in QueryStringFunctionTests
1533-
}
1534-
1535-
public void testMatchFunctionNullArgs() throws Exception {
1536-
assertEquals(
1537-
"1:19: first argument of [match(null, \"query\")] cannot be null, received [null]",
1538-
error("from test | where match(null, \"query\")")
1539-
);
1540-
assertEquals(
1541-
"1:19: second argument of [match(first_name, null)] cannot be null, received [null]",
1542-
error("from test | where match(first_name, null)")
1543-
);
1544-
}
1545-
1546-
public void testMatchTargetsExistingField() throws Exception {
1547-
assertEquals("1:39: Unknown column [first_name]", error("from test | keep emp_no | where match(first_name, \"Anna\")"));
1548-
assertEquals("1:33: Unknown column [first_name]", error("from test | keep emp_no | where first_name : \"Anna\""));
1549-
}
1550-
1551-
public void testTermFunctionArgNotConstant() throws Exception {
1552-
assumeTrue("term function capability not available", EsqlCapabilities.Cap.TERM_FUNCTION.isEnabled());
1553-
assertEquals(
1554-
"1:19: second argument of [term(first_name, first_name)] must be a constant, received [first_name]",
1555-
error("from test | where term(first_name, first_name)")
1556-
);
1557-
assertEquals(
1558-
"1:59: second argument of [term(first_name, query)] must be a constant, received [query]",
1559-
error("from test | eval query = concat(\"first\", \" name\") | where term(first_name, query)")
1560-
);
1561-
// Other value types are tested in QueryStringFunctionTests
1562-
}
1563-
1564-
public void testTermFunctionNullArgs() throws Exception {
1565-
assumeTrue("term function capability not available", EsqlCapabilities.Cap.TERM_FUNCTION.isEnabled());
1566-
assertEquals(
1567-
"1:19: first argument of [term(null, \"query\")] cannot be null, received [null]",
1568-
error("from test | where term(null, \"query\")")
1569-
);
1570-
assertEquals(
1571-
"1:19: second argument of [term(first_name, null)] cannot be null, received [null]",
1572-
error("from test | where term(first_name, null)")
1573-
);
1507+
public void testFullTextFunctionsTargetsExistingField() throws Exception {
1508+
testFullTextFunctionTargetsExistingField("match(first_name, \"Anna\")");
1509+
testFullTextFunctionTargetsExistingField("first_name : \"Anna\"");
1510+
if (EsqlCapabilities.Cap.MULTI_MATCH_FUNCTION.isEnabled()) {
1511+
testFullTextFunctionTargetsExistingField("multi_match(\"Anna\", first_name)");
1512+
}
1513+
if (EsqlCapabilities.Cap.TERM_FUNCTION.isEnabled()) {
1514+
testFullTextFunctionTargetsExistingField("term(fist_name, \"Anna\")");
1515+
}
1516+
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
1517+
testFullTextFunctionTargetsExistingField("knn(vector, [0, 1, 2])");
1518+
}
15741519
}
15751520

1576-
public void testTermTargetsExistingField() throws Exception {
1577-
assumeTrue("term function capability not available", EsqlCapabilities.Cap.TERM_FUNCTION.isEnabled());
1578-
assertEquals("1:38: Unknown column [first_name]", error("from test | keep emp_no | where term(first_name, \"Anna\")"));
1521+
private void testFullTextFunctionTargetsExistingField(String functionInvocation) throws Exception {
1522+
assertThat(error("from test | keep emp_no | where " + functionInvocation), containsString("Unknown column"));
15791523
}
15801524

15811525
public void testConditionalFunctionsWithMixedNumericTypes() {
@@ -2093,7 +2037,7 @@ public void testLookupJoinDataTypeMismatch() {
20932037
public void testFullTextFunctionOptions() {
20942038
checkOptionDataTypes(Match.ALLOWED_OPTIONS, "FROM test | WHERE match(first_name, \"Jean\", {\"%s\": %s})");
20952039
checkOptionDataTypes(QueryString.ALLOWED_OPTIONS, "FROM test | WHERE QSTR(\"first_name: Jean\", {\"%s\": %s})");
2096-
if (EsqlCapabilities.Cap.MULTI_MATCH_FUNCTION.isEnabled()){
2040+
if (EsqlCapabilities.Cap.MULTI_MATCH_FUNCTION.isEnabled()) {
20972041
checkOptionDataTypes(MultiMatch.OPTIONS, "FROM test | WHERE MULTI_MATCH(\"Jean\", first_name, last_name, {\"%s\": %s})");
20982042
}
20992043
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
@@ -2161,7 +2105,7 @@ public void testMultiMatchFunctionArgNotConstant() throws Exception {
21612105
}
21622106

21632107
// Should pass eventually once we lift some restrictions on full text search functions.
2164-
public void testMultiMatchFunctionCurrentlyUnsupportedBehaviour() throws Exception {
2108+
public void testFullTextFunctionCurrentlyUnsupportedBehaviour() throws Exception {
21652109
testFullTextFunctionsCurrentlyUnsupportedBehaviour("match(first_name, \"Anna\")");
21662110
testFullTextFunctionsCurrentlyUnsupportedBehaviour("first_name : \"Anna\"");
21672111
if (EsqlCapabilities.Cap.MULTI_MATCH_FUNCTION.isEnabled()) {
@@ -2179,30 +2123,51 @@ private void testFullTextFunctionsCurrentlyUnsupportedBehaviour(String functionI
21792123
);
21802124
}
21812125

2182-
public void testMultiMatchFunctionNullArgs() throws Exception {
2183-
assertEquals(
2184-
"1:19: first argument of [multi_match(\"query\", null)] cannot be null, received [null]",
2185-
error("from test | where multi_match(\"query\", null)")
2186-
);
2187-
assertEquals(
2188-
"1:19: first argument of [multi_match(null, first_name)] cannot be null, received [null]",
2189-
error("from test | where multi_match(null, first_name)")
2190-
);
2126+
public void testFullTextFunctionsNullArgs() throws Exception {
2127+
testFullTextFunctionNullArgs("match(null, \"query\")", "first");
2128+
testFullTextFunctionNullArgs("match(first_name, null)", "second");
2129+
testFullTextFunctionNullArgs("qstr(null)", "");
2130+
testFullTextFunctionNullArgs("kql(null)", "");
2131+
if (EsqlCapabilities.Cap.MULTI_MATCH_FUNCTION.isEnabled()) {
2132+
testFullTextFunctionNullArgs("multi_match(null, first_name)", "first");
2133+
testFullTextFunctionNullArgs("multi_match(\"query\", null)", "second");
2134+
}
2135+
if (EsqlCapabilities.Cap.TERM_FUNCTION.isEnabled()) {
2136+
testFullTextFunctionNullArgs("term(null, \"query\")", "first");
2137+
testFullTextFunctionNullArgs("term(first_name, null)", "second");
2138+
}
2139+
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
2140+
testFullTextFunctionNullArgs("knn(null, [0, 1, 2])", "first");
2141+
testFullTextFunctionNullArgs("knn(vector, null)", "second");
2142+
}
21912143
}
21922144

2193-
public void testMultiMatchTargetsExistingField() throws Exception {
2194-
assertEquals(
2195-
"1:53: Unknown column [first_name]\nline 1:65: Unknown column [last_name]",
2196-
error("from test | keep emp_no | where multi_match(\"Anna\", first_name, last_name)")
2145+
private void testFullTextFunctionNullArgs(String functionInvocation, String argOrdinal) throws Exception {
2146+
assertThat(
2147+
error("from test | where " + functionInvocation),
2148+
containsString(argOrdinal + " argument of [" + functionInvocation + "] cannot be null, received [null]")
21972149
);
21982150
}
21992151

2200-
public void testMultiMatchInsideEval() throws Exception {
2201-
assumeTrue("MultiMatch operator is available just for snapshots", Build.current().isSnapshot());
2202-
assertEquals(
2203-
"1:36: [MultiMatch] function is only supported in WHERE and STATS commands\n"
2204-
+ "line 1:55: [MultiMatch] function cannot operate on [title], which is not a field from an index mapping",
2205-
error("row title = \"brown fox\" | eval x = multi_match(\"fox\", title)")
2152+
public void testFullTextFunctionsConstantQuery() throws Exception {
2153+
testFullTextFunctionsConstantQuery("match(first_name, last_name)", "second");
2154+
testFullTextFunctionsConstantQuery("qstr(first_name)", "");
2155+
testFullTextFunctionsConstantQuery("kql(first_name)", "");
2156+
if (EsqlCapabilities.Cap.MULTI_MATCH_FUNCTION.isEnabled()) {
2157+
testFullTextFunctionsConstantQuery("multi_match(first_name, first_name)", "first");
2158+
}
2159+
if (EsqlCapabilities.Cap.TERM_FUNCTION.isEnabled()) {
2160+
testFullTextFunctionsConstantQuery("term(first_name, last_name)", "second");
2161+
}
2162+
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
2163+
testFullTextFunctionsConstantQuery("knn(vector, vector)", "second");
2164+
}
2165+
}
2166+
2167+
private void testFullTextFunctionsConstantQuery(String functionInvocation, String argOrdinal) throws Exception {
2168+
assertThat(
2169+
error("from test | where " + functionInvocation),
2170+
containsString(argOrdinal + " argument of [" + functionInvocation + "] must be a constant")
22062171
);
22072172
}
22082173

0 commit comments

Comments
 (0)