Search in sources :

Example 1 with SourceTreeNode

use of com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode 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 SourceTreeNode

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

the class OrTreeNode method buildLogicalQueryPlan.

@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
    TreeNodeLabelManager labelManager = contextManager.getTreeNodeLabelManager();
    VertexIdManager vertexIdManager = contextManager.getVertexIdManager();
    Message.LogicalCompare.Builder logicalCompareBuilder = Message.LogicalCompare.newBuilder().setCompare(Message.CompareType.OR_RELATION);
    List<TreeNode> otherTreeNodeList = Lists.newArrayList();
    boolean vertexFlag = getInputNode().getOutputValueType() instanceof VertexValueType;
    orTreeNodeList.forEach(v -> {
        if (v instanceof HasTreeNode && ((HasTreeNode) v).getInputNode() instanceof SourceTreeNode) {
            HasTreeNode hasTreeNode = HasTreeNode.class.cast(v);
            List<Message.LogicalCompare> logicalCompareList = Lists.newArrayList();
            for (HasContainer hasContainer : hasTreeNode.getHasContainerList()) {
                logicalCompareList.add(CompilerUtils.parseLogicalCompare(hasContainer, schema, labelManager.getLabelIndexList(), vertexFlag));
            }
            if (logicalCompareList.size() == 1) {
                logicalCompareBuilder.addChildCompareList(logicalCompareList.get(0));
            } else {
                Message.LogicalCompare andLogicalCompare = Message.LogicalCompare.newBuilder().setCompare(Message.CompareType.AND_RELATION).addAllChildCompareList(logicalCompareList).build();
                logicalCompareBuilder.addChildCompareList(andLogicalCompare);
            }
        } else {
            otherTreeNodeList.add(v);
        }
    });
    if (otherTreeNodeList.isEmpty()) {
        ProcessorFilterFunction filterFunction = new ProcessorFilterFunction(QueryFlowOuterClass.OperatorType.HAS);
        filterFunction.getLogicalCompareList().addAll(Lists.newArrayList(logicalCompareBuilder.build()));
        return parseSingleUnaryVertex(vertexIdManager, labelManager, filterFunction, contextManager);
    } else {
        throw new UnsupportedOperationException();
    }
}
Also used : VertexValueType(com.alibaba.maxgraph.compiler.tree.value.VertexValueType) Message(com.alibaba.maxgraph.Message) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) ProcessorFilterFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorFilterFunction) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) VertexIdManager(com.alibaba.maxgraph.compiler.logical.VertexIdManager)

Example 3 with SourceTreeNode

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

the class TraversalFilterTreeNode method buildLogicalQueryPlan.

@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
    TreeNodeLabelManager labelManager = contextManager.getTreeNodeLabelManager();
    VertexIdManager vertexIdManager = contextManager.getVertexIdManager();
    if (filterTreeNode instanceof SourceTreeNode) {
        throw new IllegalArgumentException();
    } else {
        LogicalVertex sourceVertex = getInputNode().getOutputVertex();
        LogicalSubQueryPlan logicalSubQueryPlan = new LogicalSubQueryPlan(contextManager);
        logicalSubQueryPlan.addLogicalVertex(sourceVertex);
        LogicalVertex outputVertex = TreeNodeUtils.buildFilterTreeNode(this.filterTreeNode, contextManager, logicalSubQueryPlan, sourceVertex, schema);
        addUsedLabelAndRequirement(outputVertex, labelManager);
        setFinishVertex(outputVertex, labelManager);
        return logicalSubQueryPlan;
    }
}
Also used : LogicalVertex(com.alibaba.maxgraph.compiler.logical.LogicalVertex) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) VertexIdManager(com.alibaba.maxgraph.compiler.logical.VertexIdManager) LogicalSubQueryPlan(com.alibaba.maxgraph.compiler.logical.LogicalSubQueryPlan)

Example 4 with SourceTreeNode

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

the class TreeBuilder method visitDedupGlobalStep.

private TreeNode visitDedupGlobalStep(DedupGlobalStep step, TreeNode prev) {
    Set<String> dedupLabelList = step.getScopeKeys();
    Traversal.Admin<?, ?> dedupTraversal = step.getLocalChildren().isEmpty() ? null : (Traversal.Admin<?, ?>) step.getLocalChildren().get(0);
    DedupGlobalTreeNode dedupGlobalTreeNode = new DedupGlobalTreeNode(prev, schema, dedupLabelList);
    if (null != dedupTraversal) {
        boolean saveFlag = rootPathFlag;
        rootPathFlag = false;
        dedupGlobalTreeNode.setDedupTreeNode(travelTraversalAdmin(dedupTraversal, new SourceDelegateNode(dedupGlobalTreeNode, schema)));
        rootPathFlag = saveFlag;
    }
    List<TreeNode> treeNodeList = Lists.reverse(TreeNodeUtils.buildTreeNodeListFromLeaf(dedupGlobalTreeNode));
    TreeNode sourceTreeNode = treeNodeList.get(treeNodeList.size() - 1);
    boolean subQueryNodeFlag = sourceTreeNode instanceof SourceDelegateNode;
    for (TreeNode treeNode : treeNodeList) {
        if (subQueryNodeFlag) {
            treeNode.setSubqueryNode();
        }
        treeNode.enableDedupLocal();
        if (treeNode instanceof SelectTreeNode || treeNode instanceof SelectOneTreeNode || (treeNode instanceof WherePredicateTreeNode && !treeNodeLabelManager.getLabelIndexList().containsKey(((WherePredicateTreeNode) treeNode).getStartKey()))) {
            break;
        }
    }
    return dedupGlobalTreeNode;
}
Also used : SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) EstimateCountTreeNode(com.alibaba.maxgraph.compiler.tree.source.EstimateCountTreeNode) SourceEdgeTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode) SourceVertexTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode) SourceCreateGraphTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceCreateGraphTreeNode) CustomAggregationListTraversal(com.alibaba.maxgraph.sdkcommon.compiler.custom.aggregate.CustomAggregationListTraversal) Traversal(org.apache.tinkerpop.gremlin.process.traversal.Traversal) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) SourceDelegateNode(com.alibaba.maxgraph.compiler.tree.source.SourceDelegateNode)

Example 5 with SourceTreeNode

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

the class TreeManager method optimizeOrderRange.

/**
 * Optimize order+range operator
 */
private void optimizeOrderRange() {
    TreeNode currentTreeNode = treeLeaf;
    while (!(currentTreeNode instanceof SourceTreeNode)) {
        if (currentTreeNode instanceof OrderGlobalTreeNode) {
            OrderGlobalTreeNode orderGlobalTreeNode = OrderGlobalTreeNode.class.cast(currentTreeNode);
            TreeNode orderInputNode = orderGlobalTreeNode.getInputNode();
            if (orderInputNode instanceof SourceVertexTreeNode && ((SourceVertexTreeNode) orderInputNode).getBeforeRequirementList().isEmpty() && ((SourceVertexTreeNode) orderInputNode).getAfterRequirementList().isEmpty() && orderGlobalTreeNode.isEmptyOrderNode() && !orderGlobalTreeNode.orderFlag && null != orderGlobalTreeNode.rangeLimit) {
                QueryFlowOuterClass.RangeLimit.Builder orderRangeBuilder = orderGlobalTreeNode.rangeLimit;
                orderInputNode.setRangeLimit(0, orderRangeBuilder.getRangeEnd(), true);
                ((SourceVertexTreeNode) orderInputNode).enablePartitionIdFlag();
                orderGlobalTreeNode.enablePartitionIdFlag();
            }
        }
        currentTreeNode = UnaryTreeNode.class.cast(currentTreeNode).getInputNode();
    }
}
Also used : SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) SourceVertexTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) SourceVertexTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode)

Aggregations

SourceTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode)20 SourceVertexTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode)10 SourceDelegateNode (com.alibaba.maxgraph.compiler.tree.source.SourceDelegateNode)7 TreeNode (com.alibaba.maxgraph.compiler.tree.TreeNode)6 PropertyNode (com.alibaba.maxgraph.compiler.tree.addition.PropertyNode)6 SourceEdgeTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode)6 Message (com.alibaba.maxgraph.Message)5 EstimateCountTreeNode (com.alibaba.maxgraph.compiler.tree.source.EstimateCountTreeNode)5 SourceCreateGraphTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceCreateGraphTreeNode)5 UnaryTreeNode (com.alibaba.maxgraph.compiler.tree.UnaryTreeNode)4 CustomAggregationListTraversal (com.alibaba.maxgraph.sdkcommon.compiler.custom.aggregate.CustomAggregationListTraversal)4 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)4 GraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal)4 GraphSchema (com.alibaba.maxgraph.compiler.api.schema.GraphSchema)3 VertexIdManager (com.alibaba.maxgraph.compiler.logical.VertexIdManager)3 ProcessorFunction (com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction)3 RangeGlobalTreeNode (com.alibaba.maxgraph.compiler.tree.RangeGlobalTreeNode)3 VertexTreeNode (com.alibaba.maxgraph.compiler.tree.VertexTreeNode)3 ValueValueType (com.alibaba.maxgraph.compiler.tree.value.ValueValueType)3 VertexValueType (com.alibaba.maxgraph.compiler.tree.value.VertexValueType)3