mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-12 12:37:55 +00:00
(improvement)(headless) Corrector does not automatically add sys_imp_date to group by (#1141)
Co-authored-by: jolunoluo
This commit is contained in:
@@ -4,7 +4,6 @@ import com.tencent.supersonic.common.pojo.enums.AggregateTypeEnum;
|
|||||||
import com.tencent.supersonic.common.pojo.enums.TimeDimensionEnum;
|
import com.tencent.supersonic.common.pojo.enums.TimeDimensionEnum;
|
||||||
import com.tencent.supersonic.common.util.ContextUtils;
|
import com.tencent.supersonic.common.util.ContextUtils;
|
||||||
import com.tencent.supersonic.common.util.jsqlparser.SqlAddHelper;
|
import com.tencent.supersonic.common.util.jsqlparser.SqlAddHelper;
|
||||||
import com.tencent.supersonic.common.util.jsqlparser.SqlSelectFunctionHelper;
|
|
||||||
import com.tencent.supersonic.common.util.jsqlparser.SqlSelectHelper;
|
import com.tencent.supersonic.common.util.jsqlparser.SqlSelectHelper;
|
||||||
import com.tencent.supersonic.headless.api.pojo.SchemaElement;
|
import com.tencent.supersonic.headless.api.pojo.SchemaElement;
|
||||||
import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo;
|
import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo;
|
||||||
@@ -76,8 +75,7 @@ public abstract class BaseSemanticCorrector implements SemanticCorrector {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String addFieldsToSelect(QueryContext queryContext,
|
protected String addFieldsToSelect(SemanticParseInfo semanticParseInfo, String correctS2SQL) {
|
||||||
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));
|
||||||
|
|
||||||
@@ -88,17 +86,6 @@ public abstract class BaseSemanticCorrector implements SemanticCorrector {
|
|||||||
needAddFields.addAll(SqlSelectHelper.getOrderByFields(correctS2SQL));
|
needAddFields.addAll(SqlSelectHelper.getOrderByFields(correctS2SQL));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
|
||||||
if (!SqlSelectFunctionHelper.hasAggregateFunction(correctS2SQL)
|
|
||||||
&& !hasAggFunctionToAdd(queryContext.getSemanticSchema(), needAddFields)) {
|
|
||||||
List<String> whereFields = SqlSelectHelper.getWhereFields(correctS2SQL);
|
|
||||||
List<String> timeChNameList = TimeDimensionEnum.getChNameList();
|
|
||||||
Set<String> timeFields = whereFields.stream().filter(field -> timeChNameList.contains(field))
|
|
||||||
.collect(Collectors.toSet());
|
|
||||||
needAddFields.addAll(timeFields);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (CollectionUtils.isEmpty(selectFields) || CollectionUtils.isEmpty(needAddFields)) {
|
if (CollectionUtils.isEmpty(selectFields) || CollectionUtils.isEmpty(needAddFields)) {
|
||||||
return correctS2SQL;
|
return correctS2SQL;
|
||||||
}
|
}
|
||||||
@@ -109,10 +96,6 @@ public abstract class BaseSemanticCorrector implements SemanticCorrector {
|
|||||||
return 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) {
|
||||||
//add aggregate to all metric
|
//add aggregate to all metric
|
||||||
String correctS2SQL = semanticParseInfo.getSqlInfo().getCorrectS2SQL();
|
String correctS2SQL = semanticParseInfo.getSqlInfo().getCorrectS2SQL();
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public class SelectCorrector extends BaseSemanticCorrector {
|
|||||||
&& aggregateFields.size() == selectFields.size()) {
|
&& aggregateFields.size() == selectFields.size()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
correctS2SQL = addFieldsToSelect(queryContext, semanticParseInfo, correctS2SQL);
|
correctS2SQL = addFieldsToSelect(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