(improvement)(headless) Corrector does not automatically add sys_imp_date to group by (#1141)

Co-authored-by: jolunoluo
This commit is contained in:
LXW
2024-06-13 11:49:54 +08:00
committed by GitHub
parent abf440c4d4
commit a9d66b85cc
2 changed files with 2 additions and 19 deletions

View File

@@ -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.util.ContextUtils;
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.headless.api.pojo.SchemaElement;
import com.tencent.supersonic.headless.api.pojo.SemanticParseInfo;
@@ -76,8 +75,7 @@ public abstract class BaseSemanticCorrector implements SemanticCorrector {
return result;
}
protected String addFieldsToSelect(QueryContext queryContext,
SemanticParseInfo semanticParseInfo, String correctS2SQL) {
protected String addFieldsToSelect(SemanticParseInfo semanticParseInfo, String correctS2SQL) {
Set<String> selectFields = new HashSet<>(SqlSelectHelper.getSelectFields(correctS2SQL));
Set<String> needAddFields = new HashSet<>(SqlSelectHelper.getGroupByFields(correctS2SQL));
@@ -88,17 +86,6 @@ public abstract class BaseSemanticCorrector implements SemanticCorrector {
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)) {
return correctS2SQL;
}
@@ -109,10 +96,6 @@ public abstract class BaseSemanticCorrector implements SemanticCorrector {
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) {
//add aggregate to all metric
String correctS2SQL = semanticParseInfo.getSqlInfo().getCorrectS2SQL();

View File

@@ -27,7 +27,7 @@ public class SelectCorrector extends BaseSemanticCorrector {
&& aggregateFields.size() == selectFields.size()) {
return;
}
correctS2SQL = addFieldsToSelect(queryContext, semanticParseInfo, correctS2SQL);
correctS2SQL = addFieldsToSelect(semanticParseInfo, correctS2SQL);
String querySql = SqlReplaceHelper.dealAliasToOrderBy(correctS2SQL);
semanticParseInfo.getSqlInfo().setCorrectS2SQL(querySql);
}