-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Ensure cluster string could be quoted #120355
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
Changes from 7 commits
d4d26c5
1989c3d
a944a60
29187af
c184382
d58db78
7b906c8
0de6f2b
2570e8d
36c4dae
f5df4a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 120355 | ||
| summary: Ensure cluster string could be quoted | ||
| area: ES|QL | ||
| type: enhancement | ||
| issues: [] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -143,6 +143,7 @@ indexPattern | |
|
|
||
| clusterString | ||
| : UNQUOTED_SOURCE | ||
| | QUOTED_STRING | ||
| ; | ||
|
|
||
| indexString | ||
|
|
||
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| import org.elasticsearch.cluster.metadata.MetadataCreateIndexService; | ||
| import org.elasticsearch.common.Strings; | ||
| 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; | ||
|
|
@@ -51,29 +52,51 @@ protected static String quoteIdString(String unquotedString) { | |
| return "`" + unquotedString.replace("`", "``") + "`"; | ||
| } | ||
|
|
||
| @Override | ||
| public String visitClusterString(EsqlBaseParser.ClusterStringContext ctx) { | ||
| if (ctx == null) { | ||
| return null; | ||
| } else if (ctx.UNQUOTED_SOURCE() != null) { | ||
| return ctx.UNQUOTED_SOURCE().getText(); | ||
| } else { | ||
| return unquote(ctx.QUOTED_STRING().getText()); | ||
| } | ||
|
Comment on lines
+57
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment in the grammar - this method can then be either remove or delegate to visitIndexString. |
||
| } | ||
|
|
||
| @Override | ||
| public String visitIndexString(IndexStringContext ctx) { | ||
| TerminalNode n = ctx.UNQUOTED_SOURCE(); | ||
| return n != null ? n.getText() : unquote(ctx.QUOTED_STRING().getText()); | ||
| if (ctx.UNQUOTED_SOURCE() != null) { | ||
| return ctx.UNQUOTED_SOURCE().getText(); | ||
| } else { | ||
| return unquote(ctx.QUOTED_STRING().getText()); | ||
| } | ||
| } | ||
|
|
||
| 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 = c.clusterString() != null ? c.clusterString().getText() : null; | ||
| String clusterString = visitClusterString(c.clusterString()); | ||
| // 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()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, that's slightly out of scope but I just realized that the validation for the index pattern is lacking. For instance, you can use What is a bit more in scope: at least when the cluster string is not null, we should probably validate that the index pattern is not a remote pattern. This applies to cases like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just tried locally, with your branch: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @idegtiarenko if you feel like it, I think we could add some validation for this as we're just touching this anyway. Otherwise, let's put what we found into an issue because that'll need to be fixed one day, anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed this a bit with @quux00 and @idegtiarenko . This problem pre-existed:
More generally, CCQ validation has larger scope as it also needs to take into account licensing and security. Thus, this needs a follow-up that does this properly. I opened this issue: #121901 |
||
| } else { | ||
| validateClusterString(clusterString, c); | ||
| } | ||
| patterns.add(clusterString != null ? clusterString + REMOTE_CLUSTER_INDEX_SEPARATOR + indexPattern : indexPattern); | ||
| }); | ||
| return Strings.collectionToDelimitedString(patterns, ","); | ||
| } | ||
|
|
||
| protected static void validateClusterString(String clusterString, EsqlBaseParser.IndexPatternContext ctx) { | ||
| if (clusterString.indexOf(RemoteClusterService.REMOTE_CLUSTER_INDEX_SEPARATOR) != -1) { | ||
| throw new ParsingException(source(ctx), "cluster string [{}] must not contain ':'", clusterString); | ||
| } | ||
| } | ||
|
|
||
| private static void validateIndexPattern(String indexPattern, EsqlBaseParser.IndexPatternContext ctx, boolean hasSeenStar) { | ||
| // multiple index names can be in the same double quote, e.g. indexPattern = "idx1, *, -idx2" | ||
| String[] indices = indexPattern.split(","); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -300,18 +300,18 @@ public void testStatsWithoutGroups() { | |
| ); | ||
| } | ||
|
|
||
| public void testStatsWithoutAggs() throws Exception { | ||
| public void testStatsWithoutAggs() { | ||
| assertEquals( | ||
| new Aggregate(EMPTY, PROCESSING_CMD_INPUT, Aggregate.AggregateType.STANDARD, List.of(attribute("a")), List.of(attribute("a"))), | ||
| processingCommand("stats by a") | ||
| ); | ||
| } | ||
|
|
||
| public void testStatsWithoutAggsOrGroup() throws Exception { | ||
| public void testStatsWithoutAggsOrGroup() { | ||
| expectError("from text | stats", "At least one aggregation or grouping expression required in [stats]"); | ||
| } | ||
|
|
||
| public void testAggsWithGroupKeyAsAgg() throws Exception { | ||
| public void testAggsWithGroupKeyAsAgg() { | ||
| var queries = new String[] { """ | ||
| row a = 1, b = 2 | ||
| | stats a by a | ||
|
|
@@ -332,7 +332,7 @@ public void testAggsWithGroupKeyAsAgg() throws Exception { | |
| } | ||
| } | ||
|
|
||
| public void testStatsWithGroupKeyAndAggFilter() throws Exception { | ||
| public void testStatsWithGroupKeyAndAggFilter() { | ||
| var a = attribute("a"); | ||
| var f = new UnresolvedFunction(EMPTY, "min", DEFAULT, List.of(a)); | ||
| var filter = new Alias(EMPTY, "min(a) where a > 1", new FilteredExpression(EMPTY, f, new GreaterThan(EMPTY, a, integer(1)))); | ||
|
|
@@ -342,7 +342,7 @@ public void testStatsWithGroupKeyAndAggFilter() throws Exception { | |
| ); | ||
| } | ||
|
|
||
| public void testStatsWithGroupKeyAndMixedAggAndFilter() throws Exception { | ||
| public void testStatsWithGroupKeyAndMixedAggAndFilter() { | ||
| var a = attribute("a"); | ||
| var min = new UnresolvedFunction(EMPTY, "min", DEFAULT, List.of(a)); | ||
| var max = new UnresolvedFunction(EMPTY, "max", DEFAULT, List.of(a)); | ||
|
|
@@ -377,7 +377,7 @@ public void testStatsWithGroupKeyAndMixedAggAndFilter() throws Exception { | |
| ); | ||
| } | ||
|
|
||
| public void testStatsWithoutGroupKeyMixedAggAndFilter() throws Exception { | ||
| public void testStatsWithoutGroupKeyMixedAggAndFilter() { | ||
| var a = attribute("a"); | ||
| var f = new UnresolvedFunction(EMPTY, "min", DEFAULT, List.of(a)); | ||
| var filter = new Alias(EMPTY, "min(a) where a > 1", new FilteredExpression(EMPTY, f, new GreaterThan(EMPTY, a, integer(1)))); | ||
|
|
@@ -615,6 +615,60 @@ private void clustersAndIndices(String command, String indexString1, String inde | |
| ); | ||
| } | ||
|
|
||
| public void testValidFromIndexPattern() { | ||
|
||
| var patterns = randomList(1, 5, () -> { | ||
| String pattern = randomIndexIdentifier();// index or alias | ||
| if (randomBoolean()) {// pattern | ||
| pattern += "*"; | ||
| } | ||
| if (randomBoolean()) {// quoted | ||
| pattern = "\"" + pattern + "\""; | ||
| } | ||
| if (randomBoolean()) {// remote cluster | ||
| var cluster = randomIdentifier(); | ||
| if (randomBoolean()) {// quoted | ||
| cluster = "\"" + cluster + "\""; | ||
| } | ||
| pattern = cluster + ":" + pattern; | ||
| } | ||
| if (pattern.contains(":") && pattern.contains("\"") == false) {// quote entire "cluster:index" | ||
| pattern = "\"" + pattern + "\""; | ||
| } | ||
| return pattern; | ||
| }); | ||
|
|
||
| var plan = statement("FROM " + String.join(",", patterns)); | ||
| var expected = String.join(",", patterns).replace("\"", ""); | ||
|
|
||
| assertThat(plan, instanceOf(UnresolvedRelation.class)); | ||
| assertThat(((UnresolvedRelation) plan).table().index(), equalTo(expected)); | ||
| } | ||
|
|
||
| private static String randomIndexIdentifier() { | ||
| // https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html#indices-create-api-path-params | ||
| var validFirstCharacters = "abcdefghijklmnopqrstuvwxyz0123456789!'$^&"; | ||
| var validCharacters = validFirstCharacters + "+-_."; | ||
|
|
||
| var index = new StringBuilder(); | ||
| if (randomInt(9) == 0) {// hidden index | ||
| index.append('.'); | ||
| } | ||
| index.append(randomCharacterFrom(validFirstCharacters)); | ||
| for (int i = 0; i < randomIntBetween(1, 100); i++) { | ||
| index.append(randomCharacterFrom(validCharacters)); | ||
| } | ||
| return index.toString(); | ||
| } | ||
|
|
||
| private static char randomCharacterFrom(String str) { | ||
| return str.charAt(randomInt(str.length() - 1)); | ||
| } | ||
|
|
||
| public void testInvalidFromIndexPattern() { | ||
|
||
| expectError("FROM \"remote:\":index", "line 1:6: cluster string [remote:] must not contain ':'"); | ||
| expectError("FROM \"remote:invalid\":index", "line 1:6: cluster string [remote:invalid] must not contain ':'"); | ||
| } | ||
|
|
||
| public void testInvalidQuotingAsFromIndexPattern() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should also add tests for invalid quoting of the remote name itself for good measure. |
||
| expectError("FROM \"foo", ": token recognition error at: '\"foo'"); | ||
| expectError("FROM \"foo | LIMIT 1", ": token recognition error at: '\"foo | LIMIT 1'"); | ||
|
|
@@ -2060,41 +2114,41 @@ private void assertStringAsLookupIndexPattern(String string, String statement) { | |
| assertThat(tableName.fold(FoldContext.small()), equalTo(string)); | ||
| } | ||
|
|
||
| public void testIdPatternUnquoted() throws Exception { | ||
| public void testIdPatternUnquoted() { | ||
| var string = "regularString"; | ||
| assertThat(breakIntoFragments(string), contains(string)); | ||
| } | ||
|
|
||
| public void testIdPatternQuoted() throws Exception { | ||
| public void testIdPatternQuoted() { | ||
| var string = "`escaped string`"; | ||
| assertThat(breakIntoFragments(string), contains(string)); | ||
| } | ||
|
|
||
| public void testIdPatternQuotedWithDoubleBackticks() throws Exception { | ||
| public void testIdPatternQuotedWithDoubleBackticks() { | ||
| var string = "`escaped``string`"; | ||
| assertThat(breakIntoFragments(string), contains(string)); | ||
| } | ||
|
|
||
| public void testIdPatternUnquotedAndQuoted() throws Exception { | ||
| public void testIdPatternUnquotedAndQuoted() { | ||
| var string = "this`is`a`mix`of`ids`"; | ||
| assertThat(breakIntoFragments(string), contains("this", "`is`", "a", "`mix`", "of", "`ids`")); | ||
| } | ||
|
|
||
| public void testIdPatternQuotedTraling() throws Exception { | ||
| public void testIdPatternQuotedTrailing() { | ||
| var string = "`foo`*"; | ||
| assertThat(breakIntoFragments(string), contains("`foo`", "*")); | ||
| } | ||
|
|
||
| public void testIdPatternWithDoubleQuotedStrings() throws Exception { | ||
| public void testIdPatternWithDoubleQuotedStrings() { | ||
| var string = "`this``is`a`quoted `` string``with`backticks"; | ||
| assertThat(breakIntoFragments(string), contains("`this``is`", "a", "`quoted `` string``with`", "backticks")); | ||
| } | ||
|
|
||
| public void testSpaceNotAllowedInIdPattern() throws Exception { | ||
| public void testSpaceNotAllowedInIdPattern() { | ||
| expectError("ROW a = 1| RENAME a AS this is `not okay`", "mismatched input 'is' expecting {<EOF>, '|', ',', '.'}"); | ||
| } | ||
|
|
||
| public void testSpaceNotAllowedInIdPatternKeep() throws Exception { | ||
| public void testSpaceNotAllowedInIdPatternKeep() { | ||
| expectError("ROW a = 1, b = 1| KEEP a b", "extraneous input 'b'"); | ||
| } | ||
|
|
||
|
|
||
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.
Since they are the same, point clusterString to indexString:
We could fully remove it but it's worth keeping the element in for future changes.
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.
I would like to avoid that.
clusterStringshould not be equivalent toindexString.For example
clusterStringshould not allow:and::from upcoming selector changes.I imagine some day we might need to reflect that in grammar.