Skip to content

Commit 2667a2d

Browse files
Fix: prevent duplication of "invalid index name" string in the final exception error message (#130027)
* Use `throwInvalidIndexNameException()` to throw invalid ex after dropping asterisk in `IdentifierBuilder#resolveAndValidateIndex()` * Assert the message in test * Refactor * drop invalid chars from assertion string due to randomisation issue * Re-assert invalid chars * Update docs/changelog/130027.yaml
1 parent 78eeecc commit 2667a2d

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

docs/changelog/130027.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 130027
2+
summary: "Fix: prevent duplication of \"invalid index name\" string in the final exception\
3+
\ error message"
4+
area: ES|QL
5+
type: bug
6+
issues: []

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ abstract class IdentifierBuilder extends AbstractBuilder {
3434

3535
private static final String BLANK_INDEX_ERROR_MESSAGE = "Blank index specified in index pattern";
3636

37+
private static final String INVALID_ESQL_CHARS = Strings.INVALID_FILENAME_CHARS.replace("'*',", "");
38+
3739
@Override
3840
public String visitIdentifier(IdentifierContext ctx) {
3941
return ctx == null ? null : unquoteIdentifier(ctx.QUOTED_IDENTIFIER(), ctx.UNQUOTED_IDENTIFIER());
@@ -305,17 +307,16 @@ private static void resolveAndValidateIndex(String index, EsqlBaseParser.IndexPa
305307
return;
306308
}
307309

308-
InvalidIndexNameException errorToThrow = e;
309310
/*
310311
* We only modify this particular message because it mentions '*' as an invalid char.
311312
* However, we do allow asterisk in the index patterns: wildcarded patterns. Let's not
312313
* mislead the user by mentioning this char in the error message.
313314
*/
314315
if (e.getMessage().contains("must not contain the following characters")) {
315-
errorToThrow = new InvalidIndexNameException(index, e.getMessage().replace("'*',", ""));
316+
throwInvalidIndexNameException(index, "must not contain the following characters " + INVALID_ESQL_CHARS, ctx);
316317
}
317318

318-
throw new ParsingException(errorToThrow, source(ctx), errorToThrow.getMessage());
319+
throw new ParsingException(e, source(ctx), e.getMessage());
319320
}
320321
}
321322

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3230,9 +3230,15 @@ public void testInvalidPatternsWithIntermittentQuotes() {
32303230
var randomInvalidChar = randomFrom(invalidChars);
32313231

32323232
// Construct the new invalid index pattern.
3233-
var remoteIndexWithInvalidChar = quote(randomIdentifier() + ":" + "foo" + randomInvalidChar + "bar");
3233+
var invalidIndexName = "foo" + randomInvalidChar + "bar";
3234+
var remoteIndexWithInvalidChar = quote(randomIdentifier() + ":" + invalidIndexName);
32343235
var query = "FROM " + randomIndex + "," + remoteIndexWithInvalidChar;
3235-
expectError(query, "must not contain the following characters [' ','\"',',','/','<','>','?','\\','|']");
3236+
expectError(
3237+
query,
3238+
"Invalid index name ["
3239+
+ invalidIndexName
3240+
+ "], must not contain the following characters [' ','\"',',','/','<','>','?','\\','|']"
3241+
);
32363242
}
32373243

32383244
// Colon outside a quoted string should result in an ANTLR error: a comma is expected.

0 commit comments

Comments
 (0)