Skip to content

Commit 77011c1

Browse files
committed
Add verifier tests, revert some changes to mappings
1 parent 34968ad commit 77011c1

File tree

4 files changed

+46
-76
lines changed

4 files changed

+46
-76
lines changed

x-pack/plugin/esql/qa/testFixtures/src/main/resources/dense_vector.csv-spec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ required_capability: dense_vector_field_type
3535
FROM dense_vector
3636
| EVAL v = vector
3737
| RENAME v AS new_vector
38-
| DROP vector, first_name, last_name
38+
| DROP vector
3939
| SORT id
4040
;
4141

x-pack/plugin/esql/qa/testFixtures/src/main/resources/mapping-default.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@
7575
"type" : "keyword"
7676
}
7777
}
78+
},
79+
"vector": {
80+
"type": "dense_vector",
81+
"similarity": "l2_norm"
7882
}
7983
}
8084
}

x-pack/plugin/esql/qa/testFixtures/src/main/resources/mapping-dense_vector.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
"id": {
44
"type": "long"
55
},
6-
"first_name": {
7-
"type": "text"
8-
},
9-
"last_name": {
10-
"type": "text"
11-
},
126
"vector": {
137
"type": "dense_vector",
148
"similarity": "l2_norm"

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

Lines changed: 41 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,10 @@ public void testMatchOperatornOnlyAllowedInWhere() throws Exception {
13911391
checkFullTextFunctionsOnlyAllowedInWhere(":", "first_name:\"Anna\"", "operator");
13921392
}
13931393

1394+
public void testKnnFunctionOnlyAllowedInWhere() throws Exception {
1395+
checkFullTextFunctionsOnlyAllowedInWhere("KNN", "knn(vector, [1, 2, 3])", "function");
1396+
}
1397+
13941398
private void checkFullTextFunctionsOnlyAllowedInWhere(String functionName, String functionInvocation, String functionType)
13951399
throws Exception {
13961400
assertEquals(
@@ -1425,84 +1429,57 @@ public void testKqlFunctionArgNotNullOrConstant() throws Exception {
14251429
// Other value types are tested in KqlFunctionTests
14261430
}
14271431

1428-
public void testQueryStringWithDisjunctions() {
1429-
checkWithDisjunctions("qstr(\"first_name: Anna\")");
1430-
}
1431-
1432-
public void testKqlFunctionWithDisjunctions() {
1433-
checkWithDisjunctions("kql(\"first_name: Anna\")");
1434-
}
1435-
1436-
public void testMatchFunctionWithDisjunctions() {
1437-
checkWithDisjunctions("match(first_name, \"Anna\")");
1438-
}
1439-
1440-
public void testTermFunctionWithDisjunctions() {
1441-
assumeTrue("term function capability not available", EsqlCapabilities.Cap.TERM_FUNCTION.isEnabled());
1442-
checkWithDisjunctions("term(first_name, \"Anna\")");
1443-
}
1444-
1445-
public void testMatchOperatorWithDisjunctions() {
1446-
checkWithDisjunctions("first_name : \"Anna\"");
1447-
}
1448-
1449-
private void checkWithDisjunctions(String functionInvocation) {
1450-
query("from test | where " + functionInvocation + " or length(first_name) > 12");
1451-
query(
1452-
"from test | where ("
1453-
+ functionInvocation
1454-
+ " or first_name is not null) or (length(first_name) > 12 and match(last_name, \"Smith\"))"
1455-
);
1456-
query("from test | where " + functionInvocation + " or (last_name is not null and first_name is null)");
1457-
}
1458-
14591432
public void testFullTextFunctionsDisjunctions() {
1460-
checkWithFullTextFunctionsDisjunctions("match(last_name, \"Smith\")", defaultAnalyzer);
1461-
checkWithFullTextFunctionsDisjunctions("multi_match(\"Smith\", first_name, last_name)", defaultAnalyzer);
1462-
checkWithFullTextFunctionsDisjunctions("last_name : \"Smith\"", defaultAnalyzer);
1463-
checkWithFullTextFunctionsDisjunctions("qstr(\"last_name: Smith\")", defaultAnalyzer);
1464-
checkWithFullTextFunctionsDisjunctions("kql(\"last_name: Smith\")", defaultAnalyzer);
1465-
Analyzer analyzer = AnalyzerTestUtils.analyzer(loadMapping("mapping-dense_vector.json", "test"));
1466-
checkWithFullTextFunctionsDisjunctions("knn(vector, [1, 2, 3])", analyzer);
1433+
checkWithFullTextFunctionsDisjunctions("match(last_name, \"Smith\")");
1434+
checkWithFullTextFunctionsDisjunctions("multi_match(\"Smith\", first_name, last_name)");
1435+
checkWithFullTextFunctionsDisjunctions("last_name : \"Smith\"");
1436+
checkWithFullTextFunctionsDisjunctions("qstr(\"last_name: Smith\")");
1437+
checkWithFullTextFunctionsDisjunctions("kql(\"last_name: Smith\")");
1438+
if (EsqlCapabilities.Cap.TERM_FUNCTION.isEnabled()) {
1439+
checkWithFullTextFunctionsDisjunctions("term(last_name, \"Smith\")");
1440+
}
1441+
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
1442+
checkWithFullTextFunctionsDisjunctions("knn(vector, [1, 2, 3])");
1443+
}
14671444
}
14681445

1469-
private void checkWithFullTextFunctionsDisjunctions(String functionInvocation, Analyzer analyzer) {
1446+
private void checkWithFullTextFunctionsDisjunctions(String functionInvocation) {
14701447

14711448
// Disjunctions with non-pushable functions - scoring
1472-
query("from test | where " + functionInvocation + " or length(first_name) > 10", analyzer);
1473-
query("from test | where match(last_name, \"Anneke\") or (" + functionInvocation + " and length(first_name) > 10)", analyzer);
1449+
query("from test | where " + functionInvocation + " or length(first_name) > 10");
1450+
query("from test | where match(last_name, \"Anneke\") or (" + functionInvocation + " and length(first_name) > 10)");
14741451
query(
14751452
"from test | where ("
14761453
+ functionInvocation
14771454
+ " and length(first_name) > 0) or (match(last_name, \"Anneke\") and length(first_name) > 10)"
1478-
, analyzer);
1455+
);
14791456

14801457
// Disjunctions with non-pushable functions - no scoring
1481-
query("from test | where " + functionInvocation + " or length(first_name) > 10", analyzer);
1482-
query("from test | where match(last_name, \"Anneke\") or (" + functionInvocation + " and length(first_name) > 10)", analyzer);
1458+
query("from test | where " + functionInvocation + " or length(first_name) > 10");
1459+
query("from test | where match(last_name, \"Anneke\") or (" + functionInvocation + " and length(first_name) > 10)");
14831460
query(
14841461
"from test | where ("
14851462
+ functionInvocation
14861463
+ " and length(first_name) > 0) or (match(last_name, \"Anneke\") and length(first_name) > 10)"
1487-
, analyzer);
1464+
);
14881465

14891466
// Disjunctions with full text functions - no scoring
1490-
query("from test | where " + functionInvocation + " or match(first_name, \"Anna\")", analyzer);
1491-
query("from test | where " + functionInvocation + " or not match(first_name, \"Anna\")", analyzer);
1492-
query("from test | where (" + functionInvocation + " or match(first_name, \"Anna\")) and length(first_name) > 10", analyzer);
1493-
query("from test | where (" + functionInvocation + " or match(first_name, \"Anna\")) and match(last_name, \"Smith\")", analyzer);
1494-
query("from test | where " + functionInvocation + " or (match(first_name, \"Anna\") and match(last_name, \"Smith\"))", analyzer);
1467+
query("from test | where " + functionInvocation + " or match(first_name, \"Anna\")");
1468+
query("from test | where " + functionInvocation + " or not match(first_name, \"Anna\")");
1469+
query("from test | where (" + functionInvocation + " or match(first_name, \"Anna\")) and length(first_name) > 10");
1470+
query("from test | where (" + functionInvocation + " or match(first_name, \"Anna\")) and match(last_name, \"Smith\")");
1471+
query("from test | where " + functionInvocation + " or (match(first_name, \"Anna\") and match(last_name, \"Smith\"))");
14951472

14961473
// Disjunctions with full text functions - scoring
1497-
query("from test metadata _score | where " + functionInvocation + " or match(first_name, \"Anna\")", analyzer);
1498-
query("from test metadata _score | where " + functionInvocation + " or not match(first_name, \"Anna\")", analyzer);
1499-
query("from test metadata _score | where (" + functionInvocation + " or match(first_name, \"Anna\")) and length(first_name) > 10", analyzer);
1474+
query("from test metadata _score | where " + functionInvocation + " or match(first_name, \"Anna\")");
1475+
query("from test metadata _score | where " + functionInvocation + " or not match(first_name, \"Anna\")");
1476+
query("from test metadata _score | where (" + functionInvocation + " or match(first_name, \"Anna\")) and length(first_name) > 10");
15001477
query(
15011478
"from test metadata _score | where (" + functionInvocation + " or match(first_name, \"Anna\")) and match(last_name, \"Smith\")"
1502-
, analyzer);
1479+
);
15031480
query(
15041481
"from test metadata _score | where " + functionInvocation + " or (match(first_name, \"Anna\") and match(last_name, \"Smith\"))"
1505-
, analyzer);
1482+
);
15061483

15071484
}
15081485

@@ -2175,30 +2152,29 @@ public void testLookupJoinDataTypeMismatch() {
21752152
}
21762153

21772154
public void testMatchOptions() {
2178-
checkOptionDataTypes(Match.ALLOWED_OPTIONS, "FROM test | WHERE match(first_name, \"Jean\", {\"%s\": %s})", defaultAnalyzer);
2155+
checkOptionDataTypes(Match.ALLOWED_OPTIONS, "FROM test | WHERE match(first_name, \"Jean\", {\"%s\": %s})");
21792156
}
21802157

21812158
public void testMultiMatchOptions() {
21822159
checkOptionDataTypes(
21832160
MultiMatch.OPTIONS,
2184-
"FROM test | WHERE MULTI_MATCH(\"Jean\", first_name, last_name, {\"%s\": %s})",
2185-
defaultAnalyzer
2161+
"FROM test | WHERE MULTI_MATCH(\"Jean\", first_name, last_name, {\"%s\": %s})"
21862162
);
21872163
}
21882164

21892165
public void testQueryStringOptions() {
2190-
checkOptionDataTypes(QueryString.ALLOWED_OPTIONS, "FROM test | WHERE QSTR(\"first_name: Jean\", {\"%s\": %s})", defaultAnalyzer);
2166+
checkOptionDataTypes(QueryString.ALLOWED_OPTIONS, "FROM test | WHERE QSTR(\"first_name: Jean\", {\"%s\": %s})");
21912167
}
21922168

21932169
public void testKnnOptions() {
2194-
Analyzer analyzer = AnalyzerTestUtils.analyzer(loadMapping("mapping-colors.json", "colors"));
2195-
checkOptionDataTypes(Knn.ALLOWED_OPTIONS, "FROM colors | WHERE KNN(rgb_vector, [0.1, 0.2, 0.3], {\"%s\": %s})", analyzer);
2170+
assumeTrue("knn must be enabled", EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled());
2171+
checkOptionDataTypes(Knn.ALLOWED_OPTIONS, "FROM test | WHERE KNN(vector, [0.1, 0.2, 0.3], {\"%s\": %s})");
21962172
}
21972173

21982174
/**
21992175
* Check all data types for available options. When conversion is not possible, checks that it's an error
22002176
*/
2201-
private void checkOptionDataTypes(Map<String, DataType> allowedOptionsMap, String queryTemplate, Analyzer analyzer) {
2177+
private void checkOptionDataTypes(Map<String, DataType> allowedOptionsMap, String queryTemplate) {
22022178
DataType[] optionTypes = new DataType[] { INTEGER, LONG, FLOAT, DOUBLE, KEYWORD, BOOLEAN };
22032179
for (Map.Entry<String, DataType> allowedOptions : allowedOptionsMap.entrySet()) {
22042180
String optionName = allowedOptions.getKey();
@@ -2217,18 +2193,18 @@ private void checkOptionDataTypes(Map<String, DataType> allowedOptionsMap, Strin
22172193
// Check conversion is possible
22182194
DataTypeConverter.convert(optionValue, optionType);
22192195
// If no exception was thrown, conversion is possible and should be done
2220-
query(query, analyzer);
2196+
query(query);
22212197
} catch (InvalidArgumentException e) {
22222198
// Conversion is not possible, query should fail
2223-
String error = error(query, analyzer);
2199+
String error = error(query);
22242200
assertThat(error, containsString("Invalid option [" + optionName + "]"));
22252201
assertThat(error, containsString("cannot cast [" + optionValue + "] to [" + optionType.typeName() + "]"));
22262202
}
22272203
}
22282204
}
22292205

22302206
String errorQuery = String.format(Locale.ROOT, queryTemplate, "unknown_option", "\"any_value\"");
2231-
assertThat(error(errorQuery, analyzer), containsString("Invalid option [unknown_option]"));
2207+
assertThat(error(errorQuery), containsString("Invalid option [unknown_option]"));
22322208
}
22332209

22342210
private static String exampleValueForType(DataType currentType) {
@@ -2272,10 +2248,6 @@ public void testMultiMatchFunctionNotAllowedAfterCommands() throws Exception {
22722248
);
22732249
}
22742250

2275-
public void testMultiMatchFunctionWithDisjunctions() {
2276-
checkWithDisjunctions("multi_match(\"Anna\", first_name, last_name)");
2277-
}
2278-
22792251
public void testMultiMatchFunctionWithNonBooleanFunctions() {
22802252
checkFullTextFunctionsWithNonBooleanFunctions("MultiMatch", "multi_match(\"Anna\", first_name, last_name)", "function");
22812253
}

0 commit comments

Comments
 (0)