Skip to content

Commit 8f324ae

Browse files
Check if index patterns conform to valid format before validation
1 parent 0c9c910 commit 8f324ae

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ public String visitIndexPattern(List<EsqlBaseParser.IndexPatternContext> ctx) {
7878
ctx.forEach(c -> {
7979
String indexPattern = visitIndexString(c.indexString());
8080
String clusterString = visitClusterString(c.clusterString());
81+
String assembledName = clusterString != null ? clusterString + REMOTE_CLUSTER_INDEX_SEPARATOR + indexPattern : indexPattern;
82+
// At this point, we run 2 kinds of checks. The first check is to ensure that this string adheres to the valid
83+
// pattern format. The second check is to ensure that the parts of this string, i.e. the remote name, cluster name, and
84+
// other aspects of them are valid. There's no point in running the second check if the first one fails.
85+
if (assembledName.codePoints().filter(ch -> ch == REMOTE_CLUSTER_INDEX_SEPARATOR).count() > 1) {
86+
throw new ParsingException(source(c), "More than 1 occurrence of index separator found in index pattern");
87+
}
8188
// skip validating index on remote cluster, because the behavior of remote cluster is not consistent with local cluster
8289
// For example, invalid#index is an invalid index name, however FROM *:invalid#index does not return an error
8390
if (clusterString == null) {
@@ -86,7 +93,7 @@ public String visitIndexPattern(List<EsqlBaseParser.IndexPatternContext> ctx) {
8693
} else {
8794
validateClusterString(clusterString, c);
8895
}
89-
patterns.add(clusterString != null ? clusterString + REMOTE_CLUSTER_INDEX_SEPARATOR + indexPattern : indexPattern);
96+
patterns.add(assembledName);
9097
});
9198
return Strings.collectionToDelimitedString(patterns, ",");
9299
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,8 @@ public void testInvalidQuotingAsFromIndexPattern() {
644644

645645
expectError("FROM \"\"\"foo\"\"\"bar\"\"\"", ": mismatched input 'bar' expecting {<EOF>, '|', ',', 'metadata'}");
646646
expectError("FROM \"\"\"foo\"\"\"\"\"\"bar\"\"\"", ": mismatched input '\"bar\"' expecting {<EOF>, '|', ',', 'metadata'}");
647+
expectError("FROM remote:\"foo:bar\"", "More than 1 occurrence of index separator found in index pattern");
648+
expectError("FROM \"remote:foo:bar:baz\"", "More than 1 occurrence of index separator found in index pattern");
647649
}
648650

649651
public void testInvalidQuotingAsMetricsIndexPattern() {

0 commit comments

Comments
 (0)