mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-11 12:07:42 +00:00
(improvement)(Headless) Fixed duplicate fields error (#1870)
This commit is contained in:
@@ -35,6 +35,7 @@ import org.apache.calcite.sql.validate.SqlValidator;
|
||||
import org.apache.calcite.sql.validate.SqlValidatorScope;
|
||||
import org.apache.calcite.sql.validate.SqlValidatorWithHints;
|
||||
import org.apache.calcite.sql2rel.SqlToRelConverter;
|
||||
import org.apache.calcite.util.Litmus;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
@@ -461,4 +462,27 @@ public abstract class SemanticNode {
|
||||
}
|
||||
return SqlLiteral.createSymbol(JoinType.INNER, SqlParserPos.ZERO);
|
||||
}
|
||||
|
||||
public static List<SqlNode> deduplicateNode(List<SqlNode> listNode) { // List<SqlNode>去重
|
||||
if (listNode == null) {
|
||||
return null;
|
||||
}
|
||||
List<SqlNode> uniqueElements = new ArrayList<>();
|
||||
for (SqlNode element : listNode) {
|
||||
if (!containsElement(uniqueElements, element)) {
|
||||
uniqueElements.add(element);
|
||||
}
|
||||
}
|
||||
return uniqueElements;
|
||||
}
|
||||
|
||||
private static boolean containsElement(List<SqlNode> list, SqlNode element) { // 检查List<SqlNode>中是否含有某element
|
||||
for (SqlNode i : list) {
|
||||
if (i.equalsDeep(element, Litmus.IGNORE)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -66,6 +66,8 @@ public class FilterRender extends Renderer {
|
||||
tableView.getMeasure().add(SemanticNode.parse(metric, scope, engineType));
|
||||
}
|
||||
}
|
||||
tableView.setMeasure(SemanticNode.deduplicateNode(tableView.getMeasure()));
|
||||
tableView.setDimension(SemanticNode.deduplicateNode(tableView.getDimension()));
|
||||
if (filterNode != null) {
|
||||
TableView filterView = new TableView();
|
||||
filterView.setTable(SemanticNode.buildAs(Constants.DATASOURCE_TABLE_FILTER_PREFIX,
|
||||
|
||||
@@ -138,6 +138,8 @@ public class JoinRender extends Renderer {
|
||||
}
|
||||
}
|
||||
}
|
||||
filterView.setMeasure(SemanticNode.deduplicateNode(filterView.getMeasure()));
|
||||
filterView.setDimension(SemanticNode.deduplicateNode(filterView.getDimension()));
|
||||
super.tableView = filterView;
|
||||
}
|
||||
|
||||
|
||||
@@ -94,8 +94,8 @@ public class SourceRender extends Renderer {
|
||||
datasource, schema, nonAgg, extendFields, dataSet, output, scope);
|
||||
}
|
||||
|
||||
output.setMeasure(deduplicateNode(output.getMeasure()));
|
||||
dataSet.setMeasure(deduplicateNode(dataSet.getMeasure()));
|
||||
output.setMeasure(SemanticNode.deduplicateNode(output.getMeasure()));
|
||||
dataSet.setMeasure(SemanticNode.deduplicateNode(dataSet.getMeasure()));
|
||||
|
||||
SqlNode tableNode = DataSourceNode.buildExtend(datasource, extendFields, scope);
|
||||
dataSet.setTable(tableNode);
|
||||
@@ -105,24 +105,7 @@ public class SourceRender extends Renderer {
|
||||
return output;
|
||||
}
|
||||
|
||||
private static List<SqlNode> deduplicateNode(List<SqlNode> listNode) { // List<SqlNode>去重
|
||||
List<SqlNode> uniqueElements = new ArrayList<>();
|
||||
for (SqlNode element : listNode) {
|
||||
if (!containsElement(uniqueElements, element)) {
|
||||
uniqueElements.add(element);
|
||||
}
|
||||
}
|
||||
return uniqueElements;
|
||||
}
|
||||
|
||||
private static boolean containsElement(List<SqlNode> list, SqlNode element) { // 检查List<SqlNode>中是否含有某element
|
||||
for (SqlNode i : list) {
|
||||
if (i.equalsDeep(element, Litmus.IGNORE)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void buildDimension(String alias, String dimension, DataSource datasource,
|
||||
SemanticSchema schema, boolean nonAgg, Map<String, String> extendFields,
|
||||
|
||||
@@ -149,9 +149,14 @@ public class QueryReqConverter {
|
||||
}
|
||||
|
||||
private AggOption getAggOption(QuerySqlReq databaseReq, List<MetricSchemaResp> metricSchemas) {
|
||||
String sql = databaseReq.getSql();
|
||||
if (!SqlSelectFunctionHelper.hasAggregateFunction(sql) && !SqlSelectHelper.hasGroupBy(sql)
|
||||
&& !SqlSelectHelper.hasWith(sql) && !SqlSelectHelper.hasSubSelect(sql)) {
|
||||
log.debug("getAggOption simple sql set to DEFAULT");
|
||||
return AggOption.DEFAULT;
|
||||
}
|
||||
// if there is no group by in S2SQL,set MetricTable's aggOption to "NATIVE"
|
||||
// if there is count() in S2SQL,set MetricTable's aggOption to "NATIVE"
|
||||
String sql = databaseReq.getSql();
|
||||
if (!SqlSelectFunctionHelper.hasAggregateFunction(sql)
|
||||
|| SqlSelectFunctionHelper.hasFunction(sql, "count")
|
||||
|| SqlSelectFunctionHelper.hasFunction(sql, "count_distinct")) {
|
||||
@@ -171,11 +176,6 @@ 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;
|
||||
}
|
||||
|
||||
@@ -185,6 +185,8 @@ public class QueryReqConverter {
|
||||
String sql = querySqlReq.getSql();
|
||||
log.debug("dataSetId:{},convert name to bizName before:{}", querySqlReq.getDataSetId(),
|
||||
sql);
|
||||
sql = SqlReplaceHelper.replaceSqlByPositions(sql);
|
||||
log.debug("replaceSqlByPositions:{}", sql);
|
||||
String replaceFields = SqlReplaceHelper.replaceFields(sql, fieldNameToBizNameMap, true);
|
||||
log.debug("dataSetId:{},convert name to bizName after:{}", querySqlReq.getDataSetId(),
|
||||
replaceFields);
|
||||
|
||||
Reference in New Issue
Block a user