Skip to content

Commit 924b457

Browse files
author
afoucret
committed
Throwing a parsing exception when the RERANK (or COMPLETION) command is disabled.
1 parent 088a98f commit 924b457

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/LogicalPlanBuilder.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.elasticsearch.xpack.esql.expression.predicate.logical.Not;
5656
import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.Equals;
5757
import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.EsqlBinaryComparison;
58+
import org.elasticsearch.xpack.esql.inference.InferenceCommandConfig;
5859
import org.elasticsearch.xpack.esql.parser.promql.PromqlParserUtils;
5960
import org.elasticsearch.xpack.esql.plan.EsqlStatement;
6061
import org.elasticsearch.xpack.esql.plan.IndexPattern;
@@ -1119,9 +1120,14 @@ public PlanFactory visitRerankCommand(EsqlBaseParser.RerankCommandContext ctx) {
11191120
throw qualifiersUnsupportedInFieldDefinitions(scoreAttribute.source(), ctx.targetField.getText());
11201121
}
11211122

1122-
Literal rowLimit = Literal.integer(Source.EMPTY, context.inferenceCommandConfigProvider().get("rerank").rowLimit());
1123+
InferenceCommandConfig commandConfig = context.inferenceCommandConfigProvider().get("rerank");
1124+
Literal rowLimit = Literal.integer(Source.EMPTY, commandConfig.rowLimit());
11231125

11241126
return p -> {
1127+
if (commandConfig.enabled() == false) {
1128+
throw new ParsingException(source, "RERANK command is disabled");
1129+
}
1130+
11251131
checkForRemoteClusters(p, source, "RERANK");
11261132
return applyRerankOptions(
11271133
new Rerank(source, p, queryText, rerankFields, scoreAttribute, rowLimit),
@@ -1164,11 +1170,17 @@ public PlanFactory visitCompletionCommand(EsqlBaseParser.CompletionCommandContex
11641170
if (targetField.qualifier() != null) {
11651171
throw qualifiersUnsupportedInFieldDefinitions(targetField.source(), ctx.targetField.getText());
11661172
}
1167-
1168-
Literal rowLimit = Literal.integer(Source.EMPTY, context.inferenceCommandConfigProvider().get("completion").rowLimit());
1173+
1174+
InferenceCommandConfig commandConfig = context.inferenceCommandConfigProvider().get("completion");
1175+
Literal rowLimit = Literal.integer(Source.EMPTY, commandConfig.rowLimit());
11691176

11701177
return p -> {
1178+
if (commandConfig.enabled() == false) {
1179+
throw new ParsingException(source, "COMPLETION command is disabled");
1180+
}
1181+
11711182
checkForRemoteClusters(p, source, "COMPLETION");
1183+
11721184
return applyCompletionOptions(new Completion(source, p, prompt, targetField, rowLimit), ctx.commandNamedParameters());
11731185
};
11741186
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4224,6 +4224,17 @@ public void testRerankRowLimitOverride() {
42244224
assertThat(plan.rowLimit(), equalTo(Literal.integer(EMPTY, customRowLimit)));
42254225
}
42264226

4227+
public void testRerankCommandDisabled() {
4228+
EsqlParser parserWithDisabledRerank = new EsqlParser(
4229+
Settings.builder().put(InferenceCommandConfig.RERANK_ENABLED_SETTING.getKey(), false).build()
4230+
);
4231+
ParsingException pe = expectThrows(
4232+
ParsingException.class,
4233+
() -> parserWithDisabledRerank.createStatement("FROM foo* | RERANK \"query text\" ON title")
4234+
);
4235+
assertThat(pe.getMessage(), containsString("RERANK command is disabled"));
4236+
}
4237+
42274238
public void testInvalidRerank() {
42284239
expectError(
42294240
"FROM foo* | RERANK \"query text\" ON title WITH { \"inference_id\": 3 }",
@@ -4303,6 +4314,19 @@ public void testCompletionRowLimitOverride() {
43034314
assertThat(plan.rowLimit(), equalTo(Literal.integer(EMPTY, customRowLimit)));
43044315
}
43054316

4317+
public void testCompletionCommandDisabled() {
4318+
EsqlParser parserWithDisabledCompletion = new EsqlParser(
4319+
Settings.builder().put(InferenceCommandConfig.COMPLETION_ENABLED_SETTING.getKey(), false).build()
4320+
);
4321+
ParsingException pe = expectThrows(
4322+
ParsingException.class,
4323+
() -> parserWithDisabledCompletion.createStatement(
4324+
"FROM foo* | COMPLETION targetField = prompt WITH { \"inference_id\": \"test\" }"
4325+
)
4326+
);
4327+
assertThat(pe.getMessage(), containsString("COMPLETION command is disabled"));
4328+
}
4329+
43064330
public void testCompletionWithPositionalParameters() {
43074331
var queryParams = new QueryParams(List.of(paramAsConstant(null, "inferenceId")));
43084332
var plan = as(

0 commit comments

Comments
 (0)