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

Fix arithmetic operator precedence #1172

Merged
Merged
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
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 @@ -292,11 +292,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 @@ -120,7 +120,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 @@ -153,6 +153,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