-
Notifications
You must be signed in to change notification settings - Fork 25.6k
ES|QL: Check if cluster aliases and index patterns are valid before executing query #122497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pawankartik-elastic
merged 28 commits into
elastic:main
from
pawankartik-elastic:pkar/index-pattern-check
Jun 25, 2025
Merged
Changes from 15 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
8f324ae
Check if index patterns conform to valid format before validation
pawankartik-elastic 1ddbce4
Update docs/changelog/122497.yaml
pawankartik-elastic 46758cb
Let `validateClusterString()` look for `REMOTE_CLUSTER_INDEX_SEPARATOR`
pawankartik-elastic 2f2ee85
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic 7ca6d70
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic 9dd96df
Mute 3 cases till further clarification and fix error string message
pawankartik-elastic 0686c8f
Fix tests
pawankartik-elastic 7c55d6c
Fix bug in breaking down indices
pawankartik-elastic 2653377
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic 5832ff0
Tiny refactoring around how wildcard is processed and added tests
pawankartik-elastic 6cdbf5a
[CI] Auto commit changes from spotless
c2160bb
Drop duplicated test cases and fix flaky-ness caused by quoting
pawankartik-elastic 544171b
Set cluster string to `null` when it cannot be associated with an index
pawankartik-elastic 133a7c7
Generate correct invalid patterns
pawankartik-elastic 7da29d8
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic 5c70d51
Address review comments and don't break indices into its constituents
pawankartik-elastic 7ad6ac3
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic 3058610
Adhere to the new grammar
pawankartik-elastic 376126e
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic 1a43029
Update docs/changelog/122497.yaml
pawankartik-elastic 8363b70
Update x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/…
pawankartik-elastic 9c16afb
Apply suggestions from review
pawankartik-elastic 8f9f519
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic b6339e7
Apply suggestions from code review
pawankartik-elastic 0cc5580
Apply suggestions from review
pawankartik-elastic 816fcc1
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic 0346762
Do not mention asterisk as an invalid char
pawankartik-elastic 9d275a5
Merge branch 'main' into pkar/index-pattern-check
pawankartik-elastic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 122497 | ||
| summary: Check if index patterns conform to valid format before validation | ||
| area: CCS | ||
| type: bug | ||
| issues: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,16 +15,17 @@ | |
| import org.elasticsearch.core.Tuple; | ||
| import org.elasticsearch.indices.InvalidIndexNameException; | ||
| import org.elasticsearch.transport.RemoteClusterService; | ||
| import org.elasticsearch.xpack.esql.core.util.Holder; | ||
| import org.elasticsearch.xpack.esql.parser.EsqlBaseParser.IdentifierContext; | ||
| import org.elasticsearch.xpack.esql.parser.EsqlBaseParser.IndexStringContext; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import static org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.SelectorResolver.SELECTOR_SEPARATOR; | ||
| import static org.elasticsearch.transport.RemoteClusterAware.REMOTE_CLUSTER_INDEX_SEPARATOR; | ||
| import static org.elasticsearch.transport.RemoteClusterAware.isRemoteIndexName; | ||
| import static org.elasticsearch.transport.RemoteClusterAware.splitIndexName; | ||
| import static org.elasticsearch.xpack.esql.core.util.StringUtils.EXCLUSION; | ||
| import static org.elasticsearch.xpack.esql.core.util.StringUtils.WILDCARD; | ||
| import static org.elasticsearch.xpack.esql.parser.ParserUtils.source; | ||
|
|
@@ -87,33 +88,12 @@ public String visitSelectorString(EsqlBaseParser.SelectorStringContext ctx) { | |
|
|
||
| public String visitIndexPattern(List<EsqlBaseParser.IndexPatternContext> ctx) { | ||
| List<String> patterns = new ArrayList<>(ctx.size()); | ||
| Holder<Boolean> hasSeenStar = new Holder<>(false); | ||
| ctx.forEach(c -> { | ||
| String indexPattern = visitIndexString(c.indexString()); | ||
| String clusterString = visitClusterString(c.clusterString()); | ||
| String selectorString = visitSelectorString(c.selectorString()); | ||
| // skip validating index on remote cluster, because the behavior of remote cluster is not consistent with local cluster | ||
| // For example, invalid#index is an invalid index name, however FROM *:invalid#index does not return an error | ||
| if (clusterString == null) { | ||
| hasSeenStar.set(indexPattern.contains(WILDCARD) || hasSeenStar.get()); | ||
| validateIndexPattern(indexPattern, c, hasSeenStar.get()); | ||
| // Other instances of Elasticsearch may have differing selectors so only validate selector string if remote cluster | ||
| // string is unset | ||
| if (selectorString != null) { | ||
| try { | ||
| // Ensures that the selector provided is one of the valid kinds | ||
| IndexNameExpressionResolver.SelectorResolver.validateIndexSelectorString(indexPattern, selectorString); | ||
| } catch (InvalidIndexNameException e) { | ||
| throw new ParsingException(e, source(c), e.getMessage()); | ||
| } | ||
| } | ||
| } else { | ||
| validateClusterString(clusterString, c); | ||
| // Do not allow selectors on remote cluster expressions until they are supported | ||
| if (selectorString != null) { | ||
| throwOnMixingSelectorWithCluster(reassembleIndexName(clusterString, indexPattern, selectorString), c); | ||
| } | ||
| } | ||
|
|
||
| validateClusterAndIndexPatterns(indexPattern, c, clusterString, selectorString); | ||
pawankartik-elastic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| patterns.add(reassembleIndexName(clusterString, indexPattern, selectorString)); | ||
| }); | ||
| return Strings.collectionToDelimitedString(patterns, ","); | ||
|
|
@@ -148,35 +128,109 @@ protected static void validateClusterString(String clusterString, EsqlBaseParser | |
| } | ||
| } | ||
|
|
||
| private static void validateIndexPattern(String indexPattern, EsqlBaseParser.IndexPatternContext ctx, boolean hasSeenStar) { | ||
| private static void validateClusterAndIndexPatterns( | ||
| String indexPattern, | ||
| EsqlBaseParser.IndexPatternContext ctx, | ||
| String clusterString, | ||
| String selectorString | ||
| ) { | ||
| // multiple index names can be in the same double quote, e.g. indexPattern = "idx1, *, -idx2" | ||
| String[] indices = indexPattern.split(","); | ||
| boolean hasExclusion = false; | ||
| String[] patterns = indexPattern.split(","); | ||
| boolean isFirstPattern = true; | ||
|
|
||
| for (String pattern : patterns) { | ||
pawankartik-elastic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pattern = pattern.strip(); | ||
| String[] indices = new String[] { pattern }; | ||
|
|
||
| /* | ||
| * Just because there was no clusterString before this index pattern does not mean that the indices | ||
| * are local indices. Patterns can be clubbed with remote names within quotes such as: | ||
| * "remote_one:remote_index,local_index". In this case, clusterString will be null. | ||
|
||
| */ | ||
| if (isRemoteIndexName(pattern)) { | ||
| /* | ||
| * Handle scenarios like remote_one:"index1,remote_two:index2". The clusterString here is | ||
| * remote_one and is associated with index1 and not index2. | ||
pawankartik-elastic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| if (clusterString != null && isFirstPattern) { | ||
| throw new ParsingException( | ||
| source(ctx), | ||
| "Index pattern [{}] contains a cluster alias despite specifying one [{}]", | ||
pawankartik-elastic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pattern, | ||
| clusterString | ||
| ); | ||
| } | ||
|
|
||
| // {cluster_alias, indexName} | ||
| String[] clusterAliasAndIndex = splitIndexName(pattern); | ||
pawankartik-elastic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| clusterString = clusterAliasAndIndex[0]; | ||
| indices[0] = clusterAliasAndIndex[1]; | ||
|
|
||
| /* | ||
| * What if the pattern is index1|index2? We cannot split at the pipe char blindly as that'd mess with | ||
pawankartik-elastic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * logstash-like examples. We only split this way if an index is associated with a remote cluster. | ||
| */ | ||
| indices = Arrays.stream(indices) | ||
| .map(IdentifierBuilder::breakPatternIntoIndices) | ||
| .flatMap(Arrays::stream) | ||
| .toArray(String[]::new); | ||
| } else if (clusterString != null) { | ||
| // This is not a remote index pattern and the cluster string preceding this quoted pattern | ||
| // cannot be associated with it. | ||
| if (isFirstPattern == false) { | ||
| clusterString = null; | ||
| } | ||
|
|
||
| // Cluster alias was prefixed to the pattern and did not occur within the pattern. | ||
| indices = Arrays.stream(indices) | ||
| .map(IdentifierBuilder::breakPatternIntoIndices) | ||
| .flatMap(Arrays::stream) | ||
| .toArray(String[]::new); | ||
| } | ||
|
|
||
| if (clusterString != null) { | ||
| if (selectorString != null) { | ||
pawankartik-elastic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| throwOnMixingSelectorWithCluster(reassembleIndexName(clusterString, indexPattern, selectorString), ctx); | ||
| } | ||
| validateClusterString(clusterString, ctx); | ||
pawankartik-elastic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| validateIndicesForCluster(clusterString, indices, ctx); | ||
| if (selectorString != null) { | ||
| try { | ||
| // Ensures that the selector provided is one of the valid kinds | ||
| IndexNameExpressionResolver.SelectorResolver.validateIndexSelectorString(indexPattern, selectorString); | ||
| } catch (InvalidIndexNameException e) { | ||
| throw new ParsingException(e, source(ctx), e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| isFirstPattern = false; | ||
| } | ||
| } | ||
|
|
||
| private static void validateIndicesForCluster(String clusterString, String[] indices, EsqlBaseParser.IndexPatternContext ctx) { | ||
| for (String index : indices) { | ||
| // Strip spaces off first because validation checks are not written to handle them | ||
| index = index.strip(); | ||
| if (isRemoteIndexName(index)) { // skip the validation if there is remote cluster | ||
| // Ensure that there are no selectors as they are not yet supported | ||
| if (index.contains(SELECTOR_SEPARATOR)) { | ||
| throwOnMixingSelectorWithCluster(index, ctx); | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| Tuple<String, String> splitPattern = IndexNameExpressionResolver.splitSelectorExpression(index); | ||
| if (splitPattern.v2() != null) { | ||
| index = splitPattern.v1(); | ||
| if (splitPattern.v2() != null && clusterString != null) { | ||
| throwOnMixingSelectorWithCluster(reassembleIndexName(clusterString, splitPattern.v1(), splitPattern.v2()), ctx); | ||
| } | ||
|
|
||
| index = splitPattern.v1(); | ||
| } catch (InvalidIndexNameException e) { | ||
| // throws exception if the selector expression is invalid. Selector resolution does not complain about exclusions | ||
| throw new ParsingException(e, source(ctx), e.getMessage()); | ||
| } | ||
| hasSeenStar = index.contains(WILDCARD) || hasSeenStar; | ||
pawankartik-elastic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var hasSeenStar = index.contains(WILDCARD); | ||
| index = index.replace(WILDCARD, "").strip(); | ||
| if (index.isBlank()) { | ||
| continue; | ||
| } | ||
| hasExclusion = index.startsWith(EXCLUSION); | ||
| var hasExclusion = index.startsWith(EXCLUSION); | ||
| index = removeExclusion(index); | ||
| String tempName; | ||
| try { | ||
|
|
@@ -198,9 +252,44 @@ private static void validateIndexPattern(String indexPattern, EsqlBaseParser.Ind | |
| } | ||
| throw new ParsingException(e, source(ctx), e.getMessage()); | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| private static String[] breakPatternIntoIndices(String pattern) { | ||
pawankartik-elastic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (pattern.codePoints().anyMatch(ch -> ch == ',')) { | ||
| throw new IllegalArgumentException("Found grouped index patterns, expecting a single pattern"); | ||
pawankartik-elastic marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Fast path: if there's no pipe char, no point in attempting to break down the string. | ||
| if (pattern.contains("|") == false) { | ||
| return new String[] { pattern }; | ||
| } | ||
|
|
||
| var indices = new ArrayList<String>(); | ||
| var sb = new StringBuilder(); | ||
| var inDateMathExpr = false; | ||
| for (int i = 0; i < pattern.length(); i++) { | ||
| char c = pattern.charAt(i); | ||
| sb.append(c); | ||
| if (c == '<') { | ||
| inDateMathExpr = true; | ||
| } else if (c == '>') { | ||
| inDateMathExpr = false; | ||
| } else if (c == '|' && inDateMathExpr == false) { | ||
| sb.deleteCharAt(sb.length() - 1); | ||
| indices.add(sb.toString()); | ||
| sb.setLength(0); | ||
| } | ||
| } | ||
|
|
||
| if (sb.isEmpty() == false) { | ||
| indices.add(sb.toString()); | ||
| } | ||
|
|
||
| return indices.toArray(new String[0]); | ||
| } | ||
|
|
||
| private static String removeExclusion(String indexPattern) { | ||
| return indexPattern.charAt(0) == EXCLUSION.charAt(0) ? indexPattern.substring(1) : indexPattern; | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This currently permits white spaces between separators and index name, like
FROM "remote : idx"andFROM idx :: failures; it fails with less helpful error messages later down the line.I think this is related to #129768, but not exactly the same. To avoid scope creep, we can tackle this in a follow-up or make it part of the other issue. I added a comment #129768 (comment) so we don't forget.