mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-11 20:25:12 +00:00
(feature)(chat) optimized code in GroupByCorrector (#226)
This commit is contained in:
@@ -20,29 +20,22 @@ public class GroupByCorrector extends BaseSemanticCorrector {
|
|||||||
public void correct(SemanticCorrectInfo semanticCorrectInfo) {
|
public void correct(SemanticCorrectInfo semanticCorrectInfo) {
|
||||||
|
|
||||||
super.correct(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()
|
addGroupByFields(semanticCorrectInfo);
|
||||||
.map(schemaElement -> schemaElement.getName())
|
|
||||||
.collect(Collectors.toSet());
|
|
||||||
|
|
||||||
if (!CollectionUtils.isEmpty(selectFields)
|
addAggregate(semanticCorrectInfo);
|
||||||
&& !CollectionUtils.isEmpty(metrics)
|
|
||||||
&& selectFields.stream().anyMatch(s -> metrics.contains(s))) {
|
|
||||||
//add aggregate to all metric
|
|
||||||
addAggregateToMetric(semanticCorrectInfo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addGroupByFields(SemanticCorrectInfo semanticCorrectInfo) {
|
||||||
|
Long modelId = semanticCorrectInfo.getParseInfo().getModel().getModel();
|
||||||
|
|
||||||
//add dimension group by
|
//add dimension group by
|
||||||
String sql = semanticCorrectInfo.getSql();
|
String sql = semanticCorrectInfo.getSql();
|
||||||
SemanticSchema semanticSchema = ContextUtils.getBean(SchemaService.class).getSemanticSchema();
|
SemanticSchema semanticSchema = ContextUtils.getBean(SchemaService.class).getSemanticSchema();
|
||||||
|
|
||||||
Set<String> dimensions = semanticSchema.getDimensions(modelId).stream()
|
Set<String> dimensions = semanticSchema.getDimensions(modelId).stream()
|
||||||
.map(schemaElement -> schemaElement.getName()).collect(Collectors.toSet());
|
.map(schemaElement -> schemaElement.getName()).collect(Collectors.toSet());
|
||||||
dimensions.add(DateUtils.DATE_FIELD);
|
dimensions.add(DateUtils.DATE_FIELD);
|
||||||
selectFields = SqlParserSelectHelper.getSelectFields(sql);
|
List<String> selectFields = SqlParserSelectHelper.getSelectFields(sql);
|
||||||
|
|
||||||
if (CollectionUtils.isEmpty(selectFields) || CollectionUtils.isEmpty(dimensions)) {
|
if (CollectionUtils.isEmpty(selectFields) || CollectionUtils.isEmpty(dimensions)) {
|
||||||
return;
|
return;
|
||||||
@@ -58,7 +51,13 @@ public class GroupByCorrector extends BaseSemanticCorrector {
|
|||||||
})
|
})
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
semanticCorrectInfo.setSql(SqlParserAddHelper.addGroupBy(sql, groupByFields));
|
semanticCorrectInfo.setSql(SqlParserAddHelper.addGroupBy(sql, groupByFields));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addAggregate(SemanticCorrectInfo semanticCorrectInfo) {
|
||||||
|
List<String> sqlGroupByFields = SqlParserSelectHelper.getGroupByFields(semanticCorrectInfo.getSql());
|
||||||
|
if (CollectionUtils.isEmpty(sqlGroupByFields)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
addAggregateToMetric(semanticCorrectInfo);
|
addAggregateToMetric(semanticCorrectInfo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
pom.xml
2
pom.xml
@@ -64,7 +64,7 @@
|
|||||||
<xk.time.version>3.2.4</xk.time.version>
|
<xk.time.version>3.2.4</xk.time.version>
|
||||||
<mockito-inline.version>4.5.1</mockito-inline.version>
|
<mockito-inline.version>4.5.1</mockito-inline.version>
|
||||||
<jsqlparser.version>4.5</jsqlparser.version>
|
<jsqlparser.version>4.5</jsqlparser.version>
|
||||||
<revision>0.7.5</revision>
|
<revision>0.7.5-SNAPSHOT</revision>
|
||||||
<!-- Do not bump spotless plugin version since 2.30.0 is the latest version supports Java 8-->
|
<!-- Do not bump spotless plugin version since 2.30.0 is the latest version supports Java 8-->
|
||||||
<maven.plugin.spotless.version>2.30.0</maven.plugin.spotless.version>
|
<maven.plugin.spotless.version>2.30.0</maven.plugin.spotless.version>
|
||||||
<spotless.python.includes></spotless.python.includes>
|
<spotless.python.includes></spotless.python.includes>
|
||||||
|
|||||||
Reference in New Issue
Block a user