Skip to content

Commit

Permalink
[fix][headless] Solve the problem of SQL execution error when alias i…
Browse files Browse the repository at this point in the history
…s Chinese
  • Loading branch information
Hwwwww-dev committed Feb 9, 2025
1 parent eef7b3c commit 5bebe93
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.tencent.supersonic.common.jsqlparser;

import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.statement.select.SelectItem;
import net.sf.jsqlparser.statement.select.SelectItemVisitorAdapter;
import org.apache.commons.lang3.StringUtils;

public class FieldAliasReplaceWithBackticksVisitor extends SelectItemVisitorAdapter {

@Override
public void visit(SelectItem selectExpressionItem) {
Alias alias = selectExpressionItem.getAlias();
if (alias == null) {
return;
}
String aliasName = alias.getName();
String replaceValue = loadRefName(aliasName);
if (StringUtils.isBlank(replaceValue)) {
return;
}
alias.setName(replaceValue);
}

private String loadRefName(String aliasName) {
if (StringUtils.isBlank(aliasName)) {
return "";
}
if (aliasName.startsWith("`") && aliasName.endsWith("`")) {
return "";
}
return "`" + aliasName + "`";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,19 @@ public static String replaceAliasFieldName(String sql, Map<String, String> field
return selectStatement.toString();
}

public static String replaceAliasWithBackticks(String sql) {
Select selectStatement = SqlSelectHelper.getSelect(sql);
if (!(selectStatement instanceof PlainSelect)) {
return sql;
}
PlainSelect plainSelect = (PlainSelect) selectStatement;
FieldAliasReplaceWithBackticksVisitor visitor = new FieldAliasReplaceWithBackticksVisitor();
for (SelectItem selectItem : plainSelect.getSelectItems()) {
selectItem.accept(visitor);
}
return selectStatement.toString();
}

public static String replaceAlias(String sql) {
Select selectStatement = SqlSelectHelper.getSelect(sql);
if (!(selectStatement instanceof PlainSelect)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,26 @@ void testReplaceAlias() {
replaceSql);
}

@Test
void testReplaceAliasWithBackticks() {
String sql = "SELECT 部门, SUM(访问次数) AS 总访问次数 FROM 超音数 WHERE "
+ "datediff('day', 数据日期, '2023-09-05') <= 3 GROUP BY 部门 ORDER BY 总访问次数 DESC LIMIT 10";
String replaceSql = SqlReplaceHelper.replaceAliasWithBackticks(sql);
System.out.println(replaceSql);
Assert.assertEquals("SELECT 部门, SUM(访问次数) AS `总访问次数` FROM 超音数 WHERE "
+ "datediff('day', 数据日期, '2023-09-05') <= 3 GROUP BY 部门 ORDER BY 总访问次数 DESC LIMIT 10",
replaceSql);

sql = "select 部门, sum(访问次数) as 访问次数 from 超音数 where "
+ "(datediff('day', 数据日期, '2023-09-05') <= 3) and 数据日期 = '2023-10-10' "
+ "group by 部门 order by 访问次数 desc limit 10";
replaceSql = SqlReplaceHelper.replaceAliasWithBackticks(sql);
System.out.println(replaceSql);
Assert.assertEquals("SELECT 部门, sum(访问次数) AS `访问次数` FROM 超音数 WHERE (datediff('day', 数据日期, "
+ "'2023-09-05') <= 3) AND 数据日期 = '2023-10-10' GROUP BY 部门 ORDER BY 访问次数 DESC LIMIT 10",
replaceSql);
}

@Test
void testReplaceAliasFieldName() {
Map<String, String> map = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public void parse(QueryStatement queryStatement) throws Exception {
ontologyQuery.setAggOption(sqlQueryAggOption);

convertNameToBizName(queryStatement);
// Solve the problem of SQL execution error when alias is Chinese
aliasesWithBackticks(queryStatement);
rewriteOrderBy(queryStatement);

// fill sqlQuery
Expand All @@ -88,6 +90,12 @@ public void parse(QueryStatement queryStatement) throws Exception {
log.info("parse sqlQuery [{}] ", sqlQuery);
}

private void aliasesWithBackticks(QueryStatement queryStatement) {
String sql = queryStatement.getSqlQuery().getSql();
sql = SqlReplaceHelper.replaceAliasWithBackticks(sql);
queryStatement.getSqlQuery().setSql(sql);
}

private AggOption getAggOption(String sql, Set<MetricSchemaResp> metricSchemas) {
if (SqlSelectFunctionHelper.hasAggregateFunction(sql)) {
return AggOption.AGGREGATION;
Expand Down

0 comments on commit 5bebe93

Please sign in to comment.