From 86bf40c8fbfc491d90199764eb770d2b9fb1b751 Mon Sep 17 00:00:00 2001 From: lexluo09 <39718951+lexluo09@users.noreply.github.com> Date: Mon, 16 Oct 2023 12:11:59 +0800 Subject: [PATCH] (feature)(chat) optimized code in GroupByCorrector (#226) --- .../chat/corrector/GroupByCorrector.java | 27 +++++++++---------- pom.xml | 2 +- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/chat/core/src/main/java/com/tencent/supersonic/chat/corrector/GroupByCorrector.java b/chat/core/src/main/java/com/tencent/supersonic/chat/corrector/GroupByCorrector.java index f9e8ed191..fd312c3b4 100644 --- a/chat/core/src/main/java/com/tencent/supersonic/chat/corrector/GroupByCorrector.java +++ b/chat/core/src/main/java/com/tencent/supersonic/chat/corrector/GroupByCorrector.java @@ -20,29 +20,22 @@ 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 selectFields = SqlParserSelectHelper.getSelectFields(semanticCorrectInfo.getSql()); - Set metrics = getMetricElements(modelId).stream() - .map(schemaElement -> schemaElement.getName()) - .collect(Collectors.toSet()); + addGroupByFields(semanticCorrectInfo); - if (!CollectionUtils.isEmpty(selectFields) - && !CollectionUtils.isEmpty(metrics) - && selectFields.stream().anyMatch(s -> metrics.contains(s))) { - //add aggregate to all metric - addAggregateToMetric(semanticCorrectInfo); - } + addAggregate(semanticCorrectInfo); + } + + private void addGroupByFields(SemanticCorrectInfo semanticCorrectInfo) { + Long modelId = semanticCorrectInfo.getParseInfo().getModel().getModel(); //add dimension group by String sql = semanticCorrectInfo.getSql(); SemanticSchema semanticSchema = ContextUtils.getBean(SchemaService.class).getSemanticSchema(); - Set dimensions = semanticSchema.getDimensions(modelId).stream() .map(schemaElement -> schemaElement.getName()).collect(Collectors.toSet()); dimensions.add(DateUtils.DATE_FIELD); - selectFields = SqlParserSelectHelper.getSelectFields(sql); + List selectFields = SqlParserSelectHelper.getSelectFields(sql); if (CollectionUtils.isEmpty(selectFields) || CollectionUtils.isEmpty(dimensions)) { return; @@ -58,7 +51,13 @@ public void correct(SemanticCorrectInfo semanticCorrectInfo) { }) .collect(Collectors.toSet()); semanticCorrectInfo.setSql(SqlParserAddHelper.addGroupBy(sql, groupByFields)); + } + private void addAggregate(SemanticCorrectInfo semanticCorrectInfo) { + List sqlGroupByFields = SqlParserSelectHelper.getGroupByFields(semanticCorrectInfo.getSql()); + if (CollectionUtils.isEmpty(sqlGroupByFields)) { + return; + } addAggregateToMetric(semanticCorrectInfo); } } diff --git a/pom.xml b/pom.xml index 3ec00e39d..2b605c8f7 100644 --- a/pom.xml +++ b/pom.xml @@ -64,7 +64,7 @@ 3.2.4 4.5.1 4.5 - 0.7.5 + 0.7.5-SNAPSHOT 2.30.0