Skip to content

Commit 3522afb

Browse files
committed
Check k is a constant
1 parent 5d401fc commit 3522afb

File tree

2 files changed

+15
-12
lines changed
  • x-pack/plugin/esql/src
    • main/java/org/elasticsearch/xpack/esql/expression/function/vector
    • test/java/org/elasticsearch/xpack/esql/analysis

2 files changed

+15
-12
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST;
5151
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND;
5252
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.THIRD;
53+
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isFoldable;
5354
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isMapExpression;
5455
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isNotNull;
5556
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isNotNullAndFoldable;
@@ -180,8 +181,9 @@ private TypeResolution resolveQuery() {
180181
}
181182

182183
private TypeResolution resolveK() {
183-
return isNotNull(k(), sourceText(), TypeResolutions.ParamOrdinal.THIRD)
184-
.and(isType(k(), dt -> dt == INTEGER, sourceText(), TypeResolutions.ParamOrdinal.THIRD, "integer"));
184+
return isType(k(), dt -> dt == INTEGER, sourceText(), THIRD, "integer")
185+
.and(isFoldable(k(), sourceText(), THIRD))
186+
.and(isNotNull(k(), sourceText(), THIRD));
185187
}
186188

187189
private TypeResolution resolveOptions() {

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,24 +2173,25 @@ private void checkFullTextFunctionNullArgs(String functionInvocation, String arg
21732173
);
21742174
}
21752175

2176-
public void testFullTextFunctionsConstantQuery() throws Exception {
2177-
checkFullTextFunctionsConstantQuery("match(title, category)", "second");
2178-
checkFullTextFunctionsConstantQuery("qstr(title)", "");
2179-
checkFullTextFunctionsConstantQuery("kql(title)", "");
2180-
checkFullTextFunctionsConstantQuery("match_phrase(title, tags)", "second");
2176+
public void testFullTextFunctionsConstantArg() throws Exception {
2177+
checkFullTextFunctionsConstantArg("match(title, category)", "second");
2178+
checkFullTextFunctionsConstantArg("qstr(title)", "");
2179+
checkFullTextFunctionsConstantArg("kql(title)", "");
2180+
checkFullTextFunctionsConstantArg("match_phrase(title, tags)", "second");
21812181
if (EsqlCapabilities.Cap.MULTI_MATCH_FUNCTION.isEnabled()) {
2182-
checkFullTextFunctionsConstantQuery("multi_match(category, body)", "first");
2183-
checkFullTextFunctionsConstantQuery("multi_match(concat(title, \"world\"), title)", "first");
2182+
checkFullTextFunctionsConstantArg("multi_match(category, body)", "first");
2183+
checkFullTextFunctionsConstantArg("multi_match(concat(title, \"world\"), title)", "first");
21842184
}
21852185
if (EsqlCapabilities.Cap.TERM_FUNCTION.isEnabled()) {
2186-
checkFullTextFunctionsConstantQuery("term(title, tags)", "second");
2186+
checkFullTextFunctionsConstantArg("term(title, tags)", "second");
21872187
}
21882188
if (EsqlCapabilities.Cap.KNN_FUNCTION.isEnabled()) {
2189-
checkFullTextFunctionsConstantQuery("knn(vector, vector, 10)", "second");
2189+
checkFullTextFunctionsConstantArg("knn(vector, vector, 10)", "second");
2190+
checkFullTextFunctionsConstantArg("knn(vector, [0, 1, 2], category)", "third");
21902191
}
21912192
}
21922193

2193-
private void checkFullTextFunctionsConstantQuery(String functionInvocation, String argOrdinal) throws Exception {
2194+
private void checkFullTextFunctionsConstantArg(String functionInvocation, String argOrdinal) throws Exception {
21942195
assertThat(
21952196
error("from test | where " + functionInvocation, fullTextAnalyzer),
21962197
containsString(argOrdinal + " argument of [" + functionInvocation + "] must be a constant")

0 commit comments

Comments
 (0)