(improvement)(Headless) define the measure without agg funciton by the model (#1864)

This commit is contained in:
jipeli
2024-10-30 22:13:13 +08:00
committed by GitHub
parent 68d5dac14c
commit 858feb9c3c
4 changed files with 36 additions and 6 deletions

View File

@@ -89,4 +89,18 @@ public class SqlMergeWithUtils {
SqlPrettyWriter writer = new SqlPrettyWriter(config);
return writer.format(resultNode);
}
public static boolean hasWith(EngineType engineType, String sql) throws SqlParseException {
SqlParser.Config parserConfig = Configuration.getParserConfig(engineType);
SqlParser parser = SqlParser.create(sql, parserConfig);
SqlNode sqlNode = parser.parseQuery();
SqlNode sqlSelect = sqlNode;
if (sqlNode instanceof SqlOrderBy) {
SqlOrderBy sqlOrderBy = (SqlOrderBy) sqlNode;
sqlSelect = sqlOrderBy.query;
} else if (sqlNode instanceof SqlSelect) {
sqlSelect = (SqlSelect) sqlNode;
}
return sqlSelect instanceof SqlWith;
}
}

View File

@@ -99,12 +99,20 @@ public class DefaultSemanticTranslator implements SemanticTranslator {
if (!tables.isEmpty()) {
String sql;
if (dataSetQueryParam.isSupportWith()) {
List<String> parentWithNameList =
tables.stream().map(table -> table[0]).collect(Collectors.toList());
List<String> parentSqlList =
tables.stream().map(table -> table[1]).collect(Collectors.toList());
sql = SqlMergeWithUtils.mergeWith(engineType, dataSetQueryParam.getSql(),
parentSqlList, parentWithNameList);
if (!SqlMergeWithUtils.hasWith(engineType, dataSetQueryParam.getSql())) {
sql = "with "
+ tables.stream()
.map(t -> String.format("%s as (%s)", t[0], t[1]))
.collect(Collectors.joining(","))
+ "\n" + dataSetQueryParam.getSql();
} else {
List<String> parentWithNameList = tables.stream().map(table -> table[0])
.collect(Collectors.toList());
List<String> parentSqlList = tables.stream().map(table -> table[1])
.collect(Collectors.toList());
sql = SqlMergeWithUtils.mergeWith(engineType,
dataSetQueryParam.getSql(), parentSqlList, parentWithNameList);
}
} else {
sql = dataSetQueryParam.getSql();
for (String[] tb : tables) {

View File

@@ -69,6 +69,9 @@ public class CalciteQueryParser implements QueryParser {
private String getSqlByDataSet(EngineType engineType, String parentSql, String dataSetSql,
String parentAlias) throws SqlParseException {
if (!SqlMergeWithUtils.hasWith(engineType, dataSetSql)) {
return String.format("with %s as (%s) %s", parentAlias, parentSql, dataSetSql);
}
return SqlMergeWithUtils.mergeWith(engineType, dataSetSql,
Collections.singletonList(parentSql), Collections.singletonList(parentAlias));
}

View File

@@ -171,6 +171,11 @@ public class QueryReqConverter {
log.debug("getAggOption find null defaultAgg metric set to NATIVE");
return AggOption.OUTER;
}
if (!SqlSelectFunctionHelper.hasAggregateFunction(sql) && !SqlSelectHelper.hasGroupBy(sql)
&& !SqlSelectHelper.hasWith(sql) && !SqlSelectHelper.hasSubSelect(sql)) {
log.debug("getAggOption simple sql set to NATIVE");
return AggOption.NATIVE;
}
return AggOption.DEFAULT;
}