Search in sources :

Example 1 with RepeatTreeNode

use of com.alibaba.maxgraph.compiler.tree.RepeatTreeNode 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;
}
Also used : WherePredicateTreeNode(com.alibaba.maxgraph.compiler.tree.WherePredicateTreeNode) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) RepeatTreeNode(com.alibaba.maxgraph.compiler.tree.RepeatTreeNode) UnaryTreeNode(com.alibaba.maxgraph.compiler.tree.UnaryTreeNode) UnionTreeNode(com.alibaba.maxgraph.compiler.tree.UnionTreeNode) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) SelectOneTreeNode(com.alibaba.maxgraph.compiler.tree.SelectOneTreeNode) WherePredicateTreeNode(com.alibaba.maxgraph.compiler.tree.WherePredicateTreeNode) RepeatTreeNode(com.alibaba.maxgraph.compiler.tree.RepeatTreeNode) UnaryTreeNode(com.alibaba.maxgraph.compiler.tree.UnaryTreeNode) SelectTreeNode(com.alibaba.maxgraph.compiler.tree.SelectTreeNode) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) UnionTreeNode(com.alibaba.maxgraph.compiler.tree.UnionTreeNode) SelectOneTreeNode(com.alibaba.maxgraph.compiler.tree.SelectOneTreeNode) SelectTreeNode(com.alibaba.maxgraph.compiler.tree.SelectTreeNode)

Example 2 with RepeatTreeNode

use of com.alibaba.maxgraph.compiler.tree.RepeatTreeNode in project GraphScope by alibaba.

the class DfsTraversal method buildDfsTree.

/**
 * Build bfs tree manager
 *
 * @param treeBuilder The tree builder
 * @param schema      The schema
 * @return The result tree managet
 */
public TreeManager buildDfsTree(TreeBuilder treeBuilder, GraphSchema schema) {
    TreeManager treeManager = treeBuilder.build(traversal);
    treeManager.optimizeTree();
    TreeNode currentNode = treeManager.getTreeLeaf();
    while (!(currentNode instanceof SourceTreeNode)) {
        if (currentNode.getNodeType() == NodeType.AGGREGATE) {
            throw new IllegalArgumentException("There's aggregate in the query and can'e be executed in bfs mode");
        }
        currentNode = UnaryTreeNode.class.cast(currentNode).getInputNode();
    }
    SourceDfsTreeNode sourceBfsTreeNode = new SourceDfsTreeNode(schema, batchSize);
    RepeatTreeNode repeatTreeNode = new RepeatTreeNode(sourceBfsTreeNode, schema, Maps.newHashMap());
    TreeNode sourceOutputNode = currentNode.getOutputNode();
    SourceDelegateNode repeatSourceTreeNode = new SourceDelegateNode(sourceBfsTreeNode, schema);
    DfsRepeatGraphTreeNode dfsRepeatGraphTreeNode = new DfsRepeatGraphTreeNode(repeatSourceTreeNode, SourceTreeNode.class.cast(currentNode), schema);
    if (null != sourceOutputNode) {
        UnaryTreeNode.class.cast(sourceOutputNode).setInputNode(dfsRepeatGraphTreeNode);
    } else {
        treeManager.setLeafNode(dfsRepeatGraphTreeNode);
    }
    TreeNode bodyLeafTreeNode = treeManager.getTreeLeaf();
    repeatTreeNode.setRepeatBodyTreeNode(bodyLeafTreeNode);
    repeatTreeNode.setEmitTreeNode(new SourceDelegateNode(bodyLeafTreeNode, schema));
    if (order) {
        OrderGlobalTreeNode orderTreeNode = new OrderGlobalTreeNode(new SourceDelegateNode(bodyLeafTreeNode, schema), schema, Lists.newArrayList(Pair.of(new SourceDelegateNode(bodyLeafTreeNode, schema), Order.asc)));
        repeatTreeNode.setDfsEmitTreeNode(orderTreeNode);
    }
    repeatTreeNode.setDfsFeedTreeNode(new DfsFinishTreeNode(new SourceDelegateNode(bodyLeafTreeNode, schema), schema, high));
    repeatTreeNode.setMaxLoopTimes(MAX_BFS_ITERATION_TIMES);
    RangeGlobalTreeNode rangeGlobalTreeNode = new RangeGlobalTreeNode(repeatTreeNode, schema, low, high);
    return new TreeManager(rangeGlobalTreeNode, schema, treeManager.getLabelManager(), treeManager.getQueryConfig());
}
Also used : SourceDfsTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceDfsTreeNode) DfsFinishTreeNode(com.alibaba.maxgraph.compiler.tree.DfsFinishTreeNode) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) SourceDfsTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceDfsTreeNode) DfsFinishTreeNode(com.alibaba.maxgraph.compiler.tree.DfsFinishTreeNode) RepeatTreeNode(com.alibaba.maxgraph.compiler.tree.RepeatTreeNode) OrderGlobalTreeNode(com.alibaba.maxgraph.compiler.tree.OrderGlobalTreeNode) RangeGlobalTreeNode(com.alibaba.maxgraph.compiler.tree.RangeGlobalTreeNode) UnaryTreeNode(com.alibaba.maxgraph.compiler.tree.UnaryTreeNode) DfsRepeatGraphTreeNode(com.alibaba.maxgraph.compiler.tree.DfsRepeatGraphTreeNode) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) RepeatTreeNode(com.alibaba.maxgraph.compiler.tree.RepeatTreeNode) TreeManager(com.alibaba.maxgraph.compiler.tree.TreeManager) SourceDelegateNode(com.alibaba.maxgraph.compiler.tree.source.SourceDelegateNode) OrderGlobalTreeNode(com.alibaba.maxgraph.compiler.tree.OrderGlobalTreeNode) DfsRepeatGraphTreeNode(com.alibaba.maxgraph.compiler.tree.DfsRepeatGraphTreeNode) UnaryTreeNode(com.alibaba.maxgraph.compiler.tree.UnaryTreeNode) RangeGlobalTreeNode(com.alibaba.maxgraph.compiler.tree.RangeGlobalTreeNode)

Example 3 with RepeatTreeNode

use of com.alibaba.maxgraph.compiler.tree.RepeatTreeNode in project GraphScope by alibaba.

the class LabelPushDownStrategy method apply.

@Override
public void apply(TreeManager treeManager) {
    while (true) {
        BaseTreeNode treeNode = (BaseTreeNode) TreeNodeUtils.getSourceTreeNode(treeManager.getTreeLeaf());
        boolean labelOptimizeFlag = false;
        while (true) {
            Map<QueryFlowOuterClass.RequirementType, Object> afterRequirementList = treeNode.getAfterRequirementList();
            BaseTreeNode outputNode = (BaseTreeNode) treeNode.getOutputNode();
            if (null != outputNode && !(outputNode instanceof RepeatTreeNode) && !(outputNode instanceof UnionTreeNode) && !(outputNode instanceof AndTreeNode) && !(outputNode instanceof WherePredicateTreeNode) && outputNode.getNodeType() != NodeType.AGGREGATE) {
                Set<String> labelList = (Set<String>) afterRequirementList.remove(QueryFlowOuterClass.RequirementType.LABEL_START);
                if (null != labelList) {
                    labelOptimizeFlag = true;
                    outputNode.getBeforeRequirementList().put(QueryFlowOuterClass.RequirementType.LABEL_START, labelList);
                }
            }
            if (outputNode == null) {
                break;
            }
            treeNode = outputNode;
        }
        if (!labelOptimizeFlag) {
            break;
        }
    }
}
Also used : WherePredicateTreeNode(com.alibaba.maxgraph.compiler.tree.WherePredicateTreeNode) Set(java.util.Set) UnionTreeNode(com.alibaba.maxgraph.compiler.tree.UnionTreeNode) AndTreeNode(com.alibaba.maxgraph.compiler.tree.AndTreeNode) RepeatTreeNode(com.alibaba.maxgraph.compiler.tree.RepeatTreeNode) BaseTreeNode(com.alibaba.maxgraph.compiler.tree.BaseTreeNode)

Aggregations

RepeatTreeNode (com.alibaba.maxgraph.compiler.tree.RepeatTreeNode)3 TreeNode (com.alibaba.maxgraph.compiler.tree.TreeNode)2 UnaryTreeNode (com.alibaba.maxgraph.compiler.tree.UnaryTreeNode)2 UnionTreeNode (com.alibaba.maxgraph.compiler.tree.UnionTreeNode)2 WherePredicateTreeNode (com.alibaba.maxgraph.compiler.tree.WherePredicateTreeNode)2 SourceTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode)2 AndTreeNode (com.alibaba.maxgraph.compiler.tree.AndTreeNode)1 BaseTreeNode (com.alibaba.maxgraph.compiler.tree.BaseTreeNode)1 DfsFinishTreeNode (com.alibaba.maxgraph.compiler.tree.DfsFinishTreeNode)1 DfsRepeatGraphTreeNode (com.alibaba.maxgraph.compiler.tree.DfsRepeatGraphTreeNode)1 OrderGlobalTreeNode (com.alibaba.maxgraph.compiler.tree.OrderGlobalTreeNode)1 RangeGlobalTreeNode (com.alibaba.maxgraph.compiler.tree.RangeGlobalTreeNode)1 SelectOneTreeNode (com.alibaba.maxgraph.compiler.tree.SelectOneTreeNode)1 SelectTreeNode (com.alibaba.maxgraph.compiler.tree.SelectTreeNode)1 TreeManager (com.alibaba.maxgraph.compiler.tree.TreeManager)1 SourceDelegateNode (com.alibaba.maxgraph.compiler.tree.source.SourceDelegateNode)1 SourceDfsTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceDfsTreeNode)1 Set (java.util.Set)1