Skip to content

Commit

Permalink
[BugFix] fix mod operator not be parsed correctly (#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 mergify[bot] committed Oct 18, 2023
1 parent e862a8f commit baa4930
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 @@ -5175,6 +5175,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 @@ -5188,8 +5189,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));

Check failure on line 413 in fe/fe-core/src/test/java/com/starrocks/sql/parser/ParserTest.java

View workflow job for this annotation

GitHub Actions / FE UT Report

ParserTest.testModOperator

java.lang.NullPointerException at com.starrocks.sql.analyzer.AstToSQLBuilder$AST2SQLBuilderVisitor.visitSelect(AstToSQLBuilder.java:140)
Raw output
java.lang.NullPointerException
	at com.starrocks.sql.analyzer.AstToSQLBuilder$AST2SQLBuilderVisitor.visitSelect(AstToSQLBuilder.java:140)
	at com.starrocks.sql.analyzer.AstToSQLBuilder$AST2SQLBuilderVisitor.visitSelect(AstToSQLBuilder.java:61)
	at com.starrocks.sql.ast.SelectRelation.accept(SelectRelation.java:242)
	at com.starrocks.sql.ast.AstVisitor.visit(AstVisitor.java:55)
	at com.starrocks.sql.ast.AstVisitor.visit(AstVisitor.java:51)
	at com.starrocks.sql.analyzer.AstToStringBuilder$AST2StringBuilderVisitor.visitQueryStatement(AstToStringBuilder.java:445)
	at com.starrocks.sql.analyzer.AstToStringBuilder$AST2StringBuilderVisitor.visitQueryStatement(AstToStringBuilder.java:125)
	at com.starrocks.sql.ast.QueryStatement.accept(QueryStatement.java:56)
	at com.starrocks.sql.ast.AstVisitor.visit(AstVisitor.java:55)
	at com.starrocks.sql.ast.AstVisitor.visit(AstVisitor.java:51)
	at com.starrocks.sql.analyzer.AstToSQLBuilder.toSQL(AstToSQLBuilder.java:58)
	at com.starrocks.sql.parser.ParserTest.testModOperator(ParserTest.java:413)
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 baa4930

Please sign in to comment.