mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-10 11:07:06 +00:00
(improvement)(Headless) define the measure without agg funciton by the model (#1864)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user