|
10 | 10 | import com.carrotsearch.randomizedtesting.annotations.Name; |
11 | 11 | import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; |
12 | 12 |
|
| 13 | +import org.elasticsearch.index.query.QueryBuilder; |
13 | 14 | import org.elasticsearch.xpack.esql.core.expression.Expression; |
| 15 | +import org.elasticsearch.xpack.esql.core.expression.FieldAttribute; |
| 16 | +import org.elasticsearch.xpack.esql.core.expression.Literal; |
| 17 | +import org.elasticsearch.xpack.esql.core.expression.MapExpression; |
14 | 18 | import org.elasticsearch.xpack.esql.core.tree.Source; |
| 19 | +import org.elasticsearch.xpack.esql.core.type.DataType; |
| 20 | +import org.elasticsearch.xpack.esql.expression.function.AbstractFunctionTestCase; |
15 | 21 | import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier; |
| 22 | +import org.elasticsearch.xpack.esql.expression.function.vector.Knn; |
| 23 | +import org.elasticsearch.xpack.esql.io.stream.PlanStreamOutput; |
| 24 | +import org.elasticsearch.xpack.esql.optimizer.rules.physical.local.LucenePushdownPredicates; |
16 | 25 |
|
| 26 | +import java.util.ArrayList; |
17 | 27 | import java.util.List; |
18 | 28 | import java.util.function.Supplier; |
19 | 29 |
|
20 | | -public class KnnTests extends NoneFieldFullTextFunctionTestCase { |
| 30 | +import static org.elasticsearch.xpack.esql.SerializationTestUtils.serializeDeserialize; |
| 31 | +import static org.elasticsearch.xpack.esql.core.type.DataType.BOOLEAN; |
| 32 | +import static org.elasticsearch.xpack.esql.core.type.DataType.DENSE_VECTOR; |
| 33 | +import static org.elasticsearch.xpack.esql.core.type.DataType.KEYWORD; |
| 34 | +import static org.elasticsearch.xpack.esql.core.type.DataType.UNSUPPORTED; |
| 35 | +import static org.elasticsearch.xpack.esql.planner.TranslatorHandler.TRANSLATOR_HANDLER; |
| 36 | +import static org.hamcrest.Matchers.equalTo; |
| 37 | + |
| 38 | +public class KnnTests extends AbstractFunctionTestCase { |
| 39 | + |
21 | 40 | public KnnTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) { |
22 | | - super(testCaseSupplier); |
| 41 | + this.testCase = testCaseSupplier.get(); |
23 | 42 | } |
24 | 43 |
|
25 | 44 | @ParametersFactory |
26 | 45 | public static Iterable<Object[]> parameters() { |
27 | | - return generateParameters(); |
| 46 | + return parameterSuppliersFromTypedData(addFunctionNamedParams(testCaseSuppliers())); |
| 47 | + } |
| 48 | + |
| 49 | + private static List<TestCaseSupplier> testCaseSuppliers() { |
| 50 | + List<TestCaseSupplier> suppliers = new ArrayList<>(); |
| 51 | + |
| 52 | + suppliers.add( |
| 53 | + TestCaseSupplier.testCaseSupplier( |
| 54 | + new TestCaseSupplier.TypedDataSupplier("dense_vector field", KnnTests::randomDenseVector, DENSE_VECTOR), |
| 55 | + new TestCaseSupplier.TypedDataSupplier("query", KnnTests::randomDenseVector, DENSE_VECTOR, true), |
| 56 | + (d1, d2) -> equalTo("string"), |
| 57 | + BOOLEAN, |
| 58 | + (o1, o2) -> true |
| 59 | + ) |
| 60 | + ); |
| 61 | + |
| 62 | + return suppliers; |
| 63 | + } |
| 64 | + |
| 65 | + private static List<Float> randomDenseVector() { |
| 66 | + int dimensions = randomIntBetween(64, 128); |
| 67 | + List<Float> vector = new ArrayList<>(); |
| 68 | + for (int i = 0; i < dimensions; i++) { |
| 69 | + vector.add(randomFloat()); |
| 70 | + } |
| 71 | + return vector; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Adds function named parameters to all the test case suppliers provided |
| 76 | + */ |
| 77 | + private static List<TestCaseSupplier> addFunctionNamedParams(List<TestCaseSupplier> suppliers) { |
| 78 | + // TODO get to a common class with MatchTests |
| 79 | + List<TestCaseSupplier> result = new ArrayList<>(); |
| 80 | + for (TestCaseSupplier supplier : suppliers) { |
| 81 | + List<DataType> dataTypes = new ArrayList<>(supplier.types()); |
| 82 | + dataTypes.add(UNSUPPORTED); |
| 83 | + result.add(new TestCaseSupplier(supplier.name() + ", options", dataTypes, () -> { |
| 84 | + List<TestCaseSupplier.TypedData> values = new ArrayList<>(supplier.get().getData()); |
| 85 | + values.add( |
| 86 | + new TestCaseSupplier.TypedData( |
| 87 | + new MapExpression( |
| 88 | + Source.EMPTY, |
| 89 | + List.of( |
| 90 | + new Literal(Source.EMPTY, randomAlphaOfLength(10), KEYWORD) |
| 91 | + ) |
| 92 | + ), |
| 93 | + UNSUPPORTED, |
| 94 | + "options" |
| 95 | + ).forceLiteral() |
| 96 | + ); |
| 97 | + |
| 98 | + return new TestCaseSupplier.TestCase(values, equalTo("KnnEvaluator"), BOOLEAN, equalTo(true)); |
| 99 | + })); |
| 100 | + } |
| 101 | + return result; |
28 | 102 | } |
29 | 103 |
|
30 | 104 | @Override |
31 | 105 | protected Expression build(Source source, List<Expression> args) { |
32 | | - return new Kql(source, args.get(0)); |
| 106 | + Knn knn = new Knn(source, args.get(0), args.get(1), args.size() > 2 ? args.get(2) : null); |
| 107 | + // We need to add the QueryBuilder to the match expression, as it is used to implement equals() and hashCode() and |
| 108 | + // thus test the serialization methods. But we can only do this if the parameters make sense . |
| 109 | + if (args.get(0) instanceof FieldAttribute && args.get(1).foldable()) { |
| 110 | + QueryBuilder queryBuilder = TRANSLATOR_HANDLER.asQuery(LucenePushdownPredicates.DEFAULT, knn).toQueryBuilder(); |
| 111 | + knn = (Knn) knn.replaceQueryBuilder(queryBuilder); |
| 112 | + } |
| 113 | + return knn; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Copy of the overridden method that doesn't check for children size, as the {@code options} child isn't serialized in Match. |
| 118 | + */ |
| 119 | + @Override |
| 120 | + protected Expression serializeDeserializeExpression(Expression expression) { |
| 121 | + Expression newExpression = serializeDeserialize( |
| 122 | + expression, |
| 123 | + PlanStreamOutput::writeNamedWriteable, |
| 124 | + in -> in.readNamedWriteable(Expression.class), |
| 125 | + testCase.getConfiguration() // The configuration query should be == to the source text of the function for this to work |
| 126 | + ); |
| 127 | + // Fields use synthetic sources, which can't be serialized. So we use the originals instead. |
| 128 | + return newExpression.replaceChildren(expression.children()); |
33 | 129 | } |
34 | 130 | } |
0 commit comments