Skip to content

Commit

Permalink
Fix precedence in PPL
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Dai <[email protected]>
  • Loading branch information
dai-chen committed Dec 14, 2022
1 parent ab0deae commit bac6f3a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
14 changes: 7 additions & 7 deletions ppl/src/main/antlr/OpenSearchPPLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,15 @@ comparisonExpression
;

valueExpression
: left=valueExpression binaryOperator right=valueExpression #binaryArithmetic
| LT_PRTHS left=valueExpression binaryOperator
right=valueExpression RT_PRTHS #parentheticBinaryArithmetic
: left=valueExpression
binaryOperator=(STAR | DIVIDE | MODULE)
right=valueExpression #binaryArithmetic
| left=valueExpression
binaryOperator=(PLUS | MINUS)
right=valueExpression #binaryArithmetic
| primaryExpression #valueExpressionDefault
| positionFunction #positionFunctionCall
| LT_PRTHS valueExpression RT_PRTHS #parentheticValueExpr
;

primaryExpression
Expand Down Expand Up @@ -499,10 +503,6 @@ comparisonOperator
: EQUAL | NOT_EQUAL | LESS | NOT_LESS | GREATER | NOT_GREATER | REGEXP
;

binaryOperator
: PLUS | MINUS | STAR | DIVIDE | MODULE
;


singleFieldRelevanceFunctionName
: MATCH
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.LogicalOrContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.LogicalXorContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.MultiFieldRelevanceFunctionContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.ParentheticBinaryArithmeticContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.ParentheticValueExprContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.PercentileAggFunctionContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.SingleFieldRelevanceFunctionContext;
import static org.opensearch.sql.ppl.antlr.parser.OpenSearchPPLParser.SortFieldContext;
Expand Down Expand Up @@ -154,18 +154,14 @@ public UnresolvedExpression visitInExpr(InExprContext ctx) {
@Override
public UnresolvedExpression visitBinaryArithmetic(BinaryArithmeticContext ctx) {
return new Function(
ctx.binaryOperator().getText(),
ctx.binaryOperator.getText(),
Arrays.asList(visit(ctx.left), visit(ctx.right))
);
}

@Override
public UnresolvedExpression visitParentheticBinaryArithmetic(
ParentheticBinaryArithmeticContext ctx) {
return new Function(
ctx.binaryOperator().getText(),
Arrays.asList(visit(ctx.left), visit(ctx.right))
);
public UnresolvedExpression visitParentheticValueExpr(ParentheticValueExprContext ctx) {
return visit(ctx.valueExpression()); // Discard parenthesis around
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,30 @@ public void testLiteralValueBinaryOperationExpr() {
));
}

@Test
public void testBinaryOperationExprWithParentheses() {
assertEqual("source = t | where a = (1 + 2) * 3",
filter(
relation("t"),
compare("=",
field("a"),
function("*",
function("+", intLiteral(1), intLiteral(2)),
intLiteral(3)))));
}

@Test
public void testBinaryOperationExprPrecedence() {
assertEqual("source = t | where a = 1 + 2 * 3",
filter(
relation("t"),
compare("=",
field("a"),
function("+",
intLiteral(1),
function("*", intLiteral(2), intLiteral(3))))));
}

@Test
public void testCompareExpr() {
assertEqual("source=t a='b'",
Expand Down
10 changes: 6 additions & 4 deletions sql/src/main/antlr/OpenSearchSQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,12 @@ expressionAtom
| columnName #fullColumnNameExpressionAtom
| functionCall #functionCallExpressionAtom
| LR_BRACKET expression RR_BRACKET #nestedExpressionAtom
| left=expressionAtom mathOperator=(STAR | DIVIDE | MODULE)
right=expressionAtom #mathExpressionAtom
| left=expressionAtom mathOperator=(PLUS | MINUS)
right=expressionAtom #mathExpressionAtom
| left=expressionAtom
mathOperator=(STAR | DIVIDE | MODULE)
right=expressionAtom #mathExpressionAtom
| left=expressionAtom
mathOperator=(PLUS | MINUS)
right=expressionAtom #mathExpressionAtom
;

comparisonOperator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void canBuildArithmeticExpression() {
}

@Test
public void canBuildArithmeticExpressionWithPrecedence() {
public void canBuildArithmeticExpressionPrecedence() {
assertEquals(
function("+",
intLiteral(1),
Expand Down

0 comments on commit bac6f3a

Please sign in to comment.