Skip to content

Commit 76b22d6

Browse files
authored
Fix StringIndexOutOfBoundsException in COMPLETION command when options are omitted. (elastic#138363) (elastic#138373)
Fix yaml
1 parent eef2276 commit 76b22d6

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
@@ -1029,16 +1029,20 @@ public PlanFactory visitCompletionCommand(EsqlBaseParser.CompletionCommandContex
10291029
}
10301030

10311031
private Completion applyCompletionOptions(Completion completion, EsqlBaseParser.CommandNamedParametersContext ctx) {
1032-
MapExpression optionsExpresion = visitCommandNamedParameters(ctx);
1032+
MapExpression optionsExpression = ctx == null ? null : visitCommandNamedParameters(ctx);
10331033

1034-
if (optionsExpresion == null || optionsExpresion.containsKey(Completion.INFERENCE_ID_OPTION_NAME) == false) {
1034+
if (optionsExpression == null || optionsExpression.containsKey(Completion.INFERENCE_ID_OPTION_NAME) == false) {
10351035
// Having a mandatory named parameter for inference_id is an antipattern, but it will be optional in the future when we have a
10361036
// default LLM. It is better to keep inference_id as a named parameter and relax the syntax when it will become optional than
10371037
// completely change the syntax in the future.
1038-
throw new ParsingException(source(ctx), "Missing mandatory option [{}] in COMPLETION", Completion.INFERENCE_ID_OPTION_NAME);
1038+
throw new ParsingException(
1039+
completion.source(),
1040+
"Missing mandatory option [{}] in COMPLETION",
1041+
Completion.INFERENCE_ID_OPTION_NAME
1042+
);
10391043
}
10401044

1041-
Map<String, Expression> optionsMap = visitCommandNamedParameters(ctx).keyFoldedMap();
1045+
Map<String, Expression> optionsMap = optionsExpression.keyFoldedMap();
10421046

10431047
Expression inferenceId = optionsMap.remove(Completion.INFERENCE_ID_OPTION_NAME);
10441048
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
@@ -4111,13 +4111,13 @@ public void testInvalidRerank() {
41114111
}
41124112

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

41174117
public void testCompletionEmptyOptions() {
41184118
expectError(
41194119
"FROM foo* | COMPLETION targetField = prompt WITH { }",
4120-
"line 1:45: Missing mandatory option [inference_id] in COMPLETION"
4120+
"line 1:13: Missing mandatory option [inference_id] in COMPLETION"
41214121
);
41224122
}
41234123

0 commit comments

Comments
 (0)