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)

# Conflicts:
#	fe/fe-core/src/test/java/com/starrocks/sql/parser/ParserTest.java
  • Loading branch information
packy92 authored and mergify[bot] committed Oct 18, 2023
1 parent 514b823 commit 1babdd4
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 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,6 +4528,8 @@ 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 ParsingException(PARSER_ERROR_MSG.wrongTypeOfArgs(operator.getText()), new NodePosition(operator));
}

throw new UnsupportedOperationException("Unsupported operator: " + operator.getText());
Expand Down
136 changes: 136 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,21 @@
import com.starrocks.common.Pair;
import com.starrocks.qe.SessionVariable;
import com.starrocks.qe.SqlModeHelper;
<<<<<<< HEAD
import com.starrocks.sql.ast.QueryStatement;
import com.starrocks.sql.ast.SelectList;
import com.starrocks.sql.ast.SelectRelation;
=======
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;
>>>>>>> 33e1c8bd01 ([BugFix] fix mod operator not be parsed correctly (#33073))
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
Expand All @@ -40,7 +52,11 @@
import java.util.stream.Stream;

import static com.starrocks.sql.plan.PlanTestBase.assertContains;
<<<<<<< HEAD
import static org.junit.Assert.assertThrows;
=======
import static org.junit.Assert.assertEquals;
>>>>>>> 33e1c8bd01 ([BugFix] fix mod operator not be parsed correctly (#33073))
import static org.junit.Assert.fail;

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

<<<<<<< HEAD
=======
@ParameterizedTest
@MethodSource("keyWordSqls")
void testNodeReservedWords_3(String sql) {
SessionVariable sessionVariable = new SessionVariable();
try {
SqlParser.parse(sql, sessionVariable).get(0);
} catch (Exception e) {
fail("sql should success. errMsg: " + e.getMessage());
}
}

@ParameterizedTest
@MethodSource("reservedWordSqls")
void testReservedWords(String sql) {
SessionVariable sessionVariable = new SessionVariable();
try {
SqlParser.parse(sql, sessionVariable).get(0);
fail("Not quoting reserved words. sql should fail.");
} catch (Exception e) {
Assert.assertTrue(e instanceof ParsingException);
}
}

@ParameterizedTest
@MethodSource("multipleStatements")
void testMultipleStatements(String sql, boolean isValid) {
SessionVariable sessionVariable = new SessionVariable();
try {
SqlParser.parse(sql, sessionVariable).get(0);
if (!isValid) {
fail("sql should fail.");
}
} catch (Exception e) {
if (isValid) {
fail("sql should success. errMsg: " + e.getMessage());
}
}
}

@ParameterizedTest
@MethodSource("setQuantifierInAggFunc")
void testSetQuantifierInAggFunc(String sql, boolean isValid) {
SessionVariable sessionVariable = new SessionVariable();
try {
SqlParser.parse(sql, sessionVariable).get(0);
if (!isValid) {
fail("sql should fail.");
}
} catch (Exception e) {
if (isValid) {
fail("sql should success. errMsg: " + e.getMessage());
}
}
}

@ParameterizedTest
@MethodSource("unexpectedTokenSqls")
void testUnexpectedTokenSqls(String sql, String expecting) {
SessionVariable sessionVariable = new SessionVariable();
try {
SqlParser.parse(sql, sessionVariable).get(0);
fail("sql should fail.");
} catch (Exception e) {
System.out.println(e.getMessage());
assertContains(e.getMessage(), expecting);
}
}

@Test
void testWrongVariableName() {
String res = VariableMgr.findSimilarVarNames("disable_coloce_join");
assertContains(res, "{'disable_colocate_join', 'disable_join_reorder', 'disable_function_fold_constants'}");

res = VariableMgr.findSimilarVarNames("SQL_AUTO_NULL");
assertContains(res, "{'SQL_AUTO_IS_NULL', 'sql_dialect', 'sql_mode_v2'}");

res = VariableMgr.findSimilarVarNames("pipeline");
assertContains(res, "{'pipeline_dop', 'pipeline_sink_dop', 'pipeline_profile_level'}");

res = VariableMgr.findSimilarVarNames("disable_joinreorder");
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()");
sqls.add("select current_role");
sqls.add("SHOW ALL AUTHENTICATION ");
sqls.add("CANCEL BACKUP from tbl");
sqls.add("select current_role() from tbl");
sqls.add("grant all privileges on DATABASE db1 to test");
sqls.add("revoke export on DATABASE db1 from test");
sqls.add("ALTER SYSTEM MODIFY BACKEND HOST '1' to '1'");
sqls.add("SHOW COMPUTE NODES");
sqls.add("trace times select 1");
sqls.add("select anti from t1 left anti join t2 on true");
sqls.add("select anti, semi from t1 left semi join t2 on true");
sqls.add("select * from tbl1 MINUS select * from tbl2");
return sqls.stream().map(e -> Arguments.of(e));
}


private static Stream<Arguments> reservedWordSqls() {
List<String> sqls = Lists.newArrayList();
sqls.add("select * from current_role ");
sqls.add("select * from full full join anti anti on anti.col join t1 on true");
return sqls.stream().map(e -> Arguments.of(e));
}

>>>>>>> 33e1c8bd01 ([BugFix] fix mod operator not be parsed correctly (#33073))
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 1babdd4

Please sign in to comment.