Skip to content

Commit 9167cab

Browse files
committed
K must be set either explicitly or implicitly
1 parent 89d8907 commit 9167cab

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/vector/Knn.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.common.io.stream.StreamInput;
1212
import org.elasticsearch.common.io.stream.StreamOutput;
1313
import org.elasticsearch.index.query.QueryBuilder;
14+
import org.elasticsearch.xpack.esql.capabilities.PostOptimizationVerificationAware;
1415
import org.elasticsearch.xpack.esql.common.Failures;
1516
import org.elasticsearch.xpack.esql.core.InvalidArgumentException;
1617
import org.elasticsearch.xpack.esql.core.expression.Expression;
@@ -60,7 +61,7 @@
6061
import static org.elasticsearch.xpack.esql.core.type.DataType.FLOAT;
6162
import static org.elasticsearch.xpack.esql.core.type.DataType.INTEGER;
6263

63-
public class Knn extends FullTextFunction implements OptionalArgument, VectorFunction {
64+
public class Knn extends FullTextFunction implements OptionalArgument, VectorFunction, PostOptimizationVerificationAware {
6465

6566
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "Knn", Knn::readFrom);
6667

@@ -288,6 +289,14 @@ protected String notSupportedErroMessage() {
288289
return "[{}] {} is only supported in WHERE commands";
289290
}
290291

292+
@Override
293+
public void postOptimizationVerification(Failures failures) {
294+
// Checks that k is set, either because of limit or options
295+
if (limit == null && knnQueryOptions().get(K_FIELD.getPreferredName()) == null) {
296+
failures.add(fail(this, "k must be set either through a LIMIT or using the KNN k option", functionName(), functionType()));
297+
}
298+
}
299+
291300
@Override
292301
protected NodeInfo<? extends Expression> info() {
293302
return NodeInfo.create(this, Knn::new, field(), query(), options());

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanOptimizerTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7995,4 +7995,17 @@ public void testSampleNoPushDownChangePoint() {
79957995
var topN = as(changePoint.child(), TopN.class);
79967996
var source = as(topN.child(), EsRelation.class);
79977997
}
7998+
7999+
public void testKnnFunctionHasLimitOrKSpecified() {
8000+
assumeTrue("KNN must be enabled", EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled());
8001+
8002+
var query = """
8003+
FROM test
8004+
| WHERE KNN(dense_vector, [1.0, 2.0, 3.0])
8005+
| STATS c = COUNT(*)
8006+
""";
8007+
8008+
VerificationException e = expectThrows(VerificationException.class, () -> planTypes(query));
8009+
assertThat(e.getMessage(), containsString("k must be set either through a LIMIT or using the KNN k option"));
8010+
}
79988011
}

0 commit comments

Comments
 (0)