Skip to content

Commit e35cb96

Browse files
committed
Simplify tests
1 parent ba53a4d commit e35cb96

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)");
@@ -1517,62 +1501,22 @@ private void checkFullTextFunctionsWithNonBooleanFunctions(String functionName,
15171501
);
15181502
}
15191503

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

1573-
public void testTermTargetsExistingField() throws Exception {
1574-
assumeTrue("term function capability not available", EsqlCapabilities.Cap.TERM_FUNCTION.isEnabled());
1575-
assertEquals("1:38: Unknown column [first_name]", error("from test | keep emp_no | where term(first_name, \"Anna\")"));
1518+
private void testFullTextFunctionTargetsExistingField(String functionInvocation) throws Exception {
1519+
assertThat(error("from test | keep emp_no | where " + functionInvocation), containsString("Unknown column"));
15761520
}
15771521

15781522
public void testConditionalFunctionsWithMixedNumericTypes() {
@@ -2090,7 +2034,7 @@ public void testLookupJoinDataTypeMismatch() {
20902034
public void testFullTextFunctionOptions() {
20912035
checkOptionDataTypes(Match.ALLOWED_OPTIONS, "FROM test | WHERE match(first_name, \"Jean\", {\"%s\": %s})");
20922036
checkOptionDataTypes(QueryString.ALLOWED_OPTIONS, "FROM test | WHERE QSTR(\"first_name: Jean\", {\"%s\": %s})");
2093-
if (EsqlCapabilities.Cap.MULTI_MATCH_FUNCTION.isEnabled()){
2037+
if (EsqlCapabilities.Cap.MULTI_MATCH_FUNCTION.isEnabled()) {
20942038
checkOptionDataTypes(MultiMatch.OPTIONS, "FROM test | WHERE MULTI_MATCH(\"Jean\", first_name, last_name, {\"%s\": %s})");
20952039
}
20962040
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
@@ -2158,7 +2102,7 @@ public void testMultiMatchFunctionArgNotConstant() throws Exception {
21582102
}
21592103

21602104
// Should pass eventually once we lift some restrictions on full text search functions.
2161-
public void testMultiMatchFunctionCurrentlyUnsupportedBehaviour() throws Exception {
2105+
public void testFullTextFunctionCurrentlyUnsupportedBehaviour() throws Exception {
21622106
testFullTextFunctionsCurrentlyUnsupportedBehaviour("match(first_name, \"Anna\")");
21632107
testFullTextFunctionsCurrentlyUnsupportedBehaviour("first_name : \"Anna\"");
21642108
if (EsqlCapabilities.Cap.MULTI_MATCH_FUNCTION.isEnabled()) {
@@ -2176,30 +2120,51 @@ private void testFullTextFunctionsCurrentlyUnsupportedBehaviour(String functionI
21762120
);
21772121
}
21782122

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

2190-
public void testMultiMatchTargetsExistingField() throws Exception {
2191-
assertEquals(
2192-
"1:53: Unknown column [first_name]\nline 1:65: Unknown column [last_name]",
2193-
error("from test | keep emp_no | where multi_match(\"Anna\", first_name, last_name)")
2142+
private void testFullTextFunctionNullArgs(String functionInvocation, String argOrdinal) throws Exception {
2143+
assertThat(
2144+
error("from test | where " + functionInvocation),
2145+
containsString(argOrdinal + " argument of [" + functionInvocation + "] cannot be null, received [null]")
21942146
);
21952147
}
21962148

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

0 commit comments

Comments
 (0)