mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-10 19:51:00 +00:00
(improvement)(chat) add metric aggregate only in select not exit metirc (#164)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -28,8 +28,6 @@ public class GlobalBeforeCorrector extends BaseSemanticCorrector {
|
||||
updateFieldNameByLinkingValue(semanticCorrectInfo);
|
||||
|
||||
correctFieldName(semanticCorrectInfo);
|
||||
|
||||
addAggregateToMetric(semanticCorrectInfo);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user