|
8 | 8 | package org.elasticsearch.xpack.esql.parser; |
9 | 9 |
|
10 | 10 | import org.antlr.v4.runtime.tree.TerminalNode; |
| 11 | +import org.elasticsearch.ElasticsearchParseException; |
| 12 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
| 13 | +import org.elasticsearch.cluster.metadata.MetadataCreateIndexService; |
11 | 14 | import org.elasticsearch.common.Strings; |
| 15 | +import org.elasticsearch.indices.InvalidIndexNameException; |
| 16 | +import org.elasticsearch.xpack.esql.core.util.Holder; |
12 | 17 | import org.elasticsearch.xpack.esql.parser.EsqlBaseParser.IdentifierContext; |
13 | 18 | import org.elasticsearch.xpack.esql.parser.EsqlBaseParser.IndexStringContext; |
14 | 19 |
|
15 | 20 | import java.util.ArrayList; |
16 | 21 | import java.util.List; |
17 | 22 |
|
18 | 23 | import static org.elasticsearch.transport.RemoteClusterAware.REMOTE_CLUSTER_INDEX_SEPARATOR; |
| 24 | +import static org.elasticsearch.xpack.esql.core.util.StringUtils.EXCLUSION; |
| 25 | +import static org.elasticsearch.xpack.esql.core.util.StringUtils.WILDCARD; |
| 26 | +import static org.elasticsearch.xpack.esql.parser.ParserUtils.source; |
19 | 27 |
|
20 | 28 | abstract class IdentifierBuilder extends AbstractBuilder { |
21 | 29 |
|
@@ -46,12 +54,54 @@ public String visitIndexString(IndexStringContext ctx) { |
46 | 54 |
|
47 | 55 | public String visitIndexPattern(List<EsqlBaseParser.IndexPatternContext> ctx) { |
48 | 56 | List<String> patterns = new ArrayList<>(ctx.size()); |
| 57 | + Holder<Boolean> hasSeenStar = new Holder<>(false); |
49 | 58 | ctx.forEach(c -> { |
50 | 59 | String indexPattern = visitIndexString(c.indexString()); |
| 60 | + hasSeenStar.set(indexPattern.contains(WILDCARD) || hasSeenStar.get()); |
| 61 | + validateIndexPattern(indexPattern, c, hasSeenStar.get()); |
51 | 62 | patterns.add( |
52 | 63 | c.clusterString() != null ? c.clusterString().getText() + REMOTE_CLUSTER_INDEX_SEPARATOR + indexPattern : indexPattern |
53 | 64 | ); |
54 | 65 | }); |
55 | 66 | return Strings.collectionToDelimitedString(patterns, ","); |
56 | 67 | } |
| 68 | + |
| 69 | + private static void validateIndexPattern(String indexPattern, EsqlBaseParser.IndexPatternContext ctx, boolean hasSeenStar) { |
| 70 | + // multiple index names can be in the same double quote, e.g. indexPattern = "idx1, *, -idx2" |
| 71 | + String[] indices = indexPattern.split(","); |
| 72 | + boolean hasExclusion = false; |
| 73 | + for (String index : indices) { |
| 74 | + hasSeenStar = index.contains(WILDCARD) || hasSeenStar; |
| 75 | + index = index.replace(WILDCARD, "").strip(); |
| 76 | + if (index.isBlank()) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + hasExclusion = index.startsWith(EXCLUSION); |
| 80 | + index = removeExclusion(index); |
| 81 | + String tempName; |
| 82 | + try { |
| 83 | + // remove the exclusion outside of <>, from index names with DateMath expression, |
| 84 | + // e.g. -<-logstash-{now/d}> becomes <-logstash-{now/d}> before calling resolveDateMathExpression |
| 85 | + tempName = IndexNameExpressionResolver.resolveDateMathExpression(index); |
| 86 | + } catch (ElasticsearchParseException e) { |
| 87 | + // throws exception if the DateMath expression is invalid, resolveDateMathExpression does not complain about exclusions |
| 88 | + throw new ParsingException(e, source(ctx), e.getMessage()); |
| 89 | + } |
| 90 | + hasExclusion = tempName.startsWith(EXCLUSION) || hasExclusion; |
| 91 | + index = tempName.equals(index) ? index : removeExclusion(tempName); |
| 92 | + try { |
| 93 | + MetadataCreateIndexService.validateIndexOrAliasName(index, InvalidIndexNameException::new); |
| 94 | + } catch (InvalidIndexNameException e) { |
| 95 | + // ignore invalid index name if it has exclusions and there is an index with wildcard before it |
| 96 | + if (hasSeenStar && hasExclusion) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + throw new ParsingException(e, source(ctx), e.getMessage()); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private static String removeExclusion(String indexPattern) { |
| 105 | + return indexPattern.charAt(0) == EXCLUSION.charAt(0) ? indexPattern.substring(1) : indexPattern; |
| 106 | + } |
57 | 107 | } |
0 commit comments