use of com.alibaba.maxgraph.compiler.tree.UnaryTreeNode 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.UnaryTreeNode 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.UnaryTreeNode 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;
}
use of com.alibaba.maxgraph.compiler.tree.UnaryTreeNode in project GraphScope by alibaba.
the class TreeNodeUtils method buildFilterTreeNode.
/**
* Build logical plan with filter tree node and output the input value
*/
public static LogicalVertex buildFilterTreeNode(TreeNode treeNode, ContextManager contextManager, LogicalQueryPlan logicalQueryPlan, LogicalVertex sourceVertex, GraphSchema schema) {
TreeNodeLabelManager labelManager = contextManager.getTreeNodeLabelManager();
VertexIdManager vertexIdManager = contextManager.getVertexIdManager();
TreeNode filterTreeNode = TreeNodeUtils.optimizeSubFilterNode(treeNode);
UnaryTreeNode unaryTreeNode = UnaryTreeNode.class.cast(filterTreeNode);
LogicalVertex outputVertex;
if (unaryTreeNode.getInputNode() instanceof SourceTreeNode && (unaryTreeNode instanceof SelectOneTreeNode || unaryTreeNode instanceof PropertyNode)) {
// optimize traversal filter to filter operator
int propId;
if (unaryTreeNode instanceof SelectOneTreeNode) {
propId = labelManager.getLabelIndex(SelectOneTreeNode.class.cast(unaryTreeNode).getSelectLabel());
} else {
propId = SchemaUtils.getPropId(PropertyNode.class.cast(unaryTreeNode).getPropKeyList().iterator().next(), schema);
}
Message.LogicalCompare logicalCompare = Message.LogicalCompare.newBuilder().setCompare(Message.CompareType.EXIST).setPropId(propId).build();
ProcessorFilterFunction processorFunction = new ProcessorFilterFunction(QueryFlowOuterClass.OperatorType.HAS);
if (propId < 0) {
processorFunction.getUsedLabelList().add(propId);
}
processorFunction.getLogicalCompareList().add(logicalCompare);
outputVertex = new LogicalUnaryVertex(vertexIdManager.getId(), processorFunction, false, sourceVertex);
logicalQueryPlan.addLogicalVertex(outputVertex);
logicalQueryPlan.addLogicalEdge(sourceVertex, outputVertex, new LogicalEdge());
} else {
TreeNode currentFilterTreeNode = TreeNodeUtils.buildSingleOutputNode(filterTreeNode, schema);
// build filter plan, and use join direct filter vertex to filter left stream
LogicalSubQueryPlan filterPlan = TreeNodeUtils.buildSubQueryPlan(currentFilterTreeNode, sourceVertex, contextManager);
TreeNode filterSourceNode = TreeNodeUtils.getSourceTreeNode(currentFilterTreeNode);
sourceVertex = filterSourceNode.getOutputVertex();
LogicalVertex rightVertex = filterPlan.getOutputVertex();
logicalQueryPlan.mergeLogicalQueryPlan(filterPlan);
if (TreeNodeUtils.checkJoinSourceFlag(currentFilterTreeNode)) {
LogicalBinaryVertex filterJoinVertex = new LogicalBinaryVertex(vertexIdManager.getId(), new ProcessorFunction(QueryFlowOuterClass.OperatorType.JOIN_DIRECT_FILTER), false, sourceVertex, rightVertex);
logicalQueryPlan.addLogicalVertex(filterJoinVertex);
logicalQueryPlan.addLogicalEdge(sourceVertex, filterJoinVertex, new LogicalEdge());
logicalQueryPlan.addLogicalEdge(rightVertex, filterJoinVertex, new LogicalEdge());
outputVertex = filterJoinVertex;
} else if (TreeNodeUtils.checkSelectFlag(currentFilterTreeNode)) {
String inputLabel = labelManager.createSysLabelStart(sourceVertex, "input");
ProcessorFunction selectFunction = createSelectOneFunction(inputLabel, Pop.last, labelManager.getLabelIndexList());
LogicalUnaryVertex selectVertex = new LogicalUnaryVertex(vertexIdManager.getId(), selectFunction, false, rightVertex);
logicalQueryPlan.addLogicalVertex(selectVertex);
logicalQueryPlan.addLogicalEdge(rightVertex, selectVertex, new LogicalEdge());
outputVertex = logicalQueryPlan.getOutputVertex();
} else {
outputVertex = logicalQueryPlan.getOutputVertex();
}
}
return outputVertex;
}
use of com.alibaba.maxgraph.compiler.tree.UnaryTreeNode in project GraphScope by alibaba.
the class MaxGraphLimitStopStrategy method apply.
@Override
public void apply(TreeManager treeManager) {
TreeNode currNode = treeManager.getTreeLeaf();
boolean limitStopFlag = false;
while (currNode instanceof UnaryTreeNode) {
if (currNode instanceof RangeGlobalTreeNode) {
RangeGlobalTreeNode rangeGlobalTreeNode = (RangeGlobalTreeNode) currNode;
if (rangeGlobalTreeNode.getLow() == 0) {
limitStopFlag = true;
}
break;
}
currNode = ((UnaryTreeNode) currNode).getInputNode();
}
if (limitStopFlag) {
treeManager.getQueryConfig().addProperty(CompilerConstant.QUERY_SCHEDULE_GRANULARITY, QueryFlowOuterClass.InputBatchLevel.VerySmall.name());
currNode.enableGlobalStop();
TreeNode node = ((UnaryTreeNode) currNode).getInputNode();
while (node instanceof UnaryTreeNode) {
node.enableGlobalFilter();
node = ((UnaryTreeNode) node).getInputNode();
}
node.enableGlobalFilter();
}
}
Aggregations