Skip to content

Commit

Permalink
[BugFix] fix mod operator not be parsed correctly (backport StarRocks…
Browse files Browse the repository at this point in the history
…#33073)

Signed-off-by: packy92 <[email protected]>
  • Loading branch information
packy92 committed Oct 20, 2023
1 parent 2644838 commit b1b03c7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4987,6 +4987,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 @@ -5002,9 +5003,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
20 changes: 16 additions & 4 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 @@ -27,9 +27,11 @@
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;
Expand All @@ -46,6 +48,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 @@ -171,7 +174,7 @@ void testParseLargeDecimal() {
QueryStatement stmt = (QueryStatement) SqlParser.parse(sql, sessionVariable).get(0);
Analyzer.analyze(stmt, ctx);
Type type = stmt.getQueryRelation().getOutputExpression().get(0).getType();
Assert.assertEquals(type, ScalarType.createDecimalV3Type(PrimitiveType.DECIMAL128, 38, 0));
assertEquals(type, ScalarType.createDecimalV3Type(PrimitiveType.DECIMAL128, 38, 0));
} catch (Throwable err) {
Assert.fail(err.getMessage());
}
Expand All @@ -182,7 +185,7 @@ void testParseLargeDecimal() {
QueryStatement stmt = (QueryStatement) SqlParser.parse(sql, sessionVariable).get(0);
Analyzer.analyze(stmt, ctx);
Type type = stmt.getQueryRelation().getOutputExpression().get(0).getType();
Assert.assertEquals(type, ScalarType.createDecimalV3Type(PrimitiveType.DECIMAL128, 38, 0));
assertEquals(type, ScalarType.createDecimalV3Type(PrimitiveType.DECIMAL128, 38, 0));
} catch (Throwable err) {
Assert.fail(err.getMessage());
}
Expand All @@ -206,8 +209,8 @@ void testDecimalTypeDeclarationMysqlCompatibility() {
Analyzer.analyze(stmt, ctx);
Type type1 = stmt.getQueryRelation().getOutputExpression().get(0).getType();
Type type2 = stmt.getQueryRelation().getOutputExpression().get(1).getType();
Assert.assertEquals(type1, ScalarType.createDecimalV3Type(PrimitiveType.DECIMAL128, 38, 0));
Assert.assertEquals(type2, ScalarType.createDecimalV3Type(PrimitiveType.DECIMAL64, 10, 0));
assertEquals(type1, ScalarType.createDecimalV3Type(PrimitiveType.DECIMAL128, 38, 0));
assertEquals(type2, ScalarType.createDecimalV3Type(PrimitiveType.DECIMAL64, 10, 0));
}

@Test
Expand Down Expand Up @@ -283,4 +286,13 @@ private static Stream<Arguments> multipleStatements() {
sqls.add(Pair.create("select 1 xxx select 2 xxx", false));
return sqls.stream().map(e -> Arguments.of(e.first, e.second));
}

@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);
}
}

0 comments on commit b1b03c7

Please sign in to comment.