Skip to content

Commit

Permalink
Fix precedence by reordering grammar rule
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 034027c commit ab0deae
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
9 changes: 4 additions & 5 deletions sql/src/main/antlr/OpenSearchSQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,10 @@ 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 canBuildArithmeticExpressionWithPrecedence() {
assertEquals(
function("+",
intLiteral(1),
function("*",
intLiteral(2), intLiteral(3))),
buildExprAst("1 + 2 * 3")
);
}

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

0 comments on commit ab0deae

Please sign in to comment.