Skip to content

Commit

Permalink
[BugFix] fix mod operator not be parsed correctly (backport #33073)
Browse files Browse the repository at this point in the history
Signed-off-by: packy92 <[email protected]>
  • Loading branch information
packy92 authored and wanpengfei-git committed Oct 20, 2023
1 parent 133a7c2 commit bd7ef06
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4512,6 +4512,7 @@ private static ArithmeticExpr.Operator getArithmeticBinaryOperator(Token operato
case StarRocksLexer.SLASH_SYMBOL:
return ArithmeticExpr.Operator.DIVIDE;
case StarRocksLexer.PERCENT_SYMBOL:
case StarRocksLexer.MOD:
return ArithmeticExpr.Operator.MOD;
case StarRocksLexer.INT_DIV:
return ArithmeticExpr.Operator.INT_DIVIDE;
Expand All @@ -4527,9 +4528,9 @@ private static ArithmeticExpr.Operator getArithmeticBinaryOperator(Token operato
return ArithmeticExpr.Operator.BIT_SHIFT_RIGHT;
case StarRocksLexer.BIT_SHIFT_RIGHT_LOGICAL:
return ArithmeticExpr.Operator.BIT_SHIFT_RIGHT_LOGICAL;
default:
throw new UnsupportedOperationException("Unsupported operator: " + operator.getText());
}

throw new UnsupportedOperationException("Unsupported operator: " + operator.getText());
}

@Override
Expand Down
14 changes: 14 additions & 0 deletions fe/fe-core/src/test/java/com/starrocks/sql/parser/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
import com.starrocks.common.Pair;
import com.starrocks.qe.SessionVariable;
import com.starrocks.qe.SqlModeHelper;
import com.starrocks.sql.analyzer.Analyzer;
import com.starrocks.sql.analyzer.AstToSQLBuilder;
import com.starrocks.sql.ast.QueryStatement;
import com.starrocks.sql.ast.SelectList;
import com.starrocks.sql.ast.SelectRelation;
import com.starrocks.sql.ast.StatementBase;
import com.starrocks.utframe.UtFrameUtils;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
Expand All @@ -40,6 +44,7 @@
import java.util.stream.Stream;

import static com.starrocks.sql.plan.PlanTestBase.assertContains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -175,6 +180,15 @@ void testSettingSqlMode() throws InterruptedException {
exprs[1] instanceof FunctionCallExpr);
}

@Test
void testModOperator() {
String sql = "select 100 MOD 2";
List<StatementBase> stmts = SqlParser.parse(sql, new SessionVariable());
Analyzer.analyze(stmts.get(0), UtFrameUtils.createDefaultCtx());
String newSql = AstToSQLBuilder.toSQL(stmts.get(0));
assertEquals("SELECT 100 % 2 AS `100 % 2`", newSql);
}

private static Stream<Arguments> multipleStatements() {
List<Pair<String, Boolean>> sqls = Lists.newArrayList();
sqls.add(Pair.create("select 1;;;;;;select 2", true));
Expand Down

0 comments on commit bd7ef06

Please sign in to comment.