(improvement)(Headless) The corrector adds group by field, but does not add agg field, resulting in SQL syntax errors (#1082)

Co-authored-by: jolunoluo
This commit is contained in:
LXW
2024-06-03 16:15:45 +08:00
committed by GitHub
parent 778f054a95
commit df9b469019
3 changed files with 18 additions and 6 deletions

View File

@@ -96,6 +96,11 @@ public class SemanticSchema implements Serializable {
return getElementsByDataSetId(dataSetId, metrics);
}
public List<String> getMetricNames() {
return getMetrics().stream()
.map(SchemaElement::getName).collect(Collectors.toList());
}
public List<SchemaElement> getEntities() {
List<SchemaElement> entities = new ArrayList<>();
dataSetSchemaList.stream().forEach(d -> entities.add(d.getEntity()));

View File

@@ -76,7 +76,8 @@ public abstract class BaseSemanticCorrector implements SemanticCorrector {
return result;
}
protected void addFieldsToSelect(SemanticParseInfo semanticParseInfo, String correctS2SQL) {
protected String addFieldsToSelect(QueryContext queryContext,
SemanticParseInfo semanticParseInfo, String correctS2SQL) {
Set<String> selectFields = new HashSet<>(SqlSelectHelper.getSelectFields(correctS2SQL));
Set<String> needAddFields = new HashSet<>(SqlSelectHelper.getGroupByFields(correctS2SQL));
@@ -89,7 +90,8 @@ public abstract class BaseSemanticCorrector implements SemanticCorrector {
// If there is no aggregate function in the S2SQL statement and
// there is a data field in 'WHERE' statement, add the field to the 'SELECT' statement.
if (!SqlSelectFunctionHelper.hasAggregateFunction(correctS2SQL)) {
if (!SqlSelectFunctionHelper.hasAggregateFunction(correctS2SQL)
&& !hasAggFunctionToAdd(queryContext.getSemanticSchema(), needAddFields)) {
List<String> whereFields = SqlSelectHelper.getWhereFields(correctS2SQL);
List<String> timeChNameList = TimeDimensionEnum.getChNameList();
Set<String> timeFields = whereFields.stream().filter(field -> timeChNameList.contains(field))
@@ -98,12 +100,17 @@ public abstract class BaseSemanticCorrector implements SemanticCorrector {
}
if (CollectionUtils.isEmpty(selectFields) || CollectionUtils.isEmpty(needAddFields)) {
return;
return correctS2SQL;
}
needAddFields.removeAll(selectFields);
String replaceFields = SqlAddHelper.addFieldsToSelect(correctS2SQL, new ArrayList<>(needAddFields));
semanticParseInfo.getSqlInfo().setCorrectS2SQL(replaceFields);
String addFieldsToSelectSql = SqlAddHelper.addFieldsToSelect(correctS2SQL, new ArrayList<>(needAddFields));
semanticParseInfo.getSqlInfo().setCorrectS2SQL(addFieldsToSelectSql);
return addFieldsToSelectSql;
}
private boolean hasAggFunctionToAdd(SemanticSchema semanticSchema, Set<String> needAddFields) {
return needAddFields.stream().anyMatch(field -> semanticSchema.getMetricNames().contains(field));
}
protected void addAggregateToMetric(QueryContext queryContext, SemanticParseInfo semanticParseInfo) {

View File

@@ -27,7 +27,7 @@ public class SelectCorrector extends BaseSemanticCorrector {
&& aggregateFields.size() == selectFields.size()) {
return;
}
addFieldsToSelect(semanticParseInfo, correctS2SQL);
correctS2SQL = addFieldsToSelect(queryContext, semanticParseInfo, correctS2SQL);
String querySql = SqlReplaceHelper.dealAliasToOrderBy(correctS2SQL);
semanticParseInfo.getSqlInfo().setCorrectS2SQL(querySql);
}