use of com.alibaba.maxgraph.compiler.tree.TreeNode in project GraphScope by alibaba.
the class CostUtils method buildLabelCostGraph.
private static CostGraph buildLabelCostGraph(String label, TreeNode startNode, TreeNode selectNode, CostMappingManager costMappingManager, boolean labelValueFlag) {
CostGraph costGraph = new CostGraph(costMappingManager);
TreeNode currNode = selectNode;
boolean foundFirstFlag = false;
while (currNode instanceof UnaryTreeNode) {
if (foundFirstFlag) {
costGraph.addFirstRow(new CostRow(Lists.newArrayList()));
currNode = ((UnaryTreeNode) currNode).getInputNode();
continue;
}
if (currNode == selectNode) {
if (labelValueFlag) {
costGraph.addFirstRow(new CostRow(Lists.newArrayList(new RowField(buildValueName(label)))));
} else {
costGraph.addFirstRow(new CostRow(Lists.newArrayList(new RowField(label))));
}
} else {
List<RowField> rowFieldList = currNode.getOutputNode() == selectNode ? Lists.newArrayList(new RowField(buildValueName(label))) : buildRowFieldList(label, labelValueFlag);
foundFirstFlag = currNode == startNode;
costGraph.addFirstRow(new CostRow(rowFieldList, foundFirstFlag));
}
currNode = ((UnaryTreeNode) currNode).getInputNode();
}
if (!foundFirstFlag) {
List<RowField> rowFieldList = buildRowFieldList(label, labelValueFlag);
costGraph.addFirstRow(new CostRow(rowFieldList, true));
} else {
costGraph.addFirstRow(new CostRow(Lists.newArrayList()));
}
return costGraph;
}
use of com.alibaba.maxgraph.compiler.tree.TreeNode 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;
}
use of com.alibaba.maxgraph.compiler.tree.TreeNode in project GraphScope by alibaba.
the class NodeLabelList method buildNodeLabel.
/**
* Build label list of current node from parent node, tree node and schema
*
* @param parentNodeLabel The given parent node
* @param treeNode The given tree node
* @param graphSchema The given graph schema
* @return The result node label list
*/
public static NodeLabelList buildNodeLabel(NodeLabelList parentNodeLabel, TreeNode treeNode, GraphSchema graphSchema) {
NodeLabelList nodeLabelList = new NodeLabelList();
if (null == parentNodeLabel) {
BaseTreeNode baseTreeNode = (BaseTreeNode) treeNode;
final Set<String> labelList = Sets.newHashSet();
baseTreeNode.getHasContainerList().forEach(v -> {
if (StringUtils.equals(v.getKey(), T.label.getAccessor())) {
if (v.getBiPredicate() instanceof Compare && v.getBiPredicate() == Compare.eq) {
labelList.add(v.getValue().toString());
} else if (v.getBiPredicate() instanceof Contains && v.getBiPredicate() == Contains.within) {
List<String> labelValueList = (List<String>) v.getValue();
labelList.addAll(labelValueList);
} else {
throw new IllegalArgumentException("Not support label compare " + v.toString());
}
}
});
if (treeNode instanceof SourceVertexTreeNode) {
List<String> vertexLabelList = graphSchema.getVertexList().stream().map(GraphElement::getLabel).collect(Collectors.toList());
Set<String> resultLabelList;
if (labelList.isEmpty()) {
resultLabelList = Sets.newHashSet(vertexLabelList);
} else {
resultLabelList = labelList.stream().filter(vertexLabelList::contains).collect(Collectors.toSet());
}
nodeLabelList.addVertexLabel(resultLabelList);
} else if (treeNode instanceof SourceEdgeTreeNode) {
List<String> edgeLabelList = graphSchema.getEdgeList().stream().map(GraphElement::getLabel).collect(Collectors.toList());
Set<String> resultLabelList;
if (labelList.isEmpty()) {
resultLabelList = Sets.newHashSet(edgeLabelList);
} else {
resultLabelList = labelList.stream().filter(edgeLabelList::contains).collect(Collectors.toSet());
}
nodeLabelList.addEdgeLabel(resultLabelList);
} else {
nodeLabelList.enableUnknown();
}
} else {
Set<String> parentVertexLabelList = Sets.newHashSet();
Set<String> parentEdgeLabelList = Sets.newHashSet();
if (parentNodeLabel.isUnknownFlag()) {
parentVertexLabelList.addAll(graphSchema.getVertexList().stream().map(GraphElement::getLabel).collect(Collectors.toList()));
parentEdgeLabelList.addAll(graphSchema.getEdgeList().stream().map(GraphElement::getLabel).collect(Collectors.toList()));
} else {
parentVertexLabelList.addAll(parentNodeLabel.getVertexLabelList());
parentEdgeLabelList.addAll(parentNodeLabel.getEdgeLabelList());
}
if (treeNode instanceof VertexTreeNode) {
VertexTreeNode vertexTreeNode = (VertexTreeNode) treeNode;
Direction direction = vertexTreeNode.getDirection();
nodeLabelList.addVertexLabel(computeVertexLabelList(parentVertexLabelList, direction, vertexTreeNode.getEdgeLabels(), graphSchema));
} else if (treeNode instanceof EdgeTreeNode) {
EdgeTreeNode edgeTreeNode = (EdgeTreeNode) treeNode;
Direction direction = edgeTreeNode.getDirection();
nodeLabelList.addEdgeLabel(computeEdgeLabelList(parentVertexLabelList, direction, edgeTreeNode.getEdgeLabels(), graphSchema));
} else if (treeNode instanceof EdgeVertexTreeNode) {
EdgeVertexTreeNode edgeVertexTreeNode = (EdgeVertexTreeNode) treeNode;
Map<String, Pair<Set<String>, Set<String>>> edgeSourceTargetPairList = getEdgeSourceTargetPairList(parentEdgeLabelList.toArray(new String[0]), graphSchema);
Direction direction = edgeVertexTreeNode.getDirection();
edgeSourceTargetPairList.forEach((key, value) -> {
switch(direction) {
case OUT:
{
nodeLabelList.addVertexLabel(value.getLeft());
break;
}
case IN:
{
nodeLabelList.addVertexLabel(value.getRight());
break;
}
case BOTH:
{
nodeLabelList.addVertexLabel(value.getLeft());
nodeLabelList.addVertexLabel(value.getRight());
break;
}
}
});
} else if (treeNode instanceof EdgeOtherVertexTreeNode) {
Map<String, Pair<Set<String>, Set<String>>> edgeSourceTargetPairList = getEdgeSourceTargetPairList(parentEdgeLabelList.toArray(new String[0]), graphSchema);
edgeSourceTargetPairList.forEach((key, value) -> {
nodeLabelList.addVertexLabel(value.getLeft());
nodeLabelList.addVertexLabel(value.getRight());
});
} else {
nodeLabelList.enableUnknown();
}
}
return nodeLabelList;
}
use of com.alibaba.maxgraph.compiler.tree.TreeNode in project GraphScope by alibaba.
the class TreeNodeUtils method buildSubQueryPlanWithKey.
/**
* @param treeNode The given tree node
* @param sourceVertex The source vertex
* @param treeNodeLabelManager The given label manager
* @param contextManager The context manager
* @param vertexIdManager The vertex id manager
* @return The sub query with enter key vertex
*/
public static LogicalSubQueryPlan buildSubQueryPlanWithKey(TreeNode treeNode, LogicalVertex sourceVertex, TreeNodeLabelManager treeNodeLabelManager, ContextManager contextManager, VertexIdManager vertexIdManager) {
List<TreeNode> treeNodeList = buildTreeNodeListFromLeaf(treeNode);
LogicalSubQueryPlan logicalSubQueryPlan = new LogicalSubQueryPlan(contextManager);
LogicalVertex currentSourceVertex = sourceVertex;
for (TreeNode currentNode : treeNodeList) {
if (currentNode instanceof AbstractUseKeyNode) {
((AbstractUseKeyNode) currentNode).enableUseKeyFlag(currentSourceVertex);
}
if (currentNode instanceof SourceDelegateNode) {
LogicalVertex enterKeyVertex = new LogicalUnaryVertex(vertexIdManager.getId(), new ProcessorFunction(QueryFlowOuterClass.OperatorType.ENTER_KEY, Message.Value.newBuilder().setPayload(QueryFlowOuterClass.EnterKeyArgumentProto.newBuilder().setEnterKeyType(QueryFlowOuterClass.EnterKeyTypeProto.KEY_SELF).setUniqFlag(true).build().toByteString())), false, currentSourceVertex);
currentNode.setFinishVertex(enterKeyVertex, treeNodeLabelManager);
logicalSubQueryPlan.addLogicalVertex(currentSourceVertex);
logicalSubQueryPlan.addLogicalVertex(enterKeyVertex);
logicalSubQueryPlan.addLogicalEdge(currentSourceVertex, enterKeyVertex, new LogicalEdge());
currentSourceVertex = enterKeyVertex;
}
logicalSubQueryPlan.mergeLogicalQueryPlan(currentNode.buildLogicalQueryPlan(contextManager));
}
return logicalSubQueryPlan;
}
use of com.alibaba.maxgraph.compiler.tree.TreeNode in project GraphScope by alibaba.
the class TreeNodeUtils method optimizeSubFilterNode.
/**
* Optimize filter node in TraversalFilterNode
*
* @param filterTreeNode The given filter node
* @return The optimized filter node
*/
public static TreeNode optimizeSubFilterNode(TreeNode filterTreeNode) {
TreeNode sourceNode = getSourceTreeNode(filterTreeNode);
UnaryTreeNode firstNode = (UnaryTreeNode) sourceNode.getOutputNode();
if (firstNode instanceof VertexTreeNode) {
VertexTreeNode vertexTreeNode = (VertexTreeNode) firstNode;
Direction direction = vertexTreeNode.getDirection();
if (direction == Direction.OUT) {
while (true) {
boolean optimizeFinish = true;
TreeNode outputNode = vertexTreeNode.getOutputNode();
if (null == outputNode) {
vertexTreeNode.enableCountFlag();
TreeNode hasTreeNode = new HasTreeNode(vertexTreeNode, Lists.newArrayList(new HasContainer("", P.gt(0L))), vertexTreeNode.getSchema());
hasTreeNode.setOutputNode(null);
break;
}
if (outputNode instanceof RangeGlobalTreeNode) {
TreeNode rangeOutputNode = outputNode.getOutputNode();
vertexTreeNode.setOutputNode(rangeOutputNode);
if (null != rangeOutputNode) {
((UnaryTreeNode) rangeOutputNode).setInputNode(vertexTreeNode);
}
optimizeFinish = false;
} else if (outputNode instanceof CountGlobalTreeNode) {
vertexTreeNode.enableCountFlag();
TreeNode rangeOutputNode = outputNode.getOutputNode();
vertexTreeNode.setOutputNode(rangeOutputNode);
if (null != rangeOutputNode) {
((UnaryTreeNode) rangeOutputNode).setInputNode(vertexTreeNode);
}
optimizeFinish = false;
}
if (optimizeFinish) {
break;
}
}
}
}
TreeNode currentFilterNode = sourceNode;
while (currentFilterNode.getOutputNode() != null) {
currentFilterNode = currentFilterNode.getOutputNode();
}
return currentFilterNode;
}
Aggregations