mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-10 11:07:06 +00:00
(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:
@@ -96,6 +96,11 @@ public class SemanticSchema implements Serializable {
|
|||||||
return getElementsByDataSetId(dataSetId, metrics);
|
return getElementsByDataSetId(dataSetId, metrics);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> getMetricNames() {
|
||||||
|
return getMetrics().stream()
|
||||||
|
.map(SchemaElement::getName).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
public List<SchemaElement> getEntities() {
|
public List<SchemaElement> getEntities() {
|
||||||
List<SchemaElement> entities = new ArrayList<>();
|
List<SchemaElement> entities = new ArrayList<>();
|
||||||
dataSetSchemaList.stream().forEach(d -> entities.add(d.getEntity()));
|
dataSetSchemaList.stream().forEach(d -> entities.add(d.getEntity()));
|
||||||
|
|||||||
@@ -76,7 +76,8 @@ public abstract class BaseSemanticCorrector implements SemanticCorrector {
|
|||||||
return result;
|
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> selectFields = new HashSet<>(SqlSelectHelper.getSelectFields(correctS2SQL));
|
||||||
Set<String> needAddFields = new HashSet<>(SqlSelectHelper.getGroupByFields(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
|
// 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.
|
// 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> whereFields = SqlSelectHelper.getWhereFields(correctS2SQL);
|
||||||
List<String> timeChNameList = TimeDimensionEnum.getChNameList();
|
List<String> timeChNameList = TimeDimensionEnum.getChNameList();
|
||||||
Set<String> timeFields = whereFields.stream().filter(field -> timeChNameList.contains(field))
|
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)) {
|
if (CollectionUtils.isEmpty(selectFields) || CollectionUtils.isEmpty(needAddFields)) {
|
||||||
return;
|
return correctS2SQL;
|
||||||
}
|
}
|
||||||
|
|
||||||
needAddFields.removeAll(selectFields);
|
needAddFields.removeAll(selectFields);
|
||||||
String replaceFields = SqlAddHelper.addFieldsToSelect(correctS2SQL, new ArrayList<>(needAddFields));
|
String addFieldsToSelectSql = SqlAddHelper.addFieldsToSelect(correctS2SQL, new ArrayList<>(needAddFields));
|
||||||
semanticParseInfo.getSqlInfo().setCorrectS2SQL(replaceFields);
|
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) {
|
protected void addAggregateToMetric(QueryContext queryContext, SemanticParseInfo semanticParseInfo) {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public class SelectCorrector extends BaseSemanticCorrector {
|
|||||||
&& aggregateFields.size() == selectFields.size()) {
|
&& aggregateFields.size() == selectFields.size()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
addFieldsToSelect(semanticParseInfo, correctS2SQL);
|
correctS2SQL = addFieldsToSelect(queryContext, semanticParseInfo, correctS2SQL);
|
||||||
String querySql = SqlReplaceHelper.dealAliasToOrderBy(correctS2SQL);
|
String querySql = SqlReplaceHelper.dealAliasToOrderBy(correctS2SQL);
|
||||||
semanticParseInfo.getSqlInfo().setCorrectS2SQL(querySql);
|
semanticParseInfo.getSqlInfo().setCorrectS2SQL(querySql);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user