Skip to content

Commit

Permalink
[BugFix] Fix typo in StringLiteral toSQL caused by mixing single quot…
Browse files Browse the repository at this point in the history
…es within double quotes (#13102)
  • Loading branch information
HangyuanLiu authored and wanpengfei-git committed Nov 14, 2022
1 parent 09f01a0 commit 7b609b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
17 changes: 6 additions & 11 deletions fe/fe-core/src/main/java/com/starrocks/analysis/StringLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,11 @@ public boolean isMinValue() {
@Override
public String toSqlImpl() {
String sql = value;
if (value != null && value.contains("\\")) {
sql = value.replace("\\", "\\\\");
if (value != null) {
if (value.contains("\\")) {
sql = value.replace("\\", "\\\\");
}
sql = sql.replace("'", "\\'");
}
return "'" + sql + "'";
}
Expand Down Expand Up @@ -161,7 +164,7 @@ public double getDoubleValue() {
* @throws AnalysisException when entire given string cannot be transformed into a date
*/
private LiteralExpr convertToDate(Type targetType) throws AnalysisException {
LiteralExpr newLiteral = null;
LiteralExpr newLiteral;
try {
newLiteral = new DateLiteral(value, targetType);
} catch (AnalysisException e) {
Expand Down Expand Up @@ -221,14 +224,6 @@ public Expr uncheckedCastTo(Type targetType) throws AnalysisException {
return super.uncheckedCastTo(targetType);
}

public Expr castToNontypedNumericLiteral() throws AnalysisException {
try {
return new DecimalLiteral(this.getValue());
} catch (Throwable e) {
return uncheckedCastTo(Type.DOUBLE);
}
}

@Override
public void write(DataOutput out) throws IOException {
super.write(out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,16 @@ public void testExpression() {
Assert.assertEquals("'\"'", AST2SQL.toString(statement.getQueryRelation().getOutputExpression().get(0)));
statement = (QueryStatement) analyzeSuccess("select \"7\\\"\\\"\"");
Assert.assertEquals("'7\"\"'", AST2SQL.toString(statement.getQueryRelation().getOutputExpression().get(0)));
statement = (QueryStatement) analyzeWithoutTestView("select '7'''");
Assert.assertEquals("'7''", AST2SQL.toString(statement.getQueryRelation().getOutputExpression().get(0)));
statement = (QueryStatement) analyzeSuccess("select '7'''");
Assert.assertEquals("'7\\''", AST2SQL.toString(statement.getQueryRelation().getOutputExpression().get(0)));
statement = (QueryStatement) analyzeSuccess("SELECT '7\\'\\''");
Assert.assertEquals("'7'''", AST2SQL.toString(statement.getQueryRelation().getOutputExpression().get(0)));
Assert.assertEquals("'7\\'\\''", AST2SQL.toString(statement.getQueryRelation().getOutputExpression().get(0)));
statement = (QueryStatement) analyzeSuccess("select \"Hello ' World ' !\"");
Assert.assertEquals("'Hello \\' World \\' !'",
AST2SQL.toString(statement.getQueryRelation().getOutputExpression().get(0)));
statement = (QueryStatement) analyzeSuccess("select 'Hello \" World \" !'");
Assert.assertEquals("'Hello \" World \" !'",
AST2SQL.toString(statement.getQueryRelation().getOutputExpression().get(0)));

analyzeSuccess("select @@`sql_mode`");
}
Expand Down

0 comments on commit 7b609b9

Please sign in to comment.