Skip to content

Commit a53eab3

Browse files
committed
Add tests
1 parent 1a9b44c commit a53eab3

File tree

2 files changed

+66
-20
lines changed

2 files changed

+66
-20
lines changed

x-pack/plugin/esql/qa/testFixtures/src/main/resources/vector-cosine-similarity.csv-spec

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,28 @@ gainsboro | 0.908
2424
gray | 0.908
2525
;
2626

27-
# TODO Need to implement a conversion function to convert a non-foldable row to a dense_vector
28-
similarityWithRow-Ignore
29-
required_capability: cosine_vector_similarity_function
27+
similarityAsPartOfExpression
28+
required_capability: cosine_vector_similarity_function
3029

31-
row vector = [1, 2, 3]
32-
| eval similarity = round(v_cosine(vector, [0, 1, 2]), 3)
33-
| sort similarity desc, color asc
34-
| limit 10
35-
| keep color, similarity
36-
;
30+
from colors
31+
| where color != "black"
32+
| eval score = round((1 + v_cosine(rgb_vector, [0, 255, 255]) / 2), 3)
33+
| sort score desc, color asc
34+
| limit 10
35+
| keep color, score
36+
;
3737

38-
color:text | similarity:double
39-
cyan | 1.0
40-
teal | 1.0
41-
turquoise | 0.989
42-
aqua marine | 0.965
43-
azure | 0.916
44-
lavender | 0.914
45-
honeydew | 0.912
46-
mint cream | 0.912
47-
gainsboro | 0.908
48-
gray | 0.908
38+
color:text | score:double
39+
cyan | 1.5
40+
teal | 1.5
41+
turquoise | 1.495
42+
aqua marine | 1.482
43+
azure | 1.458
44+
lavender | 1.457
45+
honeydew | 1.456
46+
mint cream | 1.456
47+
gainsboro | 1.454
48+
gray | 1.454
4949
;
5050

5151
similarityWithLiteralVectors
@@ -56,6 +56,34 @@ row a = 1
5656
| keep similarity
5757
;
5858

59+
similarity:double
60+
0.978
61+
;
62+
63+
similarityWithStats
64+
required_capability: cosine_vector_similarity_function
65+
66+
from colors
67+
| where color != "black"
68+
| eval similarity = round(v_cosine(rgb_vector, [0, 255, 255]), 3)
69+
| stats avg = round(avg(similarity), 3), min = min(similarity), max = max(similarity)
70+
;
71+
72+
avg:double | min:double | max:double
73+
0.832 | 0.5 | 1.0
74+
;
75+
76+
# TODO Need to implement a conversion function to convert a non-foldable row to a dense_vector
77+
similarityWithRow-Ignore
78+
required_capability: cosine_vector_similarity_function
79+
80+
row vector = [1, 2, 3]
81+
| eval similarity = round(v_cosine(vector, [0, 1, 2]), 3)
82+
| sort similarity desc, color asc
83+
| limit 10
84+
| keep color, similarity
85+
;
86+
5987
similarity:double
6088
0.978
6189
;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,6 +2413,24 @@ private void checkDenseVectorImplicitCastingSimilarityFunction(String similarity
24132413
assertThat(right.value(), equalTo(List.of(0.342, 0.164, 0.234)));;
24142414
}
24152415

2416+
public void testNoDenseVectorFailsSimilarityFunction() {
2417+
if (EsqlCapabilities.Cap.COSINE_VECTOR_SIMILARITY_FUNCTION.isEnabled()) {
2418+
checkNoDenseVectorFailsSimilarityFunction("v_cosine([0, 1, 2], 0.342)");
2419+
}
2420+
}
2421+
2422+
private void checkNoDenseVectorFailsSimilarityFunction(String similarityFunction) {
2423+
var query = String.format(Locale.ROOT, "row a = 1 | eval similarity = %s", similarityFunction);
2424+
VerificationException error = expectThrows(VerificationException.class, () -> analyze(query));
2425+
assertThat(
2426+
error.getMessage(),
2427+
containsString(
2428+
"second argument of [" + similarityFunction + "] must be"
2429+
+ " [dense_vector], found value [0.342] type [double]"
2430+
)
2431+
);
2432+
}
2433+
24162434
public void testRateRequiresCounterTypes() {
24172435
assumeTrue("rate requires snapshot builds", Build.current().isSnapshot());
24182436
Analyzer analyzer = analyzer(tsdbIndexResolution());

0 commit comments

Comments
 (0)