mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-12 20:51:48 +00:00
(improvement)(Headless) queryBySql supports the 'correct' feature. (#907)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.tencent.supersonic.common.pojo.enums;
|
package com.tencent.supersonic.common.pojo.enums;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -58,7 +59,7 @@ public enum TimeDimensionEnum {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if a time dimension field is included in a Chinese text field
|
* Determine if a time dimension field is included in a Chinese/English text field
|
||||||
*
|
*
|
||||||
* @param fields field
|
* @param fields field
|
||||||
* @return true/false
|
* @return true/false
|
||||||
@@ -67,8 +68,6 @@ public enum TimeDimensionEnum {
|
|||||||
if (CollectionUtil.isEmpty(fields)) {
|
if (CollectionUtil.isEmpty(fields)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return fields.contains(TimeDimensionEnum.DAY.getChName())
|
return fields.stream().anyMatch(field -> containsTimeDimension(field));
|
||||||
|| fields.contains(TimeDimensionEnum.WEEK.getChName())
|
|
||||||
|| fields.contains(TimeDimensionEnum.MONTH.getChName());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,15 @@ package com.tencent.supersonic.headless.server.rest.api;
|
|||||||
|
|
||||||
import com.tencent.supersonic.auth.api.authentication.pojo.User;
|
import com.tencent.supersonic.auth.api.authentication.pojo.User;
|
||||||
import com.tencent.supersonic.auth.api.authentication.utils.UserHolder;
|
import com.tencent.supersonic.auth.api.authentication.utils.UserHolder;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo;
|
||||||
|
import com.tencent.supersonic.headless.api.pojo.SqlInfo;
|
||||||
import com.tencent.supersonic.headless.api.pojo.request.QuerySqlReq;
|
import com.tencent.supersonic.headless.api.pojo.request.QuerySqlReq;
|
||||||
import com.tencent.supersonic.headless.api.pojo.request.QuerySqlsReq;
|
import com.tencent.supersonic.headless.api.pojo.request.QuerySqlsReq;
|
||||||
import com.tencent.supersonic.headless.api.pojo.request.SemanticQueryReq;
|
import com.tencent.supersonic.headless.api.pojo.request.SemanticQueryReq;
|
||||||
|
import com.tencent.supersonic.headless.core.chat.corrector.GrammarCorrector;
|
||||||
|
import com.tencent.supersonic.headless.core.pojo.QueryContext;
|
||||||
import com.tencent.supersonic.headless.server.service.QueryService;
|
import com.tencent.supersonic.headless.server.service.QueryService;
|
||||||
|
import com.tencent.supersonic.headless.server.utils.ComponentFactory;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -32,6 +37,7 @@ public class SqlQueryApiController {
|
|||||||
HttpServletRequest request,
|
HttpServletRequest request,
|
||||||
HttpServletResponse response) throws Exception {
|
HttpServletResponse response) throws Exception {
|
||||||
User user = UserHolder.findUser(request, response);
|
User user = UserHolder.findUser(request, response);
|
||||||
|
correct(querySqlReq);
|
||||||
return queryService.queryByReq(querySqlReq, user);
|
return queryService.queryByReq(querySqlReq, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,8 +51,25 @@ public class SqlQueryApiController {
|
|||||||
QuerySqlReq querySqlReq = new QuerySqlReq();
|
QuerySqlReq querySqlReq = new QuerySqlReq();
|
||||||
BeanUtils.copyProperties(querySqlsReq, querySqlReq);
|
BeanUtils.copyProperties(querySqlsReq, querySqlReq);
|
||||||
querySqlReq.setSql(sql);
|
querySqlReq.setSql(sql);
|
||||||
|
correct(querySqlReq);
|
||||||
return querySqlReq;
|
return querySqlReq;
|
||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
return queryService.queryByReqs(semanticQueryReqs, user);
|
return queryService.queryByReqs(semanticQueryReqs, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void correct(QuerySqlReq querySqlReq) {
|
||||||
|
QueryContext queryCtx = new QueryContext();
|
||||||
|
SemanticParseInfo semanticParseInfo = new SemanticParseInfo();
|
||||||
|
SqlInfo sqlInfo = new SqlInfo();
|
||||||
|
sqlInfo.setCorrectS2SQL(querySqlReq.getSql());
|
||||||
|
sqlInfo.setS2SQL(querySqlReq.getSql());
|
||||||
|
semanticParseInfo.setSqlInfo(sqlInfo);
|
||||||
|
|
||||||
|
ComponentFactory.getSemanticCorrectors().forEach(corrector -> {
|
||||||
|
if (!(corrector instanceof GrammarCorrector)) {
|
||||||
|
corrector.correct(queryCtx, semanticParseInfo);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
querySqlReq.setSql(sqlInfo.getCorrectS2SQL());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -180,6 +180,7 @@ public class ChatQueryServiceImpl implements ChatQueryService {
|
|||||||
.modelIdToDataSetIds(modelIdToDataSetIds)
|
.modelIdToDataSetIds(modelIdToDataSetIds)
|
||||||
.text2SQLType(queryReq.getText2SQLType())
|
.text2SQLType(queryReq.getText2SQLType())
|
||||||
.mapModeEnum(queryReq.getMapModeEnum())
|
.mapModeEnum(queryReq.getMapModeEnum())
|
||||||
|
.dataSetIds(queryReq.getDataSetIds())
|
||||||
.build();
|
.build();
|
||||||
BeanUtils.copyProperties(queryReq, queryCtx);
|
BeanUtils.copyProperties(queryReq, queryCtx);
|
||||||
return queryCtx;
|
return queryCtx;
|
||||||
|
|||||||
@@ -116,6 +116,9 @@ public class DataSetServiceImpl
|
|||||||
if (metaFilter.getName() != null) {
|
if (metaFilter.getName() != null) {
|
||||||
wrapper.lambda().eq(DataSetDO::getName, metaFilter.getName());
|
wrapper.lambda().eq(DataSetDO::getName, metaFilter.getName());
|
||||||
}
|
}
|
||||||
|
if (!CollectionUtils.isEmpty(metaFilter.getNames())) {
|
||||||
|
wrapper.lambda().in(DataSetDO::getName, metaFilter.getNames());
|
||||||
|
}
|
||||||
wrapper.lambda().ne(DataSetDO::getStatus, StatusEnum.DELETED.getCode());
|
wrapper.lambda().ne(DataSetDO::getStatus, StatusEnum.DELETED.getCode());
|
||||||
return list(wrapper).stream().map(entry -> convert(entry, user)).collect(Collectors.toList());
|
return list(wrapper).stream().map(entry -> convert(entry, user)).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -157,13 +157,13 @@ public class QueryReqConverter {
|
|||||||
return AggOption.DEFAULT;
|
return AggOption.DEFAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void convertNameToBizName(QuerySqlReq databaseReq, SemanticSchemaResp semanticSchemaResp) {
|
private void convertNameToBizName(QuerySqlReq querySqlReq, SemanticSchemaResp semanticSchemaResp) {
|
||||||
Map<String, String> fieldNameToBizNameMap = getFieldNameToBizNameMap(semanticSchemaResp);
|
Map<String, String> fieldNameToBizNameMap = getFieldNameToBizNameMap(semanticSchemaResp);
|
||||||
String sql = databaseReq.getSql();
|
String sql = querySqlReq.getSql();
|
||||||
log.info("convert name to bizName before:{}", sql);
|
log.info("dataSetId:{},convert name to bizName before:{}", querySqlReq.getDataSetId(), sql);
|
||||||
String replaceFields = SqlReplaceHelper.replaceFields(sql, fieldNameToBizNameMap, true);
|
String replaceFields = SqlReplaceHelper.replaceFields(sql, fieldNameToBizNameMap, true);
|
||||||
log.info("convert name to bizName after:{}", replaceFields);
|
log.info("dataSetId:{},convert name to bizName after:{}", querySqlReq.getDataSetId(), replaceFields);
|
||||||
databaseReq.setSql(replaceFields);
|
querySqlReq.setSql(replaceFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Set<String> getDimensions(SemanticSchemaResp semanticSchemaResp, List<String> allFields) {
|
private Set<String> getDimensions(SemanticSchemaResp semanticSchemaResp, List<String> allFields) {
|
||||||
|
|||||||
Reference in New Issue
Block a user