|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.optimizer; |
| 9 | + |
| 10 | +import org.apache.lucene.util.SetOnce; |
| 11 | +import org.elasticsearch.action.ActionListener; |
| 12 | +import org.elasticsearch.test.ESTestCase; |
| 13 | +import org.elasticsearch.xpack.core.inference.action.InferenceAction; |
| 14 | +import org.elasticsearch.xpack.core.inference.results.TextEmbeddingBitResults; |
| 15 | +import org.elasticsearch.xpack.core.inference.results.TextEmbeddingByteResults; |
| 16 | +import org.elasticsearch.xpack.core.inference.results.TextEmbeddingFloatResults; |
| 17 | +import org.elasticsearch.xpack.core.inference.results.TextEmbeddingResults; |
| 18 | +import org.elasticsearch.xpack.esql.core.expression.Alias; |
| 19 | +import org.elasticsearch.xpack.esql.core.expression.FoldContext; |
| 20 | +import org.elasticsearch.xpack.esql.core.expression.Literal; |
| 21 | +import org.elasticsearch.xpack.esql.core.tree.Source; |
| 22 | +import org.elasticsearch.xpack.esql.expression.function.inference.TextEmbedding; |
| 23 | +import org.elasticsearch.xpack.esql.expression.function.vector.Knn; |
| 24 | +import org.elasticsearch.xpack.esql.inference.InferenceRunner; |
| 25 | +import org.elasticsearch.xpack.esql.inference.bulk.BulkInferenceRequestIterator; |
| 26 | +import org.elasticsearch.xpack.esql.plan.logical.EsRelation; |
| 27 | +import org.elasticsearch.xpack.esql.plan.logical.Eval; |
| 28 | +import org.elasticsearch.xpack.esql.plan.logical.Filter; |
| 29 | + |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +import static org.elasticsearch.xpack.esql.EsqlTestUtils.as; |
| 33 | +import static org.elasticsearch.xpack.esql.EsqlTestUtils.getFieldAttribute; |
| 34 | +import static org.elasticsearch.xpack.esql.EsqlTestUtils.of; |
| 35 | +import static org.elasticsearch.xpack.esql.EsqlTestUtils.relation; |
| 36 | +import static org.elasticsearch.xpack.esql.core.type.DataType.DENSE_VECTOR; |
| 37 | +import static org.hamcrest.Matchers.equalTo; |
| 38 | +import static org.hamcrest.Matchers.hasSize; |
| 39 | +import static org.hamcrest.Matchers.notNullValue; |
| 40 | + |
| 41 | +public class PreOptimizerTests extends ESTestCase { |
| 42 | + |
| 43 | + public void testEvalFunctionEmbeddingBytes() throws Exception { |
| 44 | + testEvalFunctionEmbedding(BYTES_EMBEDDING_MODEL); |
| 45 | + } |
| 46 | + |
| 47 | + public void testEvalFunctionEmbeddingBits() throws Exception { |
| 48 | + testEvalFunctionEmbedding(BIT_EMBEDDING_MODEL); |
| 49 | + } |
| 50 | + |
| 51 | + public void testEvalFunctionEmbeddingFloats() throws Exception { |
| 52 | + testEvalFunctionEmbedding(FLOAT_EMBEDDING_MODEL); |
| 53 | + } |
| 54 | + |
| 55 | + public void testKnnFunctionEmbeddingBytes() throws Exception { |
| 56 | + testKnnFunctionEmbedding(BYTES_EMBEDDING_MODEL); |
| 57 | + } |
| 58 | + |
| 59 | + public void testKnnFunctionEmbeddingBits() throws Exception { |
| 60 | + testKnnFunctionEmbedding(BIT_EMBEDDING_MODEL); |
| 61 | + } |
| 62 | + |
| 63 | + public void testKnnFunctionEmbeddingFloats() throws Exception { |
| 64 | + testKnnFunctionEmbedding(FLOAT_EMBEDDING_MODEL); |
| 65 | + } |
| 66 | + |
| 67 | + private void testEvalFunctionEmbedding(TextEmbeddingModelMock textEmbeddingModel) throws Exception { |
| 68 | + String inferenceId = randomUUID(); |
| 69 | + String query = String.join(" ", randomArray(1, String[]::new, () -> randomAlphaOfLength(randomIntBetween(1, 10)))); |
| 70 | + String fieldName = randomIdentifier(); |
| 71 | + |
| 72 | + PreOptimizer preOptimizer = new PreOptimizer(mockInferenceRunner(textEmbeddingModel), FoldContext.small()); |
| 73 | + EsRelation relation = relation(); |
| 74 | + Eval eval = new Eval( |
| 75 | + Source.EMPTY, |
| 76 | + relation, |
| 77 | + List.of(new Alias(Source.EMPTY, fieldName, new TextEmbedding(Source.EMPTY, of(query), of(inferenceId)))) |
| 78 | + ); |
| 79 | + |
| 80 | + SetOnce<Object> preOptimizedPlanHolder = new SetOnce<>(); |
| 81 | + preOptimizer.preOptimize(eval, ActionListener.wrap(preOptimizedPlanHolder::set, ESTestCase::fail)); |
| 82 | + |
| 83 | + assertBusy(() -> { |
| 84 | + assertThat(preOptimizedPlanHolder.get(), notNullValue()); |
| 85 | + Eval preOptimizedEval = as(preOptimizedPlanHolder.get(), Eval.class); |
| 86 | + assertThat(preOptimizedEval.fields(), hasSize(1)); |
| 87 | + assertThat(preOptimizedEval.fields().get(0).name(), equalTo(fieldName)); |
| 88 | + Literal preOptimizedQuery = as(preOptimizedEval.fields().get(0).child(), Literal.class); |
| 89 | + assertThat(preOptimizedQuery.dataType(), equalTo(DENSE_VECTOR)); |
| 90 | + assertThat(preOptimizedQuery.value(), equalTo(textEmbeddingModel.embedding(query))); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + private void testKnnFunctionEmbedding(TextEmbeddingModelMock textEmbeddingModel) throws Exception { |
| 95 | + String inferenceId = randomUUID(); |
| 96 | + String query = String.join(" ", randomArray(1, String[]::new, () -> randomAlphaOfLength(randomIntBetween(1, 10)))); |
| 97 | + |
| 98 | + PreOptimizer preOptimizer = new PreOptimizer(mockInferenceRunner(textEmbeddingModel), FoldContext.small()); |
| 99 | + EsRelation relation = relation(); |
| 100 | + Filter filter = new Filter( |
| 101 | + Source.EMPTY, |
| 102 | + relation, |
| 103 | + new Knn(Source.EMPTY, getFieldAttribute("a"), new TextEmbedding(Source.EMPTY, of(query), of(inferenceId)), of(10), null) |
| 104 | + ); |
| 105 | + Knn knn = as(filter.condition(), Knn.class); |
| 106 | + |
| 107 | + SetOnce<Object> preOptimizedHolder = new SetOnce<>(); |
| 108 | + preOptimizer.preOptimize(filter, ActionListener.wrap(preOptimizedHolder::set, ESTestCase::fail)); |
| 109 | + |
| 110 | + assertBusy(() -> { |
| 111 | + assertThat(preOptimizedHolder.get(), notNullValue()); |
| 112 | + Filter preOptimizedFilter = as(preOptimizedHolder.get(), Filter.class); |
| 113 | + Knn preOptimizedKnn = as(preOptimizedFilter.condition(), Knn.class); |
| 114 | + assertThat(preOptimizedKnn.field(), equalTo(knn.field())); |
| 115 | + assertThat(preOptimizedKnn.k(), equalTo(knn.k())); |
| 116 | + assertThat(preOptimizedKnn.options(), equalTo(knn.options())); |
| 117 | + |
| 118 | + Literal preOptimizedQuery = as(preOptimizedKnn.query(), Literal.class); |
| 119 | + assertThat(preOptimizedQuery.dataType(), equalTo(DENSE_VECTOR)); |
| 120 | + assertThat(preOptimizedQuery.value(), equalTo(textEmbeddingModel.embedding(query))); |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + private InferenceRunner mockInferenceRunner(TextEmbeddingModelMock textEmbeddingModel) { |
| 125 | + return new InferenceRunner() { |
| 126 | + @Override |
| 127 | + public void execute(InferenceAction.Request request, ActionListener<InferenceAction.Response> listener) { |
| 128 | + listener.onResponse(new InferenceAction.Response(textEmbeddingModel.embeddingResults(request.getInput().getFirst()))); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void executeBulk(BulkInferenceRequestIterator requests, ActionListener<List<InferenceAction.Response>> listener) { |
| 133 | + listener.onFailure( |
| 134 | + new UnsupportedOperationException("executeBulk should not be invoked for plans without inference functions") |
| 135 | + ); |
| 136 | + } |
| 137 | + }; |
| 138 | + } |
| 139 | + |
| 140 | + private interface TextEmbeddingModelMock { |
| 141 | + TextEmbeddingResults<?> embeddingResults(String input); |
| 142 | + |
| 143 | + float[] embedding(String input); |
| 144 | + } |
| 145 | + |
| 146 | + private static final TextEmbeddingModelMock FLOAT_EMBEDDING_MODEL = new TextEmbeddingModelMock() { |
| 147 | + public TextEmbeddingResults<?> embeddingResults(String input) { |
| 148 | + TextEmbeddingFloatResults.Embedding embedding = new TextEmbeddingFloatResults.Embedding(embedding(input)); |
| 149 | + return new TextEmbeddingFloatResults(List.of(embedding)); |
| 150 | + } |
| 151 | + |
| 152 | + public float[] embedding(String input) { |
| 153 | + String[] tokens = input.split("\\s+"); |
| 154 | + float[] embedding = new float[tokens.length]; |
| 155 | + for (int i = 0; i < tokens.length; i++) { |
| 156 | + embedding[i] = tokens[i].length(); |
| 157 | + } |
| 158 | + return embedding; |
| 159 | + } |
| 160 | + }; |
| 161 | + |
| 162 | + private static final TextEmbeddingModelMock BYTES_EMBEDDING_MODEL = new TextEmbeddingModelMock() { |
| 163 | + public TextEmbeddingResults<?> embeddingResults(String input) { |
| 164 | + TextEmbeddingByteResults.Embedding embedding = new TextEmbeddingByteResults.Embedding(bytes(input)); |
| 165 | + return new TextEmbeddingBitResults(List.of(embedding)); |
| 166 | + } |
| 167 | + |
| 168 | + private byte[] bytes(String input) { |
| 169 | + return input.getBytes(); |
| 170 | + } |
| 171 | + |
| 172 | + public float[] embedding(String input) { |
| 173 | + return new TextEmbeddingByteResults.Embedding(bytes(input)).toFloatArray(); |
| 174 | + } |
| 175 | + }; |
| 176 | + |
| 177 | + private static final TextEmbeddingModelMock BIT_EMBEDDING_MODEL = new TextEmbeddingModelMock() { |
| 178 | + public TextEmbeddingResults<?> embeddingResults(String input) { |
| 179 | + TextEmbeddingByteResults.Embedding embedding = new TextEmbeddingByteResults.Embedding(bytes(input)); |
| 180 | + return new TextEmbeddingBitResults(List.of(embedding)); |
| 181 | + } |
| 182 | + |
| 183 | + private byte[] bytes(String input) { |
| 184 | + String[] tokens = input.split("\\s+"); |
| 185 | + byte[] embedding = new byte[tokens.length]; |
| 186 | + for (int i = 0; i < tokens.length; i++) { |
| 187 | + embedding[i] = (byte) (tokens[i].length() % 2); |
| 188 | + } |
| 189 | + return embedding; |
| 190 | + } |
| 191 | + |
| 192 | + public float[] embedding(String input) { |
| 193 | + return new TextEmbeddingByteResults.Embedding(bytes(input)).toFloatArray(); |
| 194 | + } |
| 195 | + }; |
| 196 | +} |
0 commit comments