Skip to content

Commit 38fa6d6

Browse files
authored
Fix StringIndexOutOfBoundsException in COMPLETION command when options are omitted. (#138363)
Fix yaml
1 parent eb77f8c commit 38fa6d6

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

docs/changelog/138363.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 138363
2+
summary: Fix StringIndexOutOfBoundsException in COMPLETION command when options are omitted.
3+
area: ES|QL
4+
type: bug
5+
issues:
6+
- 138361

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,16 +1157,20 @@ public PlanFactory visitCompletionCommand(EsqlBaseParser.CompletionCommandContex
11571157
}
11581158

11591159
private Completion applyCompletionOptions(Completion completion, EsqlBaseParser.CommandNamedParametersContext ctx) {
1160-
MapExpression optionsExpresion = visitCommandNamedParameters(ctx);
1160+
MapExpression optionsExpression = ctx == null ? null : visitCommandNamedParameters(ctx);
11611161

1162-
if (optionsExpresion == null || optionsExpresion.containsKey(Completion.INFERENCE_ID_OPTION_NAME) == false) {
1162+
if (optionsExpression == null || optionsExpression.containsKey(Completion.INFERENCE_ID_OPTION_NAME) == false) {
11631163
// Having a mandatory named parameter for inference_id is an antipattern, but it will be optional in the future when we have a
11641164
// default LLM. It is better to keep inference_id as a named parameter and relax the syntax when it will become optional than
11651165
// completely change the syntax in the future.
1166-
throw new ParsingException(source(ctx), "Missing mandatory option [{}] in COMPLETION", Completion.INFERENCE_ID_OPTION_NAME);
1166+
throw new ParsingException(
1167+
completion.source(),
1168+
"Missing mandatory option [{}] in COMPLETION",
1169+
Completion.INFERENCE_ID_OPTION_NAME
1170+
);
11671171
}
11681172

1169-
Map<String, Expression> optionsMap = visitCommandNamedParameters(ctx).keyFoldedMap();
1173+
Map<String, Expression> optionsMap = optionsExpression.keyFoldedMap();
11701174

11711175
Expression inferenceId = optionsMap.remove(Completion.INFERENCE_ID_OPTION_NAME);
11721176
if (inferenceId != null) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4233,13 +4233,13 @@ public void testInvalidRerank() {
42334233
}
42344234

42354235
public void testCompletionMissingOptions() {
4236-
expectError("FROM foo* | COMPLETION targetField = prompt", "line 1:44: Missing mandatory option [inference_id] in COMPLETION");
4236+
expectError("FROM foo* | COMPLETION targetField = prompt", "line 1:13: Missing mandatory option [inference_id] in COMPLETION");
42374237
}
42384238

42394239
public void testCompletionEmptyOptions() {
42404240
expectError(
42414241
"FROM foo* | COMPLETION targetField = prompt WITH { }",
4242-
"line 1:45: Missing mandatory option [inference_id] in COMPLETION"
4242+
"line 1:13: Missing mandatory option [inference_id] in COMPLETION"
42434243
);
42444244
}
42454245

0 commit comments

Comments
 (0)