mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-10 19:51:00 +00:00
[improvement] remove number condition (#375)
This commit is contained in:
@@ -5,6 +5,7 @@ import com.tencent.supersonic.chat.api.pojo.SemanticSchema;
|
||||
import com.tencent.supersonic.chat.api.pojo.request.QueryReq;
|
||||
import com.tencent.supersonic.common.util.ContextUtils;
|
||||
import com.tencent.supersonic.common.util.jsqlparser.SqlParserAddHelper;
|
||||
import com.tencent.supersonic.common.util.jsqlparser.SqlParserRemoveHelper;
|
||||
import com.tencent.supersonic.common.util.jsqlparser.SqlParserSelectFunctionHelper;
|
||||
import com.tencent.supersonic.common.util.jsqlparser.SqlParserSelectHelper;
|
||||
import com.tencent.supersonic.knowledge.service.SchemaService;
|
||||
@@ -26,6 +27,11 @@ public class HavingCorrector extends BaseSemanticCorrector {
|
||||
|
||||
//add having expression filed to select
|
||||
addHavingToSelect(semanticParseInfo);
|
||||
|
||||
//remove number condition
|
||||
String correctorSql = semanticParseInfo.getSqlInfo().getCorrectS2SQL();
|
||||
correctorSql = SqlParserRemoveHelper.removeNumberCondition(correctorSql);
|
||||
semanticParseInfo.getSqlInfo().setCorrectS2SQL(correctorSql);
|
||||
}
|
||||
|
||||
private void addHaving(SemanticParseInfo semanticParseInfo) {
|
||||
|
||||
@@ -317,8 +317,8 @@ public class QueryServiceImpl implements QueryService {
|
||||
|
||||
correctorSql = SqlParserAddHelper.addWhere(correctorSql, addWhereConditions);
|
||||
correctorSql = SqlParserAddHelper.addHaving(correctorSql, addHavingConditions);
|
||||
|
||||
log.info("correctorSql after replacing:{}", correctorSql);
|
||||
correctorSql = SqlParserRemoveHelper.removeNumberCondition(correctorSql);
|
||||
parseInfo.getSqlInfo().setCorrectS2SQL(correctorSql);
|
||||
semanticQuery.setParseInfo(parseInfo);
|
||||
String explainSql = semanticQuery.explain(user);
|
||||
|
||||
@@ -54,7 +54,7 @@ public class SqlParserRemoveHelper {
|
||||
}
|
||||
removeWhereExpression(whereExpression, removeFieldNames);
|
||||
}
|
||||
public static String removeWhereCondition(String sql) {
|
||||
public static String removeNumberCondition(String sql) {
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ class SqlParserRemoveHelperTest {
|
||||
void removeWhereHavingCondition() {
|
||||
String sql = "select 歌曲名 from 歌曲库 where sum(粉丝数) > 20000 and 2>1 and "
|
||||
+ "sum(播放量) > 20000 and 1=1 HAVING sum(播放量) > 20000 and 3>1";
|
||||
sql = SqlParserRemoveHelper.removeWhereCondition(sql);
|
||||
sql = SqlParserRemoveHelper.removeNumberCondition(sql);
|
||||
System.out.println(sql);
|
||||
Assert.assertEquals(
|
||||
"SELECT 歌曲名 FROM 歌曲库 WHERE sum(粉丝数) > 20000 AND sum(播放量) > 20000 HAVING sum(播放量) > 20000",
|
||||
@@ -22,7 +22,7 @@ class SqlParserRemoveHelperTest {
|
||||
sql = "SELECT 歌曲,sum(播放量) FROM 歌曲库\n"
|
||||
+ "WHERE (歌手名 = '张三' AND 2 > 1) AND 数据日期 = '2023-11-07'\n"
|
||||
+ "GROUP BY 歌曲名 HAVING sum(播放量) > 100000";
|
||||
sql = SqlParserRemoveHelper.removeWhereCondition(sql);
|
||||
sql = SqlParserRemoveHelper.removeNumberCondition(sql);
|
||||
System.out.println(sql);
|
||||
Assert.assertEquals(
|
||||
"SELECT 歌曲, sum(播放量) FROM 歌曲库 WHERE (歌手名 = '张三') "
|
||||
@@ -31,7 +31,7 @@ class SqlParserRemoveHelperTest {
|
||||
sql = "SELECT 歌曲名,sum(播放量) FROM 歌曲库 WHERE (1 = 1 AND 1 = 1 AND 2 > 1 )"
|
||||
+ "AND 1 = 1 AND 歌曲类型 IN ('类型一', '类型二') AND 歌手名 IN ('林俊杰', '周杰伦')"
|
||||
+ "AND 数据日期 = '2023-11-07' GROUP BY 歌曲名 HAVING 2 > 1 AND SUM(播放量) >= 1000";
|
||||
sql = SqlParserRemoveHelper.removeWhereCondition(sql);
|
||||
sql = SqlParserRemoveHelper.removeNumberCondition(sql);
|
||||
System.out.println(sql);
|
||||
Assert.assertEquals(
|
||||
"SELECT 歌曲名, sum(播放量) FROM 歌曲库 WHERE 歌曲类型 IN ('类型一', '类型二') "
|
||||
@@ -41,7 +41,7 @@ class SqlParserRemoveHelperTest {
|
||||
|
||||
sql = "SELECT 品牌名称,法人 FROM 互联网企业 WHERE (2 > 1 AND 1 = 1) AND 数据日期 = '2023-10-31'"
|
||||
+ "GROUP BY 品牌名称, 法人 HAVING 2 > 1 AND sum(注册资本) > 100000000 AND sum(营收占比) = 0.5 and 1 = 1";
|
||||
sql = SqlParserRemoveHelper.removeWhereCondition(sql);
|
||||
sql = SqlParserRemoveHelper.removeNumberCondition(sql);
|
||||
System.out.println(sql);
|
||||
Assert.assertEquals(
|
||||
"SELECT 品牌名称, 法人 FROM 互联网企业 WHERE 数据日期 = '2023-10-31' GROUP BY "
|
||||
|
||||
Reference in New Issue
Block a user