Skip to content

Support index pattern selector syntax in SQL #120845

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
merged 16 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public void testBasicIndexWithCatalog() {
assertThat(result.indices.get(0).id().index(), is("index"));
}

public void testBasicIndexWithSelector() {
LogicalPlan plan = new UnresolvedRelation(EMPTY, new TableIdentifier(EMPTY, null, "index::failures"), null, false);
PreAnalysis result = preAnalyzer.preAnalyze(plan);
assertThat(plan.preAnalyzed(), is(true));
assertThat(result.indices, hasSize(1));
assertThat(result.indices.get(0).id().cluster(), nullValue());
assertThat(result.indices.get(0).id().index(), is("index::failures"));
}

public void testComplicatedQuery() {
LogicalPlan plan = new Limit(
EMPTY,
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugin/sql/src/main/antlr/SqlBase.g4
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ identifier
;

tableIdentifier
: (catalog=identifier ':')? TABLE_IDENTIFIER
| (catalog=identifier ':')? name=identifier
: (catalog=identifier ':')? TABLE_IDENTIFIER ('::' selector=identifier)?
| (catalog=identifier ':')? name=identifier ('::' selector=identifier)?
;

quoteIdentifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
package org.elasticsearch.xpack.sql.parser;

import org.antlr.v4.runtime.tree.ParseTree;
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.common.Strings;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.xpack.ql.plan.TableIdentifier;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.sql.parser.SqlBaseParser.IdentifierContext;
Expand All @@ -29,7 +31,56 @@ public TableIdentifier visitTableIdentifier(TableIdentifierContext ctx) {
ParseTree tree = ctx.name != null ? ctx.name : ctx.TABLE_IDENTIFIER();
String index = tree.getText();

return new TableIdentifier(source, visitIdentifier(ctx.catalog), unquoteIdentifier(index));
String cluster = visitIdentifier(ctx.catalog);
String indexName = unquoteIdentifier(index);
String selector = visitIdentifier(ctx.selector);

if (cluster != null && selector != null) {
throw new ParsingException(
source,
"Invalid index name [{}:{}::{}], Selectors are not yet supported on remote cluster patterns",
cluster,
indexName,
selector
);
}

if (selector != null) {
try {
IndexNameExpressionResolver.SelectorResolver.validateIndexSelectorString(indexName, selector);
} catch (Exception e) {
throw new ParsingException(source, e.getMessage());
}
}

if (indexName.contains(IndexNameExpressionResolver.SelectorResolver.SELECTOR_SEPARATOR)) {
if (selector != null) {
throw new ParsingException(
source,
"Invalid index name [{}::{}], Invalid usage of :: separator, only one :: separator is allowed per expression",
indexName,
selector
);
}
try {
Tuple<String, String> split = IndexNameExpressionResolver.splitSelectorExpression(indexName);
indexName = split.v1();
selector = split.v2();
} catch (Exception e) {
throw new ParsingException(source, e.getMessage());
}
if (selector != null) {
try {
IndexNameExpressionResolver.SelectorResolver.validateIndexSelectorString(indexName, selector);
} catch (Exception e) {
throw new ParsingException(source, "Invalid index name [{}::{}], {}", indexName, selector, e.getMessage());
}
}
}

indexName = IndexNameExpressionResolver.combineSelectorExpression(indexName, selector);

return new TableIdentifier(source, cluster, indexName);
}

@Override
Expand Down
Loading