mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-11 12:07:42 +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);
|
SqlPrettyWriter writer = new SqlPrettyWriter(config);
|
||||||
return writer.format(resultNode);
|
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()) {
|
if (!tables.isEmpty()) {
|
||||||
String sql;
|
String sql;
|
||||||
if (dataSetQueryParam.isSupportWith()) {
|
if (dataSetQueryParam.isSupportWith()) {
|
||||||
List<String> parentWithNameList =
|
if (!SqlMergeWithUtils.hasWith(engineType, dataSetQueryParam.getSql())) {
|
||||||
tables.stream().map(table -> table[0]).collect(Collectors.toList());
|
sql = "with "
|
||||||
List<String> parentSqlList =
|
+ tables.stream()
|
||||||
tables.stream().map(table -> table[1]).collect(Collectors.toList());
|
.map(t -> String.format("%s as (%s)", t[0], t[1]))
|
||||||
sql = SqlMergeWithUtils.mergeWith(engineType, dataSetQueryParam.getSql(),
|
.collect(Collectors.joining(","))
|
||||||
parentSqlList, parentWithNameList);
|
+ "\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 {
|
} else {
|
||||||
sql = dataSetQueryParam.getSql();
|
sql = dataSetQueryParam.getSql();
|
||||||
for (String[] tb : tables) {
|
for (String[] tb : tables) {
|
||||||
|
|||||||
@@ -69,6 +69,9 @@ public class CalciteQueryParser implements QueryParser {
|
|||||||
|
|
||||||
private String getSqlByDataSet(EngineType engineType, String parentSql, String dataSetSql,
|
private String getSqlByDataSet(EngineType engineType, String parentSql, String dataSetSql,
|
||||||
String parentAlias) throws SqlParseException {
|
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,
|
return SqlMergeWithUtils.mergeWith(engineType, dataSetSql,
|
||||||
Collections.singletonList(parentSql), Collections.singletonList(parentAlias));
|
Collections.singletonList(parentSql), Collections.singletonList(parentAlias));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -171,6 +171,11 @@ public class QueryReqConverter {
|
|||||||
log.debug("getAggOption find null defaultAgg metric set to NATIVE");
|
log.debug("getAggOption find null defaultAgg metric set to NATIVE");
|
||||||
return AggOption.OUTER;
|
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;
|
return AggOption.DEFAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user