(improvement)(chat) add metric aggregate only in select not exit metirc (#164)

This commit is contained in:
lexluo09
2023-09-28 17:59:37 +08:00
committed by GitHub
parent 405e846a0e
commit 2a6391a2ee
6 changed files with 31 additions and 10 deletions

View File

@@ -62,9 +62,9 @@ public abstract class BaseSemanticCorrector implements SemanticCorrector {
String sql = semanticCorrectInfo.getSql();
Long modelId = semanticCorrectInfo.getParseInfo().getModel().getModel();
SemanticSchema semanticSchema = ContextUtils.getBean(SchemaService.class).getSemanticSchema();
List<SchemaElement> metrics = getMetricElements(modelId);
Map<String, String> metricToAggregate = semanticSchema.getMetrics(modelId).stream()
Map<String, String> metricToAggregate = metrics.stream()
.map(schemaElement -> {
if (Objects.isNull(schemaElement.getDefaultAgg())) {
schemaElement.setDefaultAgg(AggregateTypeEnum.SUM.name());
@@ -79,4 +79,14 @@ public abstract class BaseSemanticCorrector implements SemanticCorrector {
String aggregateSql = SqlParserUpdateHelper.addAggregateToField(sql, metricToAggregate);
semanticCorrectInfo.setSql(aggregateSql);
}
protected List<SchemaElement> getMetricElements(Long modelId) {
SemanticSchema semanticSchema = ContextUtils.getBean(SchemaService.class).getSemanticSchema();
return semanticSchema.getMetrics(modelId);
}
protected List<SchemaElement> getDimensionElements(Long modelId) {
SemanticSchema semanticSchema = ContextUtils.getBean(SchemaService.class).getSemanticSchema();
return semanticSchema.getDimensions(modelId);
}
}

View File

@@ -14,7 +14,6 @@ public class GlobalAfterCorrector extends BaseSemanticCorrector {
public void correct(SemanticCorrectInfo semanticCorrectInfo) {
super.correct(semanticCorrectInfo);
addAggregateToMetric(semanticCorrectInfo);
String sql = semanticCorrectInfo.getSql();
if (!SqlParserSelectHelper.hasAggregateFunction(sql)) {
return;

View File

@@ -28,8 +28,6 @@ public class GlobalBeforeCorrector extends BaseSemanticCorrector {
updateFieldNameByLinkingValue(semanticCorrectInfo);
correctFieldName(semanticCorrectInfo);
addAggregateToMetric(semanticCorrectInfo);
}

View File

@@ -20,18 +20,32 @@ public class GroupByCorrector extends BaseSemanticCorrector {
public void correct(SemanticCorrectInfo semanticCorrectInfo) {
super.correct(semanticCorrectInfo);
Long modelId = semanticCorrectInfo.getParseInfo().getModel().getModel();
// if select not exit metric not add aggregate
List<String> selectFields = SqlParserSelectHelper.getSelectFields(semanticCorrectInfo.getSql());
Set<String> metrics = getMetricElements(modelId).stream()
.map(schemaElement -> schemaElement.getName())
.collect(Collectors.toSet());
if (!CollectionUtils.isEmpty(selectFields)
&& !CollectionUtils.isEmpty(metrics)
&& selectFields.stream().anyMatch(s -> metrics.contains(s))) {
return;
}
//add aggregate to all metric
String sql = semanticCorrectInfo.getSql();
Long modelId = semanticCorrectInfo.getParseInfo().getModel().getModel();
addAggregateToMetric(semanticCorrectInfo);
//add dimension group by
String sql = semanticCorrectInfo.getSql();
SemanticSchema semanticSchema = ContextUtils.getBean(SchemaService.class).getSemanticSchema();
Set<String> dimensions = semanticSchema.getDimensions(modelId).stream()
.filter(schemaElement -> !DateUtils.DATE_FIELD.equals(schemaElement.getBizName()))
.map(schemaElement -> schemaElement.getName()).collect(Collectors.toSet());
List<String> selectFields = SqlParserSelectHelper.getSelectFields(sql);
selectFields = SqlParserSelectHelper.getSelectFields(sql);
if (CollectionUtils.isEmpty(selectFields) || CollectionUtils.isEmpty(dimensions)) {
return;

View File

@@ -34,6 +34,6 @@ com.tencent.supersonic.chat.api.component.SemanticCorrector=\
com.tencent.supersonic.chat.corrector.GlobalBeforeCorrector, \
com.tencent.supersonic.chat.corrector.SelectCorrector, \
com.tencent.supersonic.chat.corrector.WhereCorrector, \
com.tencent.supersonic.chat.corrector.HavingCorrector, \
com.tencent.supersonic.chat.corrector.GroupByCorrector, \
com.tencent.supersonic.chat.corrector.HavingCorrector, \
com.tencent.supersonic.chat.corrector.GlobalAfterCorrector

View File

@@ -34,6 +34,6 @@ com.tencent.supersonic.chat.api.component.SemanticCorrector=\
com.tencent.supersonic.chat.corrector.GlobalBeforeCorrector, \
com.tencent.supersonic.chat.corrector.SelectCorrector, \
com.tencent.supersonic.chat.corrector.WhereCorrector, \
com.tencent.supersonic.chat.corrector.HavingCorrector, \
com.tencent.supersonic.chat.corrector.GroupByCorrector, \
com.tencent.supersonic.chat.corrector.HavingCorrector, \
com.tencent.supersonic.chat.corrector.GlobalAfterCorrector