Skip to content

Commit

Permalink
limit cluster prefix to table names only
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Kao <[email protected]>
  • Loading branch information
seankao-az committed Apr 27, 2023
1 parent a283a5b commit c03b69e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
8 changes: 5 additions & 3 deletions ppl/src/main/antlr/OpenSearchPPLLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,14 @@ Y: 'Y';
// LITERALS AND VALUES
//STRING_LITERAL: DQUOTA_STRING | SQUOTA_STRING | BQUOTA_STRING;
ID: ID_LITERAL;
CLUSTER: CLUSTER_PREFIX_LITERAL;
INTEGER_LITERAL: DEC_DIGIT+;
DECIMAL_LITERAL: (DEC_DIGIT+)? '.' DEC_DIGIT+;

fragment DATE_SUFFIX: ([\-.][*0-9]+)*;
fragment ID_LITERAL: ([*A-Z]+?[*A-Z_\-0-9]*':')?[@*A-Z]+?[*A-Z_\-0-9]*;
ID_DATE_SUFFIX: ID_LITERAL DATE_SUFFIX;
fragment DATE_SUFFIX: ([\-.][*0-9]+)+;
fragment ID_LITERAL: [@*A-Z]+?[*A-Z_\-0-9]*;
fragment CLUSTER_PREFIX_LITERAL: [*A-Z]+?[*A-Z_\-0-9]* COLON;
ID_DATE_SUFFIX: CLUSTER_PREFIX_LITERAL? ID_LITERAL DATE_SUFFIX;
DQUOTA_STRING: '"' ( '\\'. | '""' | ~('"'| '\\') )* '"';
SQUOTA_STRING: '\'' ('\\'. | '\'\'' | ~('\'' | '\\'))* '\'';
BQUOTA_STRING: '`' ( '\\'. | '``' | ~('`'|'\\'))* '`';
Expand Down
10 changes: 9 additions & 1 deletion ppl/src/main/antlr/OpenSearchPPLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ multiFieldRelevanceFunction

/** tables */
tableSource
: qualifiedName
: tableQualifiedName
| ID_DATE_SUFFIX
;

Expand Down Expand Up @@ -723,6 +723,10 @@ qualifiedName
: ident (DOT ident)* #identsAsQualifiedName
;

tableQualifiedName
: tableIdent (DOT ident)* #identsAsTableQualifiedName
;

wcQualifiedName
: wildcard (DOT wildcard)* #identsAsWildcardQualifiedName
;
Expand All @@ -734,6 +738,10 @@ ident
| keywordsCanBeId
;

tableIdent
: (CLUSTER)? ident
;

wildcard
: ident (MODULE ident)* (MODULE)?
| SINGLE_QUOTE wildcard SINGLE_QUOTE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.EvalFunctionCallContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.FieldExpressionContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.IdentsAsQualifiedNameContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.IdentsAsTableQualifiedNameContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.IdentsAsWildcardQualifiedNameContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.InExprContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.IntegerLiteralContext;
Expand All @@ -50,6 +51,7 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RuleContext;
import org.opensearch.sql.ast.dsl.AstDSL;
Expand Down Expand Up @@ -280,8 +282,8 @@ public UnresolvedExpression visitMultiFieldRelevanceFunction(

@Override
public UnresolvedExpression visitTableSource(TableSourceContext ctx) {
if (ctx.getChild(0) instanceof IdentsAsQualifiedNameContext) {
return visitIdentifiers(((IdentsAsQualifiedNameContext) ctx.getChild(0)).ident());
if (ctx.getChild(0) instanceof IdentsAsTableQualifiedNameContext) {
return visitIdentsAsTableQualifiedName((IdentsAsTableQualifiedNameContext) ctx.getChild(0));
} else {
return visitIdentifiers(Arrays.asList(ctx));
}
Expand All @@ -304,6 +306,14 @@ public UnresolvedExpression visitIdentsAsQualifiedName(IdentsAsQualifiedNameCont
return visitIdentifiers(ctx.ident());
}

@Override
public UnresolvedExpression visitIdentsAsTableQualifiedName(
IdentsAsTableQualifiedNameContext ctx) {
return visitIdentifiers(
Stream.concat(Stream.of(ctx.tableIdent()), ctx.ident().stream())
.collect(Collectors.toList()));
}

@Override
public UnresolvedExpression visitIdentsAsWildcardQualifiedName(
IdentsAsWildcardQualifiedNameContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ public void testSearchCommandCrossClusterShouldPass() {
assertNotEquals(null, tree);
}

@Test
public void testSearchCommandCrossClusterHiddenShouldPass() {
ParseTree tree = new PPLSyntaxParser().parse("search source=c:.t a=1 b=2");
assertNotEquals(null, tree);
}

@Test
public void testSearchCommandCrossClusterQualifiedShouldPass() {
ParseTree tree = new PPLSyntaxParser().parse("search source=c:t.u a=1 b=2");
assertNotEquals(null, tree);
}

@Test
public void testSearchCommandCrossClusterHiddenQualifiedShouldPass() {
ParseTree tree = new PPLSyntaxParser().parse("search source=c:.t.u a=1 b=2");
assertNotEquals(null, tree);
}

@Test
public void testSearchCommandMatchAllCrossClusterShouldPass() {
ParseTree tree = new PPLSyntaxParser().parse("search source=*:t a=1 b=2");
Expand Down

0 comments on commit c03b69e

Please sign in to comment.