Search in sources :

Example 1 with CountGlobalTreeNode

use of com.alibaba.maxgraph.compiler.tree.CountGlobalTreeNode 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;
}
Also used : VertexTreeNode(com.alibaba.maxgraph.compiler.tree.VertexTreeNode) HasTreeNode(com.alibaba.maxgraph.compiler.tree.HasTreeNode) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) VertexTreeNode(com.alibaba.maxgraph.compiler.tree.VertexTreeNode) MaxTreeNode(com.alibaba.maxgraph.compiler.tree.MaxTreeNode) RangeGlobalTreeNode(com.alibaba.maxgraph.compiler.tree.RangeGlobalTreeNode) FoldTreeNode(com.alibaba.maxgraph.compiler.tree.FoldTreeNode) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) TokenTreeNode(com.alibaba.maxgraph.compiler.tree.TokenTreeNode) MinTreeNode(com.alibaba.maxgraph.compiler.tree.MinTreeNode) CountGlobalTreeNode(com.alibaba.maxgraph.compiler.tree.CountGlobalTreeNode) HasTreeNode(com.alibaba.maxgraph.compiler.tree.HasTreeNode) SelectOneTreeNode(com.alibaba.maxgraph.compiler.tree.SelectOneTreeNode) PropertyMapTreeNode(com.alibaba.maxgraph.compiler.tree.PropertyMapTreeNode) SumTreeNode(com.alibaba.maxgraph.compiler.tree.SumTreeNode) UnaryTreeNode(com.alibaba.maxgraph.compiler.tree.UnaryTreeNode) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) CountGlobalTreeNode(com.alibaba.maxgraph.compiler.tree.CountGlobalTreeNode) Direction(org.apache.tinkerpop.gremlin.structure.Direction) UnaryTreeNode(com.alibaba.maxgraph.compiler.tree.UnaryTreeNode) RangeGlobalTreeNode(com.alibaba.maxgraph.compiler.tree.RangeGlobalTreeNode)

Example 2 with CountGlobalTreeNode

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

the class TreeNodeUtils method buildSubQueryWithLabel.

/**
 * Build sub query plan, and save the result of sub plan to given label id
 *
 * @param treeNode The given tree node
 * @param sourceVertex The given source vertex
 * @param contextManager The given context manager
 * @return The result query plan and label id
 */
public static Pair<LogicalQueryPlan, Integer> buildSubQueryWithLabel(TreeNode treeNode, LogicalVertex sourceVertex, ContextManager contextManager) {
    boolean joinFlag = checkJoinSourceFlag(treeNode);
    List<TreeNode> treeNodeList = buildTreeNodeListFromLeaf(treeNode);
    LogicalQueryPlan queryPlan = new LogicalQueryPlan(contextManager);
    LogicalVertex originalVertex = null;
    LogicalVertex currentSourceVertex = sourceVertex;
    for (TreeNode currentNode : treeNodeList) {
        if (currentNode instanceof SourceDelegateNode) {
            queryPlan.addLogicalVertex(sourceVertex);
            if (joinFlag) {
                LogicalVertex enterKeyVertex = new LogicalUnaryVertex(contextManager.getVertexIdManager().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, contextManager.getTreeNodeLabelManager());
                queryPlan.addLogicalVertex(enterKeyVertex);
                queryPlan.addLogicalEdge(sourceVertex, enterKeyVertex, new LogicalEdge());
                currentSourceVertex = enterKeyVertex;
                originalVertex = enterKeyVertex;
            } else {
                currentNode.setFinishVertex(sourceVertex, contextManager.getTreeNodeLabelManager());
                originalVertex = sourceVertex;
            }
        } else {
            queryPlan.mergeLogicalQueryPlan(currentNode.buildLogicalQueryPlan(contextManager));
        }
    }
    checkNotNull(originalVertex, "original vertex can't be null");
    LogicalVertex valueVertex = queryPlan.getOutputVertex();
    String valueLabel = contextManager.getTreeNodeLabelManager().createSysLabelStart("val");
    int valueLabelId = contextManager.getTreeNodeLabelManager().getLabelIndex(valueLabel);
    if (joinFlag) {
        LogicalVertex joinVertex;
        if (treeNode instanceof CountGlobalTreeNode || treeNode instanceof SumTreeNode) {
            joinVertex = new LogicalBinaryVertex(contextManager.getVertexIdManager().getId(), new ProcessorFunction(QueryFlowOuterClass.OperatorType.JOIN_COUNT_LABEL, Message.Value.newBuilder().setIntValue(valueLabelId)), false, originalVertex, valueVertex);
        } else {
            joinVertex = new LogicalBinaryVertex(contextManager.getVertexIdManager().getId(), new ProcessorFunction(QueryFlowOuterClass.OperatorType.JOIN_LABEL, Message.Value.newBuilder().setIntValue(valueLabelId)), false, originalVertex, valueVertex);
        }
        queryPlan.addLogicalVertex(joinVertex);
        queryPlan.addLogicalEdge(originalVertex, joinVertex, LogicalEdge.shuffleByKey(0));
        queryPlan.addLogicalEdge(valueVertex, joinVertex, LogicalEdge.forwardEdge());
    } else {
        LogicalVertex originalOutputVertex = queryPlan.getTargetVertex(originalVertex);
        String originalLabel = contextManager.getTreeNodeLabelManager().createBeforeSysLabelStart(originalOutputVertex, "original");
        ProcessorFunction selectFunction = createSelectOneFunction(originalLabel, Pop.first, contextManager.getTreeNodeLabelManager().getLabelIndexList());
        LogicalVertex selectVertex = new LogicalUnaryVertex(contextManager.getVertexIdManager().getId(), selectFunction, valueVertex);
        selectVertex.getBeforeRequirementList().add(QueryFlowOuterClass.RequirementValue.newBuilder().setReqType(QueryFlowOuterClass.RequirementType.LABEL_START).setReqArgument(Message.Value.newBuilder().addIntValueList(valueLabelId)));
        queryPlan.addLogicalVertex(selectVertex);
        queryPlan.addLogicalEdge(valueVertex, selectVertex, LogicalEdge.forwardEdge());
    }
    return Pair.of(queryPlan, valueLabelId);
}
Also used : LogicalUnaryVertex(com.alibaba.maxgraph.compiler.logical.LogicalUnaryVertex) ProcessorFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction) LogicalEdge(com.alibaba.maxgraph.compiler.logical.LogicalEdge) SourceDelegateNode(com.alibaba.maxgraph.compiler.tree.source.SourceDelegateNode) CountGlobalTreeNode(com.alibaba.maxgraph.compiler.tree.CountGlobalTreeNode) LogicalBinaryVertex(com.alibaba.maxgraph.compiler.logical.LogicalBinaryVertex) LogicalVertex(com.alibaba.maxgraph.compiler.logical.LogicalVertex) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) VertexTreeNode(com.alibaba.maxgraph.compiler.tree.VertexTreeNode) MaxTreeNode(com.alibaba.maxgraph.compiler.tree.MaxTreeNode) RangeGlobalTreeNode(com.alibaba.maxgraph.compiler.tree.RangeGlobalTreeNode) FoldTreeNode(com.alibaba.maxgraph.compiler.tree.FoldTreeNode) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) TokenTreeNode(com.alibaba.maxgraph.compiler.tree.TokenTreeNode) MinTreeNode(com.alibaba.maxgraph.compiler.tree.MinTreeNode) CountGlobalTreeNode(com.alibaba.maxgraph.compiler.tree.CountGlobalTreeNode) HasTreeNode(com.alibaba.maxgraph.compiler.tree.HasTreeNode) SelectOneTreeNode(com.alibaba.maxgraph.compiler.tree.SelectOneTreeNode) PropertyMapTreeNode(com.alibaba.maxgraph.compiler.tree.PropertyMapTreeNode) SumTreeNode(com.alibaba.maxgraph.compiler.tree.SumTreeNode) UnaryTreeNode(com.alibaba.maxgraph.compiler.tree.UnaryTreeNode) LogicalQueryPlan(com.alibaba.maxgraph.compiler.logical.LogicalQueryPlan) SumTreeNode(com.alibaba.maxgraph.compiler.tree.SumTreeNode)

Aggregations

CountGlobalTreeNode (com.alibaba.maxgraph.compiler.tree.CountGlobalTreeNode)2 FoldTreeNode (com.alibaba.maxgraph.compiler.tree.FoldTreeNode)2 HasTreeNode (com.alibaba.maxgraph.compiler.tree.HasTreeNode)2 MaxTreeNode (com.alibaba.maxgraph.compiler.tree.MaxTreeNode)2 MinTreeNode (com.alibaba.maxgraph.compiler.tree.MinTreeNode)2 PropertyMapTreeNode (com.alibaba.maxgraph.compiler.tree.PropertyMapTreeNode)2 RangeGlobalTreeNode (com.alibaba.maxgraph.compiler.tree.RangeGlobalTreeNode)2 SelectOneTreeNode (com.alibaba.maxgraph.compiler.tree.SelectOneTreeNode)2 SumTreeNode (com.alibaba.maxgraph.compiler.tree.SumTreeNode)2 TokenTreeNode (com.alibaba.maxgraph.compiler.tree.TokenTreeNode)2 TreeNode (com.alibaba.maxgraph.compiler.tree.TreeNode)2 UnaryTreeNode (com.alibaba.maxgraph.compiler.tree.UnaryTreeNode)2 VertexTreeNode (com.alibaba.maxgraph.compiler.tree.VertexTreeNode)2 SourceTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode)2 LogicalBinaryVertex (com.alibaba.maxgraph.compiler.logical.LogicalBinaryVertex)1 LogicalEdge (com.alibaba.maxgraph.compiler.logical.LogicalEdge)1 LogicalQueryPlan (com.alibaba.maxgraph.compiler.logical.LogicalQueryPlan)1 LogicalUnaryVertex (com.alibaba.maxgraph.compiler.logical.LogicalUnaryVertex)1 LogicalVertex (com.alibaba.maxgraph.compiler.logical.LogicalVertex)1 ProcessorFunction (com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction)1