Skip to content

Commit 3ae0e9b

Browse files
committed
Execute premapper after logical optimisation, to allow query builders to be generated with prefilters
1 parent 616b725 commit 3ae0e9b

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/KnnFunctionIT.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,28 @@ public void testKnnNonPushedDown() {
113113
assertEquals(5 + Math.max(0, numDocs - 10 - 1), valuesList.size());
114114
}
115115
}
116+
public void testKnnWithPrefilters() {
117+
float[] queryVector = new float[numDims];
118+
Arrays.fill(queryVector, 1.0f);
119+
120+
// We retrieve 5 from knn, but must be prefiltered with id > 5 or no result will be returned as it would be post-filtered
121+
var query = String.format(Locale.ROOT, """
122+
FROM test METADATA _score
123+
| WHERE knn(vector, %s, 5) AND id > 5
124+
| KEEP id, floats, _score, vector
125+
| SORT _score DESC
126+
| LIMIT 5
127+
""", Arrays.toString(queryVector));
128+
129+
try (var resp = run(query)) {
130+
assertColumnNames(resp.columns(), List.of("id", "floats", "_score", "vector"));
131+
assertColumnTypes(resp.columns(), List.of("integer", "double", "double", "dense_vector"));
116132

133+
List<List<Object>> valuesList = EsqlTestUtils.getValuesList(resp);
134+
// K = 5, 1 more for every id > 10
135+
assertEquals(5, valuesList.size());
136+
}
137+
}
117138
public void testKnnWithLookupJoin() {
118139
float[] queryVector = new float[numDims];
119140
Arrays.fill(queryVector, 1.0f);
@@ -202,6 +223,5 @@ private void createAndPopulateLookupIndex(IndicesAdminClient client, String look
202223

203224
var createRequest = client.prepareCreate(lookupIndexName).setMapping(mapping).setSettings(settingsBuilder.build());
204225
assertAcked(createRequest);
205-
206226
}
207227
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/EsqlSession.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,10 @@ public void execute(EsqlQueryRequest request, EsqlExecutionInfo executionInfo, P
198198
analyzedPlan(parsed, executionInfo, request.filter(), new EsqlCCSUtils.CssPartialErrorsActionListener(executionInfo, listener) {
199199
@Override
200200
public void onResponse(LogicalPlan analyzedPlan) {
201+
LogicalPlan optimizedPlan = optimizedPlan(analyzedPlan);
201202
preMapper.preMapper(
202-
analyzedPlan,
203-
listener.delegateFailureAndWrap((l, p) -> executeOptimizedPlan(request, executionInfo, planRunner, optimizedPlan(p), l))
203+
optimizedPlan,
204+
listener.delegateFailureAndWrap((l, p) -> executeOptimizedPlan(request, executionInfo, planRunner, p, l))
204205
);
205206
}
206207
});

0 commit comments

Comments
 (0)