mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-14 22:25:19 +00:00
[improvement][headless]Clean code logic of headless translator.
This commit is contained in:
@@ -14,7 +14,7 @@ import java.util.stream.Collectors;
|
||||
public class Ontology {
|
||||
|
||||
private List<Metric> metrics = new ArrayList<>();
|
||||
private Map<String, DataModel> datasourceMap = new HashMap<>();
|
||||
private Map<String, DataModel> dataModelMap = new HashMap<>();
|
||||
private Map<String, List<Dimension>> dimensionMap = new HashMap<>();
|
||||
private List<Materialization> materializationList = new ArrayList<>();
|
||||
private List<JoinRelation> joinRelations;
|
||||
@@ -26,7 +26,7 @@ public class Ontology {
|
||||
}
|
||||
|
||||
public Map<Long, DataModel> getModelMap() {
|
||||
return datasourceMap.values().stream()
|
||||
return dataModelMap.values().stream()
|
||||
.collect(Collectors.toMap(DataModel::getId, dataSource -> dataSource));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,11 +29,15 @@ public class S2CalciteSchema extends AbstractSchema {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Map<String, DataModel> getDatasource() {
|
||||
return ontology.getDatasourceMap();
|
||||
public Map<String, DataModel> getDataModels() {
|
||||
return ontology.getDataModelMap();
|
||||
}
|
||||
|
||||
public Map<String, List<Dimension>> getDimension() {
|
||||
public List<Metric> getMetrics() {
|
||||
return ontology.getMetrics();
|
||||
}
|
||||
|
||||
public Map<String, List<Dimension>> getDimensions() {
|
||||
return ontology.getDimensionMap();
|
||||
}
|
||||
|
||||
@@ -41,8 +45,4 @@ public class S2CalciteSchema extends AbstractSchema {
|
||||
return ontology.getJoinRelations();
|
||||
}
|
||||
|
||||
public List<Metric> getMetrics() {
|
||||
return ontology.getMetrics();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,8 +32,8 @@ import java.util.Objects;
|
||||
@Slf4j
|
||||
public class SqlBuilder {
|
||||
|
||||
private MetricQueryParam metricQueryParam;
|
||||
private final S2CalciteSchema schema;
|
||||
private MetricQueryParam metricQueryParam;
|
||||
private SqlValidatorScope scope;
|
||||
private SqlNode parserNode;
|
||||
private boolean isAgg = false;
|
||||
|
||||
@@ -202,7 +202,7 @@ public class DataModelNode extends SemanticNode {
|
||||
DataModel baseDataModel = null;
|
||||
// one , match measure count
|
||||
Map<String, Integer> dataSourceMeasures = new HashMap<>();
|
||||
for (Map.Entry<String, DataModel> entry : schema.getDatasource().entrySet()) {
|
||||
for (Map.Entry<String, DataModel> entry : schema.getDataModels().entrySet()) {
|
||||
Set<String> sourceMeasure = entry.getValue().getMeasures().stream()
|
||||
.map(mm -> mm.getName()).collect(Collectors.toSet());
|
||||
sourceMeasure.retainAll(measures);
|
||||
@@ -212,7 +212,7 @@ public class DataModelNode extends SemanticNode {
|
||||
Optional<Map.Entry<String, Integer>> base = dataSourceMeasures.entrySet().stream()
|
||||
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).findFirst();
|
||||
if (base.isPresent()) {
|
||||
baseDataModel = schema.getDatasource().get(base.get().getKey());
|
||||
baseDataModel = schema.getDataModels().get(base.get().getKey());
|
||||
dataModels.add(baseDataModel);
|
||||
}
|
||||
// second , check match all dimension and metric
|
||||
@@ -223,8 +223,8 @@ public class DataModelNode extends SemanticNode {
|
||||
Set<String> dimension = baseDataModel.getDimensions().stream().map(dd -> dd.getName())
|
||||
.collect(Collectors.toSet());
|
||||
baseDataModel.getIdentifiers().stream().forEach(i -> dimension.add(i.getName()));
|
||||
if (schema.getDimension().containsKey(baseDataModel.getName())) {
|
||||
schema.getDimension().get(baseDataModel.getName()).stream()
|
||||
if (schema.getDimensions().containsKey(baseDataModel.getName())) {
|
||||
schema.getDimensions().get(baseDataModel.getName()).stream()
|
||||
.forEach(d -> dimension.add(d.getName()));
|
||||
}
|
||||
filterMeasure.addAll(sourceMeasure);
|
||||
@@ -319,8 +319,8 @@ public class DataModelNode extends SemanticNode {
|
||||
}
|
||||
boolean isMatch = false;
|
||||
boolean isRight = before.contains(joinRelation.getLeft());
|
||||
DataModel other = isRight ? schema.getDatasource().get(joinRelation.getRight())
|
||||
: schema.getDatasource().get(joinRelation.getLeft());
|
||||
DataModel other = isRight ? schema.getDataModels().get(joinRelation.getRight())
|
||||
: schema.getDataModels().get(joinRelation.getLeft());
|
||||
if (!queryDimension.isEmpty()) {
|
||||
Set<String> linkDimension = other.getDimensions().stream()
|
||||
.map(dd -> dd.getName()).collect(Collectors.toSet());
|
||||
@@ -336,8 +336,8 @@ public class DataModelNode extends SemanticNode {
|
||||
if (!linkMeasure.isEmpty()) {
|
||||
isMatch = true;
|
||||
}
|
||||
if (!isMatch && schema.getDimension().containsKey(other.getName())) {
|
||||
Set<String> linkDimension = schema.getDimension().get(other.getName()).stream()
|
||||
if (!isMatch && schema.getDimensions().containsKey(other.getName())) {
|
||||
Set<String> linkDimension = schema.getDimensions().get(other.getName()).stream()
|
||||
.map(dd -> dd.getName()).collect(Collectors.toSet());
|
||||
linkDimension.retainAll(queryDimension);
|
||||
if (!linkDimension.isEmpty()) {
|
||||
@@ -362,7 +362,7 @@ public class DataModelNode extends SemanticNode {
|
||||
}
|
||||
}
|
||||
orders.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEach(d -> {
|
||||
linkDataModels.add(schema.getDatasource().get(d.getKey()));
|
||||
linkDataModels.add(schema.getDataModels().get(d.getKey()));
|
||||
});
|
||||
}
|
||||
return linkDataModels;
|
||||
@@ -388,7 +388,7 @@ public class DataModelNode extends SemanticNode {
|
||||
S2CalciteSchema schema) {
|
||||
Set<String> linkDataSourceName = new HashSet<>();
|
||||
List<DataModel> linkDataModels = new ArrayList<>();
|
||||
for (Map.Entry<String, DataModel> entry : schema.getDatasource().entrySet()) {
|
||||
for (Map.Entry<String, DataModel> entry : schema.getDataModels().entrySet()) {
|
||||
if (entry.getKey().equalsIgnoreCase(baseDataModel.getName())) {
|
||||
continue;
|
||||
}
|
||||
@@ -419,7 +419,7 @@ public class DataModelNode extends SemanticNode {
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Map.Entry<String, List<Dimension>> entry : schema.getDimension().entrySet()) {
|
||||
for (Map.Entry<String, List<Dimension>> entry : schema.getDimensions().entrySet()) {
|
||||
if (!queryDimension.isEmpty()) {
|
||||
Set<String> linkDimension = entry.getValue().stream().map(dd -> dd.getName())
|
||||
.collect(Collectors.toSet());
|
||||
@@ -430,7 +430,7 @@ public class DataModelNode extends SemanticNode {
|
||||
}
|
||||
}
|
||||
for (String linkName : linkDataSourceName) {
|
||||
linkDataModels.add(schema.getDatasource().get(linkName));
|
||||
linkDataModels.add(schema.getDataModels().get(linkName));
|
||||
}
|
||||
if (!CollectionUtils.isEmpty(linkDataModels)) {
|
||||
List<DataModel> all = new ArrayList<>();
|
||||
|
||||
@@ -241,8 +241,8 @@ public class JoinRender extends Renderer {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (schema.getDimension().containsKey(dataModel.getName())) {
|
||||
for (Dimension dim : schema.getDimension().get(dataModel.getName())) {
|
||||
if (schema.getDimensions().containsKey(dataModel.getName())) {
|
||||
for (Dimension dim : schema.getDimensions().get(dataModel.getName())) {
|
||||
if (dim.getName().equalsIgnoreCase(oriDimension)) {
|
||||
isAdd = true;
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ public class SourceRender extends Renderer {
|
||||
private static void buildDimension(String alias, String dimension, DataModel datasource,
|
||||
S2CalciteSchema schema, boolean nonAgg, Map<String, String> extendFields,
|
||||
TableView dataSet, TableView output, SqlValidatorScope scope) throws Exception {
|
||||
List<Dimension> dimensionList = schema.getDimension().get(datasource.getName());
|
||||
List<Dimension> dimensionList = schema.getDimensions().get(datasource.getName());
|
||||
EngineType engineType = EngineType.fromString(schema.getOntology().getDatabase().getType());
|
||||
boolean isAdd = false;
|
||||
if (!CollectionUtils.isEmpty(dimensionList)) {
|
||||
@@ -195,7 +195,7 @@ public class SourceRender extends Renderer {
|
||||
}
|
||||
}
|
||||
for (String where : fields) {
|
||||
List<Dimension> dimensionList = schema.getDimension().get(datasource.getName());
|
||||
List<Dimension> dimensionList = schema.getDimensions().get(datasource.getName());
|
||||
boolean isAdd = false;
|
||||
if (!CollectionUtils.isEmpty(dimensionList)) {
|
||||
for (Dimension dim : dimensionList) {
|
||||
@@ -262,8 +262,8 @@ public class SourceRender extends Renderer {
|
||||
dimensions.add(oriField);
|
||||
return;
|
||||
}
|
||||
if (schema.getDimension().containsKey(datasource.getName())) {
|
||||
Optional<Dimension> dataSourceDim = schema.getDimension().get(datasource.getName())
|
||||
if (schema.getDimensions().containsKey(datasource.getName())) {
|
||||
Optional<Dimension> dataSourceDim = schema.getDimensions().get(datasource.getName())
|
||||
.stream().filter(d -> d.getName().equalsIgnoreCase(field)).findFirst();
|
||||
if (dataSourceDim.isPresent()) {
|
||||
dimensions.add(oriField);
|
||||
@@ -300,8 +300,8 @@ public class SourceRender extends Renderer {
|
||||
if (identify.isPresent()) {
|
||||
return true;
|
||||
}
|
||||
if (schema.getDimension().containsKey(datasource.getName())) {
|
||||
Optional<Dimension> dataSourceDim = schema.getDimension().get(datasource.getName())
|
||||
if (schema.getDimensions().containsKey(datasource.getName())) {
|
||||
Optional<Dimension> dataSourceDim = schema.getDimensions().get(datasource.getName())
|
||||
.stream().filter(d -> d.getName().equalsIgnoreCase(name)).findFirst();
|
||||
if (dataSourceDim.isPresent()) {
|
||||
return true;
|
||||
|
||||
@@ -40,7 +40,7 @@ public class SqlVariableParseConverter implements QueryConverter {
|
||||
modelResp.getModelDetail().getSqlVariables(),
|
||||
queryStatement.getQueryParam().getParams());
|
||||
DataModel dataModel =
|
||||
queryStatement.getOntology().getDatasourceMap().get(modelResp.getBizName());
|
||||
queryStatement.getOntology().getDataModelMap().get(modelResp.getBizName());
|
||||
dataModel.setSqlQuery(sqlParsed);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user