Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
52 changes: 26 additions & 26 deletions x-pack/plugin/sql/src/main/antlr/SqlBase.g4
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ statement
(columnPattern=likePattern)? #sysColumns
| SYS TYPES ((PLUS | MINUS)? type=number)? #sysTypes
;

query
: (WITH namedQuery (',' namedQuery)*)? queryNoWith
;
Expand All @@ -83,10 +83,10 @@ queryNoWith
;

limitClause
: LIMIT limit=(INTEGER_VALUE | ALL)
| LIMIT_ESC limit=(INTEGER_VALUE | ALL) ESC_END
: LIMIT limit=(INTEGER_VALUE | ALL)
| LIMIT_ESC limit=(INTEGER_VALUE | ALL) ESC_END
;

queryTerm
: querySpecification #queryPrimaryDefault
| '(' queryNoWith ')' #subquery
Expand Down Expand Up @@ -134,7 +134,7 @@ setQuantifier
| ALL
;

selectItems
selectItems
: selectItem (',' selectItem)*
;

Expand Down Expand Up @@ -174,13 +174,13 @@ pivotClause
;

pivotArgs
: namedValueExpression (',' namedValueExpression)*
: namedValueExpression (',' namedValueExpression)*
;

namedValueExpression
: valueExpression (AS? identifier)?
;

expression
: booleanExpression
;
Expand Down Expand Up @@ -221,11 +221,11 @@ predicate
likePattern
: LIKE pattern
;

pattern
: value=string patternEscape?
;

patternEscape
: ESCAPE escape=string
| ESCAPE_ESC escape=string ESC_END
Expand Down Expand Up @@ -278,7 +278,7 @@ extractExpression
: extractTemplate
| FUNCTION_ESC extractTemplate ESC_END
;

extractTemplate
: EXTRACT '(' field=identifier FROM valueExpression ')'
;
Expand All @@ -287,16 +287,16 @@ functionExpression
: functionTemplate
| FUNCTION_ESC functionTemplate ESC_END
;

functionTemplate
: functionName '(' (setQuantifier? expression (',' expression)*)? ')'
;
functionName
: LEFT
| RIGHT
: LEFT
| RIGHT
| identifier
;

constant
: NULL #nullLiteral
| interval #intervalLiteral
Expand All @@ -319,9 +319,9 @@ booleanValue
;

interval
: INTERVAL sign=(PLUS | MINUS)? (valueNumeric=number | valuePattern=string) leading=intervalField (TO trailing=intervalField)?
: INTERVAL sign=(PLUS | MINUS)? (valueNumeric=number | valuePattern=string) leading=intervalField (TO trailing=intervalField)?
;

intervalField
: YEAR | YEARS | MONTH | MONTHS | DAY | DAYS | HOUR | HOURS | MINUTE | MINUTES | SECOND | SECONDS
;
Expand All @@ -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 Expand Up @@ -372,19 +372,19 @@ whenClause
// http://developer.mimer.se/validator/sql-reserved-words.tml
// https://developer.mimer.com/wp-content/uploads/standard-sql-reserved-words-summary.pdf
nonReserved
: ANALYZE | ANALYZED
: ANALYZE | ANALYZED
| CATALOGS | COLUMNS | CURRENT_DATE | CURRENT_TIME | CURRENT_TIMESTAMP
| DAY | DEBUG
| EXECUTABLE | EXPLAIN
| DAY | DEBUG
| EXECUTABLE | EXPLAIN
| FIRST | FORMAT | FULL | FUNCTIONS
| GRAPHVIZ
| HOUR
| INTERVAL
| LAST | LIMIT
| LAST | LIMIT
| MAPPED | MINUTE | MONTH
| OPTIMIZED
| PARSED | PHYSICAL | PIVOT | PLAN
| QUERY
| OPTIMIZED
| PARSED | PHYSICAL | PIVOT | PLAN
| QUERY
| RLIKE
| SCHEMAS | SECOND | SHOW | SYS
| TABLES | TEXT | TOP | TYPE | TYPES
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