Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/138363.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 138363
summary: Fix StringIndexOutOfBoundsException in COMPLETION command when options are omitted.
area: ES|QL
type: bug
issues:
- 138361
Original file line number Diff line number Diff line change
Expand Up @@ -1157,16 +1157,20 @@ public PlanFactory visitCompletionCommand(EsqlBaseParser.CompletionCommandContex
}

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

if (optionsExpresion == null || optionsExpresion.containsKey(Completion.INFERENCE_ID_OPTION_NAME) == false) {
if (optionsExpression == null || optionsExpression.containsKey(Completion.INFERENCE_ID_OPTION_NAME) == false) {
// Having a mandatory named parameter for inference_id is an antipattern, but it will be optional in the future when we have a
// default LLM. It is better to keep inference_id as a named parameter and relax the syntax when it will become optional than
// completely change the syntax in the future.
throw new ParsingException(source(ctx), "Missing mandatory option [{}] in COMPLETION", Completion.INFERENCE_ID_OPTION_NAME);
throw new ParsingException(
completion.source(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Using completion.source instead of source(ctx). Avoid StringIndexOutOfBoundsException that were cause when options were missing (ctx is null)

"Missing mandatory option [{}] in COMPLETION",
Completion.INFERENCE_ID_OPTION_NAME
);
}

Map<String, Expression> optionsMap = visitCommandNamedParameters(ctx).keyFoldedMap();
Map<String, Expression> optionsMap = optionsExpression.keyFoldedMap();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ For whatever reason (abuse of pizzas?) I was not reusing the optionExpression while it was the right things to do.


Expression inferenceId = optionsMap.remove(Completion.INFERENCE_ID_OPTION_NAME);
if (inferenceId != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4233,13 +4233,13 @@ public void testInvalidRerank() {
}

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

public void testCompletionEmptyOptions() {
expectError(
"FROM foo* | COMPLETION targetField = prompt WITH { }",
"line 1:45: Missing mandatory option [inference_id] in COMPLETION"
"line 1:13: Missing mandatory option [inference_id] in COMPLETION"
);
}

Expand Down