use of com.alibaba.maxgraph.compiler.tree.SelectTreeNode in project GraphScope by alibaba.
the class CostUtils method buildCostGraph.
public static CostGraph buildCostGraph(TreeNode leafNode, TreeNodeLabelManager treeNodeLabelManager) {
TreeNode currNode = leafNode;
CostMappingManager costMappingManager = new CostMappingManager();
CostGraph costGraph = new CostGraph(costMappingManager);
CostEstimate costEstimate = new CostEstimate();
while (currNode instanceof UnaryTreeNode) {
if (currNode instanceof SelectTreeNode) {
SelectTreeNode selectTreeNode = (SelectTreeNode) currNode;
Map<String, TreeNode> labelStartNodeList = selectTreeNode.getLabelTreeNodeList();
List<String> selectKeyList = selectTreeNode.getSelectKeyList();
Map<String, TreeNode> labelValueNodeList = selectTreeNode.getLabelValueTreeNodeList();
for (String label : selectKeyList) {
TreeNode labelValueNode = labelValueNodeList.get(label);
TreeNode labelStartNode = labelStartNodeList.get(label);
if (labelStartNode == null) {
continue;
}
double nodeSelfComputeCost = costEstimate.estimateComputeCost(labelStartNode, labelStartNode);
double startNodeNetworkCost = costEstimate.estimateNetworkCost(labelStartNode);
costMappingManager.addComputeCost(Pair.of(label, label), nodeSelfComputeCost);
costMappingManager.addValueNetworkCost(label, startNodeNetworkCost);
if (labelValueNode != null && !(labelValueNode instanceof SourceTreeNode)) {
costMappingManager.addComputeTree(label, labelValueNode);
double nodeValueComputeCost = costEstimate.estimateComputeCost(labelStartNode, labelValueNode);
double nodeValueNetworkCost = costEstimate.estimateNetworkCost(labelValueNode);
String labelValueTag = CostUtils.buildValueName(label);
costMappingManager.addValueParent(labelValueTag, label);
costMappingManager.addComputeCost(Pair.of(label, labelValueTag), nodeValueComputeCost);
costMappingManager.addValueNetworkCost(labelValueTag, nodeValueNetworkCost);
}
CostGraph currGraph = buildLabelCostGraph(label, labelStartNode, currNode, costMappingManager, labelValueNode != null && !(labelValueNode instanceof SourceTreeNode));
costGraph.mergeCostGraph(currGraph);
}
} else if (currNode instanceof SelectOneTreeNode) {
SelectOneTreeNode selectOneTreeNode = (SelectOneTreeNode) currNode;
String label = selectOneTreeNode.getSelectLabel();
TreeNode labelStartNode = selectOneTreeNode.getLabelStartTreeNode();
TreeNode labelValueNode = selectOneTreeNode.getTraversalTreeNode();
if (null == labelStartNode || null == labelValueNode) {
break;
}
double nodeSelfComputeCost = costEstimate.estimateComputeCost(labelStartNode, labelStartNode);
double startNodeNetworkCost = costEstimate.estimateNetworkCost(labelStartNode);
costMappingManager.addComputeCost(Pair.of(label, label), nodeSelfComputeCost);
costMappingManager.addValueNetworkCost(label, startNodeNetworkCost);
if (labelValueNode != null && !(labelValueNode instanceof SourceTreeNode)) {
costMappingManager.addComputeTree(label, labelValueNode);
double nodeValueComputeCost = costEstimate.estimateComputeCost(labelStartNode, labelValueNode);
double nodeValueNetworkCost = costEstimate.estimateNetworkCost(labelValueNode);
String labelValueTag = CostUtils.buildValueName(label);
costMappingManager.addValueParent(labelValueTag, label);
costMappingManager.addComputeCost(Pair.of(label, labelValueTag), nodeValueComputeCost);
costMappingManager.addValueNetworkCost(labelValueTag, nodeValueNetworkCost);
}
CostGraph currGraph = buildLabelCostGraph(label, labelStartNode, currNode, costMappingManager, labelValueNode != null && !(labelValueNode instanceof SourceTreeNode));
costGraph.mergeCostGraph(currGraph);
} else if (currNode instanceof WherePredicateTreeNode) {
WherePredicateTreeNode wherePredicateTreeNode = (WherePredicateTreeNode) currNode;
String startKey = wherePredicateTreeNode.getStartKey();
if (StringUtils.isNotEmpty(startKey)) {
CostGraph startKeyGraph = buildLabelCostGraph(wherePredicateTreeNode.getStartKey(), treeNodeLabelManager.getLastTreeNode(wherePredicateTreeNode.getStartKey()), currNode, costMappingManager, false);
costGraph.mergeCostGraph(startKeyGraph);
}
String predicateValue = wherePredicateTreeNode.getPredicateValue();
if (StringUtils.isNotEmpty(predicateValue)) {
List<TreeNode> labelNodeList = treeNodeLabelManager.getLabelTreeNodeList(predicateValue);
if (null != labelNodeList) {
TreeNode predicateNode = labelNodeList.get(labelNodeList.size() - 1);
if (null != predicateNode) {
CostGraph predicateGraph = buildLabelCostGraph(predicateValue, predicateNode, currNode, costMappingManager, false);
costGraph.mergeCostGraph(predicateGraph);
}
}
}
} else if (currNode instanceof RepeatTreeNode || currNode instanceof UnionTreeNode) {
costGraph.clear();
return costGraph;
}
currNode = ((UnaryTreeNode) currNode).getInputNode();
}
return costGraph;
}
Aggregations