Skip to content

Commit

Permalink
Fix arithmetic operator precedence (#1172)
Browse files Browse the repository at this point in the history
* Fix precedence by reordering grammar rule

Signed-off-by: Chen Dai <[email protected]>

* Fix precedence in PPL

Signed-off-by: Chen Dai <[email protected]>

Signed-off-by: Chen Dai <[email protected]>
(cherry picked from commit ab46fdd)
  • Loading branch information
dai-chen authored and github-actions[bot] committed Dec 16, 2022
1 parent 6c0af83 commit 61977ea
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 21 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
11 changes: 6 additions & 5 deletions sql/src/main/antlr/OpenSearchSQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,12 @@ expressionAtom
| columnName #fullColumnNameExpressionAtom
| functionCall #functionCallExpressionAtom
| LR_BRACKET expression RR_BRACKET #nestedExpressionAtom
| left=expressionAtom mathOperator right=expressionAtom #mathExpressionAtom
;

mathOperator
: PLUS | MINUS | STAR | DIVIDE | MODULE
| 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 @@ -133,7 +133,7 @@ public UnresolvedExpression visitQualifiedName(QualifiedNameContext ctx) {
@Override
public UnresolvedExpression visitMathExpressionAtom(MathExpressionAtomContext ctx) {
return new Function(
ctx.mathOperator().getText(),
ctx.mathOperator.getText(),
Arrays.asList(visit(ctx.left), visit(ctx.right))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ public void canBuildArithmeticExpression() {
);
}

@Test
public void canBuildArithmeticExpressionPrecedence() {
assertEquals(
function("+",
intLiteral(1),
function("*",
intLiteral(2), intLiteral(3))),
buildExprAst("1 + 2 * 3")
);
}

@Test
public void canBuildFunctionWithoutArguments() {
assertEquals(
Expand Down

0 comments on commit 61977ea

Please sign in to comment.