Skip to content

Commit ab0deae

Browse files
committed
Fix precedence by reordering grammar rule
Signed-off-by: Chen Dai <[email protected]>
1 parent 034027c commit ab0deae

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

sql/src/main/antlr/OpenSearchSQLParser.g4

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,10 @@ expressionAtom
292292
| columnName #fullColumnNameExpressionAtom
293293
| functionCall #functionCallExpressionAtom
294294
| LR_BRACKET expression RR_BRACKET #nestedExpressionAtom
295-
| left=expressionAtom mathOperator right=expressionAtom #mathExpressionAtom
296-
;
297-
298-
mathOperator
299-
: PLUS | MINUS | STAR | DIVIDE | MODULE
295+
| left=expressionAtom mathOperator=(STAR | DIVIDE | MODULE)
296+
right=expressionAtom #mathExpressionAtom
297+
| left=expressionAtom mathOperator=(PLUS | MINUS)
298+
right=expressionAtom #mathExpressionAtom
300299
;
301300

302301
comparisonOperator

sql/src/main/java/org/opensearch/sql/sql/parser/AstExpressionBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public UnresolvedExpression visitQualifiedName(QualifiedNameContext ctx) {
120120
@Override
121121
public UnresolvedExpression visitMathExpressionAtom(MathExpressionAtomContext ctx) {
122122
return new Function(
123-
ctx.mathOperator().getText(),
123+
ctx.mathOperator.getText(),
124124
Arrays.asList(visit(ctx.left), visit(ctx.right))
125125
);
126126
}

sql/src/test/java/org/opensearch/sql/sql/parser/AstExpressionBuilderTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ public void canBuildArithmeticExpression() {
153153
);
154154
}
155155

156+
@Test
157+
public void canBuildArithmeticExpressionWithPrecedence() {
158+
assertEquals(
159+
function("+",
160+
intLiteral(1),
161+
function("*",
162+
intLiteral(2), intLiteral(3))),
163+
buildExprAst("1 + 2 * 3")
164+
);
165+
}
166+
156167
@Test
157168
public void canBuildFunctionWithoutArguments() {
158169
assertEquals(

0 commit comments

Comments
 (0)