diff --git a/docs/changelog/130027.yaml b/docs/changelog/130027.yaml new file mode 100644 index 0000000000000..e00ce036ae082 --- /dev/null +++ b/docs/changelog/130027.yaml @@ -0,0 +1,6 @@ +pr: 130027 +summary: "Fix: prevent duplication of \"invalid index name\" string in the final exception\ + \ error message" +area: ES|QL +type: bug +issues: [] diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/IdentifierBuilder.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/IdentifierBuilder.java index 0a07e06c4c1df..9268dd08bc7e7 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/IdentifierBuilder.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/IdentifierBuilder.java @@ -34,6 +34,8 @@ abstract class IdentifierBuilder extends AbstractBuilder { private static final String BLANK_INDEX_ERROR_MESSAGE = "Blank index specified in index pattern"; + private static final String INVALID_ESQL_CHARS = Strings.INVALID_FILENAME_CHARS.replace("'*',", ""); + @Override public String visitIdentifier(IdentifierContext ctx) { return ctx == null ? null : unquoteIdentifier(ctx.QUOTED_IDENTIFIER(), ctx.UNQUOTED_IDENTIFIER()); @@ -305,17 +307,16 @@ private static void resolveAndValidateIndex(String index, EsqlBaseParser.IndexPa return; } - InvalidIndexNameException errorToThrow = e; /* * We only modify this particular message because it mentions '*' as an invalid char. * However, we do allow asterisk in the index patterns: wildcarded patterns. Let's not * mislead the user by mentioning this char in the error message. */ if (e.getMessage().contains("must not contain the following characters")) { - errorToThrow = new InvalidIndexNameException(index, e.getMessage().replace("'*',", "")); + throwInvalidIndexNameException(index, "must not contain the following characters " + INVALID_ESQL_CHARS, ctx); } - throw new ParsingException(errorToThrow, source(ctx), errorToThrow.getMessage()); + throw new ParsingException(e, source(ctx), e.getMessage()); } } diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java index 108059ac2c883..d414f922f8c7d 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/parser/StatementParserTests.java @@ -3230,9 +3230,15 @@ public void testInvalidPatternsWithIntermittentQuotes() { var randomInvalidChar = randomFrom(invalidChars); // Construct the new invalid index pattern. - var remoteIndexWithInvalidChar = quote(randomIdentifier() + ":" + "foo" + randomInvalidChar + "bar"); + var invalidIndexName = "foo" + randomInvalidChar + "bar"; + var remoteIndexWithInvalidChar = quote(randomIdentifier() + ":" + invalidIndexName); var query = "FROM " + randomIndex + "," + remoteIndexWithInvalidChar; - expectError(query, "must not contain the following characters [' ','\"',',','/','<','>','?','\\','|']"); + expectError( + query, + "Invalid index name [" + + invalidIndexName + + "], must not contain the following characters [' ','\"',',','/','<','>','?','\\','|']" + ); } // Colon outside a quoted string should result in an ANTLR error: a comma is expected.