Skip to content

Commit

Permalink
[BugFix] fix mod operator not be parsed correctly (StarRocks#33073)
Browse files Browse the repository at this point in the history
select 100 mod 2 doesn't be parsed correctly.

Signed-off-by: packy92 <[email protected]>
(cherry picked from commit 33e1c8b)
  • Loading branch information
packy92 authored and murphyatwork committed Oct 23, 2023
1 parent c4502fc commit af987a5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5177,6 +5177,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 @@ -5190,8 +5191,10 @@ private static ArithmeticExpr.Operator getArithmeticBinaryOperator(Token operato
return ArithmeticExpr.Operator.BIT_SHIFT_LEFT;
case StarRocksLexer.BIT_SHIFT_RIGHT:
return ArithmeticExpr.Operator.BIT_SHIFT_RIGHT;
default:
case StarRocksLexer.BIT_SHIFT_RIGHT_LOGICAL:
return ArithmeticExpr.Operator.BIT_SHIFT_RIGHT_LOGICAL;
default:
throw new ParsingException(PARSER_ERROR_MSG.wrongTypeOfArgs(operator.getText()), new NodePosition(operator));
}
}

Expand Down
11 changes: 11 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 @@ -29,10 +29,12 @@
import com.starrocks.qe.SqlModeHelper;
import com.starrocks.qe.VariableMgr;
import com.starrocks.sql.analyzer.Analyzer;
import com.starrocks.sql.analyzer.AstToSQLBuilder;
import com.starrocks.sql.ast.JoinRelation;
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;
Expand All @@ -49,6 +51,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.fail;

class ParserTest {
Expand Down Expand Up @@ -403,6 +406,14 @@ void testWrongVariableName() {
assertContains(res, "{'disable_join_reorder', 'disable_colocate_join', 'enable_predicate_reorder'}");
}

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

private static Stream<Arguments> keyWordSqls() {
List<String> sqls = Lists.newArrayList();
sqls.add("select current_role()");
Expand Down

0 comments on commit af987a5

Please sign in to comment.