Skip to content
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

Support index pattern selector syntax in SQL #120845

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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 @@ -753,7 +753,7 @@ private static Attribute tiebreaker() {
}

private static LogicalPlan rel() {
return new UnresolvedRelation(EMPTY, new TableIdentifier(EMPTY, "catalog", "index"), "", false);
return new UnresolvedRelation(EMPTY, new TableIdentifier(EMPTY, "catalog", "index", "data"), "", false);
}

private static KeyedFilter keyedFilter(LogicalPlan child) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.Objects;

import static org.elasticsearch.cluster.metadata.IndexNameExpressionResolver.SelectorResolver.SELECTOR_SEPARATOR;
import static org.elasticsearch.transport.RemoteClusterAware.REMOTE_CLUSTER_INDEX_SEPARATOR;

public class TableIdentifier {
Expand All @@ -18,11 +19,13 @@ public class TableIdentifier {

private final String cluster;
private final String index;
private final String selector;

public TableIdentifier(Source source, String catalog, String index) {
public TableIdentifier(Source source, String catalog, String index, String selector) {
this.source = source;
this.cluster = catalog;
this.index = index;
this.selector = selector;
}

public String cluster() {
Expand All @@ -33,9 +36,13 @@ public String index() {
return index;
}

public String selector() {
return selector;
}

@Override
public int hashCode() {
return Objects.hash(cluster, index);
return Objects.hash(cluster, index, selector);
}

@Override
Expand All @@ -49,25 +56,32 @@ public boolean equals(Object obj) {
}

TableIdentifier other = (TableIdentifier) obj;
return Objects.equals(index, other.index) && Objects.equals(cluster, other.cluster);
return Objects.equals(index, other.index) && Objects.equals(cluster, other.cluster) && Objects.equals(selector, other.selector);
}

public Source source() {
return source;
}

public String qualifiedIndex() {
return cluster != null ? cluster + REMOTE_CLUSTER_INDEX_SEPARATOR + index : index;
if (cluster == null && selector == null) {
return index;
}
StringBuilder qualifiedIndex = new StringBuilder();
if (cluster != null) {
qualifiedIndex.append(cluster);
qualifiedIndex.append(REMOTE_CLUSTER_INDEX_SEPARATOR);
}
qualifiedIndex.append(index);
if (selector != null) {
qualifiedIndex.append(SELECTOR_SEPARATOR);
qualifiedIndex.append(selector);
}
return qualifiedIndex.toString();
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
if (cluster != null) {
builder.append(cluster);
builder.append(REMOTE_CLUSTER_INDEX_SEPARATOR);
}
builder.append(index);
return builder.toString();
return qualifiedIndex();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,40 @@ public class PreAnalyzerTests extends ESTestCase {
private PreAnalyzer preAnalyzer = new PreAnalyzer();

public void testBasicIndex() {
LogicalPlan plan = new UnresolvedRelation(EMPTY, new TableIdentifier(EMPTY, null, "index"), null, false);
LogicalPlan plan = new UnresolvedRelation(EMPTY, new TableIdentifier(EMPTY, null, "index", null), 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"));
assertThat(result.indices.get(0).id().selector(), nullValue());
}

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

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"));
assertThat(result.indices.get(0).id().selector(), is("failures"));
}

public void testComplicatedQuery() {
LogicalPlan plan = new Limit(
EMPTY,
new Literal(EMPTY, 10, INTEGER),
new UnresolvedRelation(EMPTY, new TableIdentifier(EMPTY, null, "aaa"), null, false)
new UnresolvedRelation(EMPTY, new TableIdentifier(EMPTY, null, "aaa", null), null, false)
);
PreAnalysis result = preAnalyzer.preAnalyze(plan);
assertThat(plan.preAnalyzed(), is(true));
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 @@ -29,7 +29,7 @@ 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));
return new TableIdentifier(source, visitIdentifier(ctx.catalog), unquoteIdentifier(index), visitIdentifier(ctx.selector));
}

@Override
Expand Down
Loading