Unverified Commit e6aeea73 authored by aiwenmo's avatar aiwenmo Committed by GitHub

[Optimization-1753][core] Remove deprecated lineage code (#1758)

parent 63ea7d98
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.ca;
import org.dinky.plus.FlinkSqlPlus;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* CABuilder
*
* @author wenmo
* @since 2021/6/23 11:03
*/
@Deprecated
public class CABuilder {
public static List<TableCANode> getOneTableCAByStatement(String statement) {
List<TableCANode> tableCANodes = new ArrayList<>();
FlinkSqlPlus plus = FlinkSqlPlus.build();
List<TableCAResult> results = plus.generateTableCA(statement);
for (int j = 0; j < results.size(); j++) {
TableCAResult result = results.get(j);
TableCANode node = new TableCANode();
TableCA sinkTableCA = (TableCA) result.getSinkTableCA();
node.setName(sinkTableCA.getTableName());
List<TableCANode> children = new ArrayList<>();
for (int k = 0; k < result.getSourceTableCAS().size(); k++) {
children.add(new TableCANode(result.getSourceTableCAS().get(k).getTableName()));
}
node.setChildren(children);
tableCANodes.add(node);
}
return tableCANodes;
}
public static List<TableCANode> getOneTableColumnCAByStatement(String statement) {
List<TableCANode> tableCANodes = new ArrayList<>();
FlinkSqlPlus plus = FlinkSqlPlus.build();
int id = 1;
List<TableCAResult> results = plus.explainSqlTableColumnCA(statement);
for (int j = 0; j < results.size(); j++) {
TableCAResult result = results.get(j);
TableCA sinkTableCA = (TableCA) result.getSinkTableCA();
TableCANode node =
new TableCANode(id++, sinkTableCA.getTableName(), sinkTableCA.getFields());
List<TableCANode> children = new ArrayList<>();
for (int k = 0; k < result.getSourceTableCAS().size(); k++) {
TableCA tableCA = (TableCA) result.getSourceTableCAS().get(k);
children.add(new TableCANode(id++, tableCA.getTableName(), tableCA.getFields()));
}
node.setChildren(children);
tableCANodes.add(node);
}
return tableCANodes;
}
@Deprecated
public static List<ColumnCANode> getColumnCAByStatement(String statement) {
List<ColumnCANode> columnCANodes = new ArrayList<>();
FlinkSqlPlus plus = FlinkSqlPlus.build();
List<ColumnCAResult> columnCAResults = plus.explainSqlColumnCA(statement);
for (int j = 0; j < columnCAResults.size(); j++) {
ColumnCAResult result = columnCAResults.get(j);
List<Integer> sinkColumns = result.getSinkColumns();
for (int k = 0; k < sinkColumns.size(); k++) {
ColumnCA columnCA = (ColumnCA) result.getColumnCASMaps().get(sinkColumns.get(k));
ColumnCANode node = new ColumnCANode();
node.setName(columnCA.getAlias());
node.setType(columnCA.getType());
node.setTitle(columnCA.getAlias());
node.setOperation(columnCA.getOperation());
List<ColumnCANode> children = new ArrayList<>();
buildColumnCANodeChildren(
children, result, sinkColumns.get(k), columnCA.getOperation());
node.setChildren(children);
columnCANodes.add(node);
}
}
return columnCANodes;
}
private static void buildColumnCANodeChildren(
List<ColumnCANode> children,
ColumnCAResult result,
Integer columnId,
String operation) {
Set<NodeRel> columnCASRel = result.getColumnCASRel();
boolean hasChildren = false;
for (NodeRel nodeRel : columnCASRel) {
if (columnId.equals(nodeRel.getSufId())) {
ColumnCA childca = (ColumnCA) result.getColumnCASMaps().get(nodeRel.getPreId());
// operation =
// operation.replaceAll(childca.getAlias().replaceAll("\\$","\\\\$"),childca.getOperation());
operation =
operation.replaceAll(
childca.getAlias().replaceAll("\\)", ""), childca.getOperation());
buildColumnCANodeChildren(children, result, nodeRel.getPreId(), operation);
hasChildren = true;
}
}
if (!hasChildren) {
ColumnCA columnCA = (ColumnCA) result.getColumnCASMaps().get(columnId);
if (result.getSourColumns().contains(columnCA.getId())) {
ColumnCANode columnCANode = new ColumnCANode();
columnCANode.setName(columnCA.getName());
columnCANode.setTitle(columnCA.getName());
columnCANode.setOperation(operation);
children.add(columnCANode);
}
}
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.ca;
public interface CAGenerator {
void translate();
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.ca;
import java.util.List;
/**
* ColumnCA
*
* @author wenmo
* @since 2021/6/22
*/
public class ColumnCA implements ICA {
private Integer id;
private Integer tableId;
private List<Integer> parentId;
private String name;
private String alias;
private String operation;
private String columnName;
private String familyName;
private String type;
private String columnType;
private TableCA tableCA;
private String tableName;
public ColumnCA(
Integer id,
String name,
String alias,
String columnName,
String familyName,
String operation,
TableCA tableCA) {
this.id = id;
this.name = name;
this.alias = alias;
this.columnName = columnName;
this.familyName = familyName;
this.operation = operation;
this.tableCA = tableCA;
this.tableId = tableCA.getId();
this.tableName = tableCA.getName();
this.type = tableCA.getType();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<Integer> getParentId() {
return parentId;
}
public void setParentId(List<Integer> parentId) {
this.parentId = parentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAlias() {
return alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
public String getColumnName() {
return columnName;
}
public void setColumnName(String columnName) {
this.columnName = columnName;
}
public String getFamilyName() {
return familyName;
}
public void setFamilyName(String familyName) {
this.familyName = familyName;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public TableCA getTableCA() {
return tableCA;
}
public void setTableCA(TableCA tableCA) {
this.tableCA = tableCA;
}
public Integer getTableId() {
return tableId;
}
public void setTableId(Integer tableId) {
this.tableId = tableId;
}
public String getColumnType() {
return columnType;
}
public void setColumnType(String columnType) {
this.columnType = columnType;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.ca;
import java.io.Serializable;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
/**
* ColumnCANode
*
* @author wenmo
* @since 2021/6/23 11:03
*/
@Getter
@Setter
public class ColumnCANode implements Serializable {
private static final long serialVersionUID = 122624200268430762L;
private Integer id;
private Integer tableId;
private String name;
private String title;
private String value;
private String type;
private String operation;
private List<ColumnCANode> children;
public ColumnCANode() {}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.ca;
import org.dinky.explainer.lineage.LineageColumnGenerator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* ColumnCAResult
*
* @author wenmo
* @since 2021/6/22
*/
public class ColumnCAResult {
private List<TableCA> tableCAS;
private Map<Integer, ColumnCA> columnCASMaps;
private Set<NodeRel> columnCASRel;
private Set<NodeRel> columnCASRelChain;
private List<Integer> sinkColumns;
private List<Integer> sourColumns;
public ColumnCAResult(LineageColumnGenerator generator) {
this.tableCAS = generator.getTableCAS();
this.columnCASMaps = generator.getColumnCASMaps();
this.columnCASRel = generator.getColumnCASRel();
this.columnCASRelChain = generator.getColumnCASRelChain();
this.sinkColumns = generator.getSinkColumns();
this.sourColumns = generator.getSourceColumns();
}
public List<TableCA> getTableCAS() {
return tableCAS;
}
public void setTableCAS(List<TableCA> tableCAS) {
this.tableCAS = tableCAS;
}
public Map<Integer, ColumnCA> getColumnCASMaps() {
return columnCASMaps;
}
public void setColumnCASMaps(Map<Integer, ColumnCA> columnCASMaps) {
this.columnCASMaps = columnCASMaps;
}
public Set<NodeRel> getColumnCASRel() {
return columnCASRel;
}
public void setColumnCASRel(Set<NodeRel> columnCASRel) {
this.columnCASRel = columnCASRel;
}
public Set<NodeRel> getColumnCASRelChain() {
return columnCASRelChain;
}
public void setColumnCASRelChain(Set<NodeRel> columnCASRelChain) {
this.columnCASRelChain = columnCASRelChain;
}
public List<Integer> getSinkColumns() {
return sinkColumns;
}
public void setSinkColumns(List<Integer> sinkColumns) {
this.sinkColumns = sinkColumns;
}
public List<Integer> getSourColumns() {
return sourColumns;
}
public void setSourColumns(List<Integer> sourColumns) {
this.sourColumns = sourColumns;
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.ca;
public interface ICA {
Integer getId();
String getTableName();
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.ca;
import java.util.Objects;
/**
* NodeRel
*
* @author wenmo
* @since 2021/6/22
*/
public class NodeRel {
private Integer preId;
private Integer sufId;
public NodeRel(Integer preId, Integer sufId) {
this.preId = preId;
this.sufId = sufId;
}
public Integer getPreId() {
return preId;
}
public void setPreId(Integer preId) {
this.preId = preId;
}
public Integer getSufId() {
return sufId;
}
public void setSufId(Integer sufId) {
this.sufId = sufId;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
NodeRel nodeRel = (NodeRel) o;
return Objects.equals(preId, nodeRel.preId) && Objects.equals(sufId, nodeRel.sufId);
}
@Override
public int hashCode() {
return Objects.hash(preId, sufId);
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.ca;
import org.dinky.assertion.Asserts;
import org.dinky.explainer.trans.Field;
import org.dinky.explainer.trans.OperatorTrans;
import org.dinky.explainer.trans.SinkTrans;
import org.dinky.explainer.trans.SourceTrans;
import org.dinky.explainer.trans.Trans;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
/**
* TableCA
*
* @author wenmo
* @since 2021/6/22
*/
@Getter
@Setter
public class TableCA implements ICA {
private Integer id;
private Integer parentId;
private String name;
private String catalog;
private String database;
private String table;
private String type;
private List<String> fields;
private List<String> useFields;
private List<String> alias;
private Set<Integer> columnCAIds = new HashSet<>();
private Integer parallelism;
private static final TableCA EMPTY = new TableCA();
public TableCA() {}
public TableCA(SourceTrans trans) {
this.id = trans.getId();
this.parentId = trans.getParentId();
this.name = trans.getName();
this.catalog = trans.getCatalog();
this.database = trans.getDatabase();
this.table = trans.getTable();
this.fields = trans.getFields();
this.useFields = trans.getFields();
this.parallelism = trans.getParallelism();
this.type = trans.getPact();
}
public TableCA(SinkTrans trans) {
this.id = trans.getId();
this.parentId = trans.getParentId();
this.name = trans.getName();
this.catalog = trans.getCatalog();
this.database = trans.getDatabase();
this.table = trans.getTable();
this.fields = trans.getFields();
this.useFields = trans.getFields();
this.parallelism = trans.getParallelism();
this.type = trans.getPact();
}
public TableCA(OperatorTrans trans) {
List<String> tableList = trans.getTable();
this.id = trans.getId();
this.parentId = trans.getParentId();
List<Field> select = trans.getSelect();
List<String> fieldList = new ArrayList<>();
for (Field field : select) {
fieldList.add(field.getAlias());
}
this.fields = fieldList;
this.useFields = fieldList;
this.parallelism = trans.getParallelism();
this.type = trans.getPact();
if (tableList.size() > 0) {
String tableStr = tableList.get(0);
String[] strings = tableStr.split("\\.");
if (strings.length > 2) {
this.catalog = strings[0];
this.database = strings[1];
this.table = strings[2];
} else if (strings.length == 2) {
this.catalog = "default_catalog";
this.database = strings[0];
this.table = strings[1];
} else if (strings.length == 1) {
this.catalog = "default_catalog";
this.database = "default_database";
this.table = strings[0];
}
}
this.name = this.catalog + "." + this.database + "." + this.table;
}
public static TableCA build(Trans trans) {
if (trans instanceof SourceTrans) {
return new TableCA((SourceTrans) trans);
} else if (trans instanceof SinkTrans) {
return new TableCA((SinkTrans) trans);
} else if (trans instanceof OperatorTrans) {
OperatorTrans operatorTrans = (OperatorTrans) trans;
if (Asserts.isNotNullCollection(operatorTrans.getTable())) {
return new TableCA(operatorTrans);
} else {
return null;
}
} else {
return null;
}
}
@Override
public String toString() {
return "TableCA{"
+ "id="
+ id
+ ", parentId="
+ parentId
+ ", name='"
+ name
+ '\''
+ ", catalog='"
+ catalog
+ '\''
+ ", database='"
+ database
+ '\''
+ ", table='"
+ table
+ '\''
+ ", fields="
+ fields
+ ", useFields="
+ useFields
+ ", parallelism="
+ parallelism
+ '}';
}
@Override
public String getTableName() {
return this.table;
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.ca;
import org.dinky.assertion.Asserts;
import org.dinky.explainer.trans.Field;
import org.dinky.explainer.trans.OperatorTrans;
import org.dinky.explainer.trans.SinkTrans;
import org.dinky.explainer.trans.SourceTrans;
import org.dinky.explainer.trans.Trans;
import org.apache.commons.collections.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* TableCAGenerator
*
* @author wenmo
* @since 2021/6/22
*/
public class TableCAGenerator implements CAGenerator {
private List<Trans> transList;
private Map<Integer, Trans> transMaps;
private Set<Integer> parentIdSet;
private List<ICA> sourceTableCAS = new ArrayList<>();
private List<ICA> sinkTableCAS = new ArrayList<>();
private ICA sinkTableCA = null;
private String sinkTableName;
public TableCAGenerator(List<Trans> transList) {
this.transList = transList;
this.transMaps = new HashMap<>();
this.parentIdSet = new HashSet<>();
for (int i = 0; i < transList.size(); i++) {
this.transMaps.put(transList.get(i).getId(), transList.get(i));
if (transList.get(i).getParentId() != null) {
parentIdSet.add(transList.get(i).getParentId());
}
}
}
public static TableCAGenerator build(List<Trans> transList) {
return new TableCAGenerator(transList);
}
public TableCAResult getResult() {
return new TableCAResult(this);
}
@Override
public void translate() {
for (Trans trans : transList) {
if (trans instanceof SourceTrans) {
TableCA tableCA = TableCA.build(trans);
List<String> sourceFields = new ArrayList<>();
CollectionUtils.addAll(sourceFields, new Object[tableCA.getFields().size()]);
Collections.copy(sourceFields, tableCA.getFields());
for (int j = 0; j < sourceFields.size(); j++) {
buildTableCAFields(tableCA, tableCA.getParentId(), sourceFields.get(j));
}
this.sourceTableCAS.add(tableCA);
} else if (trans instanceof SinkTrans) {
TableCA tableCA = TableCA.build(trans);
this.sinkTableCA = tableCA;
this.sinkTableName = tableCA.getName();
this.sinkTableCAS.add(TableCA.build(trans));
}
}
}
public void translateOnlyTable() {
for (Trans trans : transList) {
if (trans instanceof SourceTrans) {
this.sourceTableCAS.add(new TableCA((SourceTrans) trans));
} else if (trans instanceof SinkTrans) {
TableCA tableCA = new TableCA((SinkTrans) trans);
this.sinkTableCA = tableCA;
this.sinkTableName = tableCA.getName();
}
}
}
private void buildTableCAFields(TableCA tableCA, Integer id, String field) {
if (transMaps.get(id) instanceof OperatorTrans) {
OperatorTrans trans = (OperatorTrans) transMaps.get(id);
searchSelectFields(tableCA, trans.getSelect(), field);
searchWhereFields(tableCA, trans.getWhere(), field);
if (Asserts.isNotNull(trans.getParentId())) {
buildTableCAFields(tableCA, trans.getParentId(), field);
}
}
}
private void searchSelectFields(TableCA tableCA, List<Field> selects, String field) {
if (Asserts.isNotNull(selects)) {
for (int i = 0; i < selects.size(); i++) {
List<String> fields = matchFields(selects.get(i).getFragment(), field);
/*
* if(tableCA.getFields().contains(field)){ tableCA.getFields().remove(field); }
*/
for (int j = 0; j < fields.size(); j++) {
if (!tableCA.getUseFields().contains(fields.get(j))) {
tableCA.getUseFields().add(fields.get(j));
}
}
}
}
}
private void searchWhereFields(TableCA tableCA, String wheres, String field) {
if (Asserts.isNotNull(wheres) && !"[]".equals(wheres)) {
List<String> fields = matchFields(wheres, field);
/*
* if(tableCA.getFields().contains(field)){ tableCA.getFields().remove(field); }
*/
for (int j = 0; j < fields.size(); j++) {
if (!tableCA.getUseFields().contains(fields.get(j))) {
tableCA.getUseFields().add(fields.get(j));
}
}
}
}
private List<String> matchFields(String fragement, String field) {
List<String> fields = new ArrayList<>();
String sign = "([^a-zA-Z0-9_]?)";
Pattern p = Pattern.compile(sign + field + sign);
Matcher m = p.matcher(fragement);
while (m.find()) {
fields.add(m.group(1).trim());
fields.add(fragement.trim());
}
if (fragement.equals(field)) {
fields.add(fragement.trim());
}
return fields;
}
public List<Trans> getTransList() {
return transList;
}
public Map<Integer, Trans> getTransMaps() {
return transMaps;
}
public Set<Integer> getParentIdSet() {
return parentIdSet;
}
public List<ICA> getSourceTableCAS() {
return sourceTableCAS;
}
public ICA getSinkTableCA() {
return sinkTableCA;
}
public String getSinkTableName() {
return sinkTableName;
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.ca;
import java.io.Serializable;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
/**
* TableCANode
*
* @author wenmo
* @since 2021/6/23 11:03
*/
@Getter
@Setter
public class TableCANode implements Serializable {
private static final long serialVersionUID = 356665302973181483L;
private String id;
private Integer tableId;
private String name;
private String title;
private String value;
private String type;
private Integer columnSize;
// private Tables tables;
private List<String> columns;
private List<TableCANode> children;
public TableCANode() {}
public TableCANode(String name) {
this.name = name;
}
public TableCANode(String name, String value) {
this.name = name;
this.value = value;
}
public TableCANode(Integer id, String name, List<String> columns) {
this.id = id.toString();
this.name = name;
this.title = name;
this.columnSize = columns.size();
this.columns = columns;
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.ca;
import java.util.List;
/**
* TableCAResult
*
* @author wenmo
* @since 2021/6/22
*/
public class TableCAResult {
private String sinkName;
private List<ICA> sourceTableCAS;
private ICA sinkTableCA;
public TableCAResult(TableCAGenerator generator) {
this.sourceTableCAS = generator.getSourceTableCAS();
this.sinkTableCA = generator.getSinkTableCA();
this.sinkName = generator.getSinkTableName();
}
public String getSinkName() {
return sinkName;
}
public void setSinkName(String sinkName) {
this.sinkName = sinkName;
}
public List<ICA> getSourceTableCAS() {
return sourceTableCAS;
}
public void setSourceTableCAS(List<ICA> sourceTableCAS) {
this.sourceTableCAS = sourceTableCAS;
}
public ICA getSinkTableCA() {
return sinkTableCA;
}
public void setSinkTableCA(ICA sinkTableCA) {
this.sinkTableCA = sinkTableCA;
}
}
......@@ -19,18 +19,14 @@
package org.dinky.explainer.lineage;
import org.dinky.explainer.ca.ColumnCAResult;
import org.dinky.explainer.ca.NodeRel;
import org.dinky.explainer.ca.TableCA;
import org.dinky.executor.Executor;
import org.dinky.explainer.Explainer;
import org.dinky.model.LineageRel;
import org.dinky.plus.FlinkSqlPlus;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* LineageBuilder
......@@ -40,117 +36,9 @@ import java.util.Set;
*/
public class LineageBuilder {
@Deprecated
public static LineageResult getLineage(String statement) {
return getLineage(statement, true);
}
@Deprecated
public static LineageResult getLineage(String statement, boolean statementSet) {
FlinkSqlPlus plus = FlinkSqlPlus.build(statementSet);
List<ColumnCAResult> columnCAResults = plus.explainSqlColumnCA(statement);
List<LineageTable> tables = new ArrayList<>();
List<LineageRelation> relations = new ArrayList<>();
int index = 0;
for (ColumnCAResult item : columnCAResults) {
for (TableCA tableCA : item.getTableCAS()) {
tables.add(LineageTable.build(tableCA));
}
Set<String> keySet = new HashSet<>();
for (NodeRel nodeRel : item.getColumnCASRelChain()) {
if (item.getColumnCASMaps().containsKey(nodeRel.getPreId())
&& item.getColumnCASMaps().containsKey(nodeRel.getSufId())
&& !item.getColumnCASMaps()
.get(nodeRel.getPreId())
.getTableId()
.equals(
item.getColumnCASMaps()
.get(nodeRel.getSufId())
.getTableId())) {
String key =
item.getColumnCASMaps().get(nodeRel.getPreId()).getTableId().toString()
+ "@"
+ item.getColumnCASMaps()
.get(nodeRel.getSufId())
.getTableId()
.toString()
+ "@"
+ item.getColumnCASMaps().get(nodeRel.getPreId()).getName()
+ "@"
+ item.getColumnCASMaps().get(nodeRel.getSufId()).getName();
// 去重
if (!keySet.contains(key)) {
index++;
relations.add(
LineageRelation.build(
index + "",
item.getColumnCASMaps()
.get(nodeRel.getPreId())
.getTableId()
.toString(),
item.getColumnCASMaps()
.get(nodeRel.getSufId())
.getTableId()
.toString(),
item.getColumnCASMaps().get(nodeRel.getPreId()).getName(),
item.getColumnCASMaps().get(nodeRel.getSufId()).getName()));
keySet.add(key);
}
}
}
}
// 获取重复表集合
List<List<LineageTable>> repeatTablesList = new ArrayList<>();
for (int i = 0; i < tables.size() - 1; i++) {
List<LineageTable> repeatTables = new ArrayList<>();
for (int j = i + 1; j < tables.size(); j++) {
if (tables.get(i).getName().equals(tables.get(j).getName())) {
repeatTables.add(tables.get(j));
}
}
if (repeatTables.size() > 0) {
repeatTables.add(tables.get(i));
repeatTablesList.add(repeatTables);
}
}
// 重复表合并
Map<String, String> correctTableIdMap = new HashMap<>();
for (List<LineageTable> tableList : repeatTablesList) {
LineageTable newTable = new LineageTable();
Set<String> columnKeySet = new HashSet<>();
for (LineageTable table : tableList) {
if (newTable.getId() == null || newTable.getName() == null) {
newTable.setId(table.getId());
newTable.setName(table.getName());
newTable.setColumns(new ArrayList<>());
}
for (LineageColumn column : table.getColumns()) {
String key = column.getName() + "@&" + column.getTitle();
if (!columnKeySet.contains(key)) {
newTable.getColumns().add(column);
columnKeySet.add(key);
}
}
correctTableIdMap.put(table.getId(), newTable.getId());
tables.remove(table);
}
tables.add(newTable);
}
// 关系中id重新指向
for (LineageRelation relation : relations) {
if (correctTableIdMap.containsKey(relation.getSrcTableId())) {
relation.setSrcTableId(correctTableIdMap.get(relation.getSrcTableId()));
}
if (correctTableIdMap.containsKey(relation.getTgtTableId())) {
relation.setTgtTableId(correctTableIdMap.get(relation.getTgtTableId()));
}
}
return LineageResult.build(tables, relations);
}
public static LineageResult getColumnLineageByLogicalPlan(String statement) {
FlinkSqlPlus plus = FlinkSqlPlus.build(false);
List<LineageRel> lineageRelList = plus.getLineage(statement);
Explainer explainer = new Explainer(Executor.build(), false);
List<LineageRel> lineageRelList = explainer.getLineage(statement);
List<LineageRelation> relations = new ArrayList<>();
Map<String, LineageTable> tableMap = new HashMap<>();
int tableIndex = 1;
......
......@@ -19,8 +19,6 @@
package org.dinky.explainer.lineage;
import org.dinky.explainer.ca.TableCA;
import java.util.ArrayList;
import java.util.List;
......@@ -48,18 +46,6 @@ public class LineageTable {
return new LineageTable(id, name);
}
public static LineageTable build(TableCA tableCA) {
LineageTable lineageTable = new LineageTable();
lineageTable.setId(tableCA.getId().toString());
lineageTable.setName(tableCA.getTableName());
List<LineageColumn> columnList = new ArrayList<>();
for (String columnName : tableCA.getFields()) {
columnList.add(LineageColumn.build(columnName, columnName));
}
lineageTable.setColumns(columnList);
return lineageTable;
}
public String getId() {
return id;
}
......
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.lineage;
import org.dinky.assertion.Asserts;
import org.dinky.explainer.ca.TableCA;
import org.dinky.explainer.trans.OperatorTrans;
import org.dinky.explainer.trans.SinkTrans;
import org.dinky.explainer.trans.SourceTrans;
import org.dinky.explainer.trans.Trans;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* LineageTableGenerator
*
* @author wenmo
* @since 2022/3/16 19:56
*/
public class LineageTableGenerator {
private Map<Integer, Trans> transMaps;
private List<TableCA> tables = new ArrayList<>();
public LineageTableGenerator() {}
public static LineageTableGenerator build(List<Trans> transList) {
LineageTableGenerator generator = new LineageTableGenerator();
Map<Integer, Trans> map = new HashMap<>();
for (Trans trans : transList) {
map.put(trans.getId(), trans);
}
generator.setTransMaps(map);
return generator;
}
public void translate() {
for (Map.Entry<Integer, Trans> entry : transMaps.entrySet()) {
if (entry.getValue() instanceof SourceTrans) {
tables.add(TableCA.build(entry.getValue()));
} else if (entry.getValue() instanceof SinkTrans) {
tables.add(TableCA.build(entry.getValue()));
} else if (entry.getValue() instanceof OperatorTrans) {
TableCA tableCA = TableCA.build(entry.getValue());
if (Asserts.isNotNull(tableCA)) {
tables.add(tableCA);
}
}
}
}
public Map<Integer, Trans> getTransMaps() {
return transMaps;
}
public void setTransMaps(Map<Integer, Trans> transMaps) {
this.transMaps = transMaps;
}
public List<TableCA> getTables() {
return tables;
}
public void setTables(List<TableCA> tables) {
this.tables = tables;
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.trans;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.fasterxml.jackson.databind.JsonNode;
/**
* AbstractTrans
*
* @author wenmo
* @since 2021/6/22
*/
public abstract class AbstractTrans {
protected Integer id;
protected Integer parentId;
protected String name;
protected String type;
protected String text;
protected Integer stage;
protected String contents;
protected String pact;
protected Integer parallelism;
protected List<Predecessor> predecessors;
public void build(JsonNode node) {
id = node.get("id").asInt();
text = node.toPrettyString();
stage = id;
type = matchType(node.get("type").asText());
name = matchType(node.get("type").asText());
pact = node.get("pact").asText();
contents = matchContents(node.get("contents").asText());
translate();
parallelism = node.get("parallelism").asInt();
predecessors = new ArrayList<>();
if (node.has("predecessors")) {
JsonNode predecessornodes = node.get("predecessors");
for (JsonNode predecessor : predecessornodes) {
predecessors.add(
new Predecessor(
predecessor.get("id").asInt(),
predecessor.get("ship_strategy").asText(),
predecessor.get("side").asText()));
}
}
}
abstract void translate();
public static String matchType(String str) {
Pattern p = Pattern.compile("(.*?)\\(");
Matcher m = p.matcher(str);
String type = null;
if (m.find()) {
type = m.group(0).replaceAll("\\(", "").trim();
} else {
type = str;
}
return type;
}
public static String matchPact(String str) {
Pattern p = Pattern.compile(": (.*?)$");
Matcher m = p.matcher(str);
String pact = null;
if (m.find()) {
pact = m.group(0).replaceAll(": ", "").trim();
} else {
pact = str;
}
return pact;
}
public static String matchContents(String str) {
Pattern p = Pattern.compile("\\((.*?)$");
Matcher m = p.matcher(str);
String contents = null;
if (m.find()) {
contents = m.group(0).replaceFirst("\\(", "").trim();
contents = contents.substring(0, contents.lastIndexOf(")"));
} else {
contents = str;
}
return contents;
}
public static String matchStage(String str) {
Pattern p = Pattern.compile("Stage (.*?) :");
Matcher m = p.matcher(str);
String type = null;
if (m.find()) {
type = m.group(0).replaceFirst("Stage ", "").replaceFirst(" :", "").trim();
}
return type;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Integer getStage() {
return stage;
}
public void setStage(Integer stage) {
this.stage = stage;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
public String getPact() {
return pact;
}
public void setPact(String pact) {
this.pact = pact;
}
public Integer getParallelism() {
return parallelism;
}
public void setParallelism(Integer parallelism) {
this.parallelism = parallelism;
}
public List<Predecessor> getPredecessors() {
return predecessors;
}
public void setPredecessors(List<Predecessor> predecessors) {
this.predecessors = predecessors;
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.trans;
/**
* Field
*
* @author wenmo
* @since 2021/6/22
*/
public class Field {
private String fragment;
private String alias;
public Field(String fragment) {
this.fragment = fragment;
this.alias = fragment;
}
public Field(String fragment, String alias) {
this.fragment = fragment;
this.alias = alias;
}
public String getFragment() {
return fragment;
}
public void setFragment(String fragment) {
this.fragment = fragment;
}
public String getAlias() {
return alias;
}
public void setAlias(String alias) {
this.alias = alias;
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.trans;
import org.dinky.utils.MapParseUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* OperatorTrans
*
* @author wenmo
* @since 2021/6/22
*/
public class OperatorTrans extends AbstractTrans implements Trans {
private List<Field> select;
private List<String> table;
private List<String> fields;
private List<String> joinType;
private List<String> lookup;
private String where;
private List<String> leftInputSpec;
private List<String> rightInputSpec;
public static final String TRANS_TYPE = "Operator";
private static final String FIELD_AS = " AS ";
public List<Field> getSelect() {
return select;
}
public List<String> getFields() {
return fields;
}
public List<String> getJoinType() {
return joinType;
}
public String getWhere() {
return where;
}
public List<String> getLeftInputSpec() {
return leftInputSpec;
}
public List<String> getRightInputSpec() {
return rightInputSpec;
}
public List<String> getTable() {
return table;
}
public List<String> getLookup() {
return lookup;
}
@Override
public String getHandle() {
return TRANS_TYPE;
}
@Override
public boolean canHandle(String pact) {
return TRANS_TYPE.equals(pact);
}
@Override
public void translate() {
name = pact;
Map map = MapParseUtils.parseForSelect(contents);
translateSelect((ArrayList<String>) map.get("select"));
fields = (ArrayList<String>) map.get("fields");
table = (ArrayList<String>) map.get("table");
joinType = (ArrayList<String>) map.get("joinType");
lookup = (ArrayList<String>) map.get("lookup");
where = map.containsKey("where") ? map.get("where").toString() : null;
leftInputSpec = (ArrayList<String>) map.get("leftInputSpec");
rightInputSpec = (ArrayList<String>) map.get("rightInputSpec");
}
private void translateSelect(ArrayList<String> fieldStrs) {
if (fieldStrs != null && fieldStrs.size() > 0) {
select = new ArrayList<>();
for (int i = 0; i < fieldStrs.size(); i++) {
String fieldStr = fieldStrs.get(i).trim();
if (fieldStr.toUpperCase().contains(FIELD_AS)) {
String[] fieldNames = fieldStr.split(FIELD_AS);
if (fieldNames.length == 2) {
select.add(new Field(fieldNames[0].trim(), fieldNames[1].trim()));
} else if (fieldNames.length == 1) {
select.add(new Field(fieldNames[0].trim()));
} else {
List<String> fieldNameList = new ArrayList<>();
for (int j = 0; j < fieldNames.length - 1; j++) {
fieldNameList.add(fieldNames[j]);
}
select.add(
new Field(
StringUtils.join(fieldNameList, FIELD_AS).trim(),
fieldNames[fieldNames.length - 1].trim()));
}
} else {
select.add(new Field(fieldStr));
}
}
}
}
@Override
public String asSummaryString() {
return null;
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.trans;
import lombok.Getter;
import lombok.Setter;
/**
* Predecessor
*
* @author wenmo
* @since 2021/6/22
*/
@Getter
@Setter
public class Predecessor {
private Integer id;
private String shipStrategy;
private String side;
public Predecessor(Integer id, String shipStrategy, String side) {
this.id = id;
this.shipStrategy = shipStrategy;
this.side = side;
}
@Override
public String toString() {
return "Predecessor{"
+ "id="
+ id
+ ", shipStrategy='"
+ shipStrategy
+ '\''
+ ", side='"
+ side
+ '\''
+ '}';
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.trans;
import org.dinky.utils.MapParseUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* SinkTrans
*
* @author wenmo
* @since 2021/6/22
*/
public class SinkTrans extends AbstractTrans implements Trans {
private String catalog;
private String database;
private String table;
private List<String> fields;
public static final String TRANS_TYPE = "Data Sink";
public SinkTrans() {}
public String getCatalog() {
return catalog;
}
public void setCatalog(String catalog) {
this.catalog = catalog;
}
public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}
public String getTable() {
return table;
}
public void setTable(String table) {
this.table = table;
}
public List<String> getFields() {
return fields;
}
public void setFields(List<String> fields) {
this.fields = fields;
}
@Override
public String getHandle() {
return TRANS_TYPE;
}
@Override
public boolean canHandle(String pact) {
return TRANS_TYPE.equals(pact);
}
@Override
public void translate() {
Map map = MapParseUtils.parse(contents);
ArrayList<String> tables = (ArrayList<String>) map.get("table");
if (tables != null && tables.size() > 0) {
name = tables.get(0);
String[] names = tables.get(0).split("\\.");
if (names.length >= 3) {
catalog = names[0];
database = names[1];
table = names[2];
} else {
table = name;
}
}
fields = (ArrayList<String>) map.get("fields");
}
@Override
public String asSummaryString() {
return "";
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.trans;
import org.dinky.utils.MapParseUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.table.operations.Operation;
import org.apache.flink.table.operations.OperationUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* SourceTrans
*
* @author wenmo
* @since 2021/6/22
*/
public class SourceTrans extends AbstractTrans implements Trans {
private String catalog;
private String database;
private String table;
private List<String> project;
private List<String> fields;
public static final String TRANS_TYPE = "Data Source";
public SourceTrans() {}
public String getCatalog() {
return catalog;
}
public String getDatabase() {
return database;
}
public String getTable() {
return table;
}
public List<String> getProject() {
return project;
}
public List<String> getFields() {
return fields;
}
@Override
public String getHandle() {
return TRANS_TYPE;
}
@Override
public boolean canHandle(String pact) {
return TRANS_TYPE.equals(pact);
}
@Override
public String asSummaryString() {
Map<String, Object> params = new LinkedHashMap<>();
/*
* params.put("originalQuery", catalogView.getOriginalQuery()); params.put("expandedQuery",
* catalogView.getExpandedQuery()); params.put("identifier", viewIdentifier); params.put("ignoreIfExists",
* ignoreIfExists); params.put("isTemporary", isTemporary);
*/
return OperationUtils.formatWithChildren(
"CREATE VIEW", params, Collections.emptyList(), Operation::asSummaryString);
}
@Override
public void translate() {
Map map = MapParseUtils.parse(contents);
ArrayList<ArrayList<Object>> tables = (ArrayList<ArrayList<Object>>) map.get("table");
ArrayList<Object> names = tables.get(0);
if (names.size() == 4) {
project = (ArrayList<String>) ((Map) names.get(3)).get("project");
names.remove(3);
}
name = StringUtils.join(names, ".");
if (names.size() >= 3) {
catalog = names.get(0).toString();
database = names.get(1).toString();
table = names.get(2).toString();
} else {
table = name;
}
fields = (ArrayList<String>) map.get("fields");
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.trans;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
/**
* Trans
*
* @author wenmo
* @since 2021/6/22
*/
public interface Trans {
String getHandle();
boolean canHandle(String pact);
void build(JsonNode node);
void translate();
String asSummaryString();
Integer getId();
List<Predecessor> getPredecessors();
void setParentId(Integer parentId);
Integer getParentId();
String getPact();
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.explainer.trans;
import org.dinky.assertion.Asserts;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* TransGenerator
*
* @author wenmo
* @since 2021/6/22
*/
public class TransGenerator {
private ObjectNode plan;
public TransGenerator(ObjectNode plan) {
this.plan = plan;
}
public static Trans getTrans(String pact) {
switch (pact) {
case OperatorTrans.TRANS_TYPE:
return new OperatorTrans();
case SourceTrans.TRANS_TYPE:
return new SourceTrans();
case SinkTrans.TRANS_TYPE:
return new SinkTrans();
default:
return null;
}
}
public List<Trans> translateTrans() {
JsonNode nodes = plan.get("nodes");
List<Trans> nodeList = new ArrayList<>();
Map<Integer, Trans> nodemap = new HashMap<>();
for (JsonNode node : nodes) {
String pact = node.get("pact").asText();
Trans trans = getTrans(pact);
Asserts.checkNotNull(trans, "该转换无法被解析,原文如下:" + pact);
trans.build(node);
nodemap.put(trans.getId(), trans);
}
setParentId(nodemap);
for (Map.Entry<Integer, Trans> entry : nodemap.entrySet()) {
nodeList.add(entry.getValue());
}
return nodeList;
}
private void setParentId(Map<Integer, Trans> nodemap) {
for (Map.Entry<Integer, Trans> entry : nodemap.entrySet()) {
Trans trans = entry.getValue();
List<Predecessor> predecessors = trans.getPredecessors();
if (Asserts.isNullCollection(predecessors)) {
continue;
}
for (int i = 0; i < predecessors.size(); i++) {
nodemap.get(predecessors.get(i).getId()).setParentId(trans.getId());
}
}
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.plus;
import org.dinky.executor.Executor;
import org.dinky.explainer.Explainer;
import org.dinky.explainer.ca.ColumnCAResult;
import org.dinky.explainer.ca.TableCAResult;
import org.dinky.model.LineageRel;
import org.dinky.result.SqlExplainResult;
import org.apache.flink.runtime.rest.messages.JobPlanInfo;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* FlinkSqlPlus
*
* @author wenmo
* @since 2021/6/22
*/
public class FlinkSqlPlus {
private Executor executor;
private Explainer explainer;
private boolean statementSet;
public FlinkSqlPlus(Executor executor) {
this.executor = executor;
this.explainer = new Explainer(executor);
}
public FlinkSqlPlus(Executor executor, boolean statementSet) {
this.executor = executor;
this.explainer = new Explainer(executor, statementSet);
}
public static FlinkSqlPlus build() {
return build(true);
}
public static FlinkSqlPlus build(boolean statementSet) {
return new FlinkSqlPlus(Executor.build(), statementSet);
}
public List<SqlResult> executeSql(String sql) {
if (sql == null || "".equals(sql)) {
return new ArrayList<>();
}
String[] sqls = sql.split(";");
List<SqlResult> sqlResults = new ArrayList<>();
try {
for (int i = 0; i < sqls.length; i++) {
sqlResults.add(new SqlResult(executor.executeSql(sqls[i])));
}
} catch (Exception e) {
e.printStackTrace();
sqlResults.add(new SqlResult(false, e.getMessage()));
return sqlResults;
}
return sqlResults;
}
public SqlResult execute(String sql) {
if (sql == null || "".equals(sql)) {
return SqlResult.NULL;
}
try {
return new SqlResult(executor.executeSql(sql));
} catch (Exception e) {
return new SqlResult(false, e.getMessage());
}
}
public List<SqlExplainResult> explainSqlRecord(String statement) {
return explainer.explainSqlResult(statement);
}
public List<TableCAResult> explainSqlTableColumnCA(String statement) {
return explainer.explainSqlTableColumnCA(statement);
}
public List<TableCAResult> generateTableCA(String statement) {
return explainer.generateTableCA(statement);
}
@Deprecated
public List<ColumnCAResult> explainSqlColumnCA(String statement) {
return explainer.explainSqlColumnCA(statement);
}
public ObjectNode getStreamGraph(String statement) {
return executor.getStreamGraph(statement);
}
public JobPlanInfo getJobPlanInfo(String statement) {
return explainer.getJobPlanInfo(statement);
}
public List<LineageRel> getLineage(String statement) {
return explainer.getLineage(statement);
}
}
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.core;
import org.dinky.plus.FlinkSqlPlus;
import org.apache.flink.runtime.rest.messages.JobPlanInfo;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* FlinkSqlPlusTest
*
* @author wenmo
* @since 2021/6/23 10:37
*/
@Ignore
public class FlinkSqlPlusTest {
private static final Logger LOGGER = LoggerFactory.getLogger(FlinkSqlPlusTest.class);
@Ignore
@Test
public void getJobPlanInfo() {
String sql =
"jdbcconfig:='connector' = 'jdbc',\n"
+ " 'url' = 'jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false"
+ "&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true',\n"
+ " 'username'='dinky',\n"
+ " 'password'='dinky',;\n"
+ "create temporary function TOP2 as 'org.dinky.ud.udtaf.Top2';\n"
+ "CREATE TABLE student (\n"
+ " sid INT,\n"
+ " name STRING,\n"
+ " PRIMARY KEY (sid) NOT ENFORCED\n"
+ ") WITH (\n"
+ " ${jdbcconfig}\n"
+ " 'table-name' = 'student'\n"
+ ");\n"
+ "CREATE TABLE score (\n"
+ " cid INT,\n"
+ " sid INT,\n"
+ " cls STRING,\n"
+ " score INT,\n"
+ " PRIMARY KEY (cid) NOT ENFORCED\n"
+ ") WITH (\n"
+ " ${jdbcconfig}\n"
+ " 'table-name' = 'score'\n"
+ ");\n"
+ "CREATE TABLE scoretop2 (\n"
+ " cls STRING,\n"
+ " score INT,\n"
+ " `rank` INT,\n"
+ " PRIMARY KEY (cls,`rank`) NOT ENFORCED\n"
+ ") WITH (\n"
+ " ${jdbcconfig}\n"
+ " 'table-name' = 'scoretop2'\n"
+ ");\n"
+ "CREATE AGGTABLE aggscore AS \n"
+ "SELECT cls,score,rank\n"
+ "FROM score\n"
+ "GROUP BY cls\n"
+ "AGG BY TOP2(score) as (score,rank);\n"
+ "insert into scoretop2\n"
+ "select \n"
+ "b.cls,b.score,b.`rank`\n"
+ "from aggscore b";
FlinkSqlPlus plus = FlinkSqlPlus.build();
JobPlanInfo jobPlanInfo = plus.getJobPlanInfo(sql);
LOGGER.info(jobPlanInfo.getJsonPlan());
}
}
......@@ -193,7 +193,7 @@ const SimpleTaskForm: React.FC<UpdateFormProps> = (props) => {
bodyStyle={{padding: '32px 40px 48px'}}
destroyOnClose
title={isCreate ? '创建新作业' : ('重命名作业-' + formVals.name)}
open={updateModalVisible}
visible={updateModalVisible}
footer={renderFooter()}
onCancel={() => handleUpdateModalVisible()}
>
......
......@@ -94,7 +94,7 @@ const UpdateCatalogueForm: React.FC<UpdateFormProps> = (props) => {
bodyStyle={{padding: '32px 40px 48px'}}
destroyOnClose
title={isCreate ? '创建新目录' : ('重命名目录-' + formVals.name)}
open={updateModalVisible}
visible={updateModalVisible}
footer={renderFooter()}
onCancel={() => handleUpdateModalVisible()}
>
......
......@@ -59,7 +59,7 @@ const UploadModal: React.FC<UploadModalProps> = (props: any) => {
return (
<div>
<Modal title="上传文件"
open={visible}
visible={visible}
onOk={handleOk}
onCancel={onCancel}
maskClosable={false}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment