Search in sources :

Example 6 with LogicalQueryPlan

use of com.alibaba.maxgraph.compiler.logical.LogicalQueryPlan in project GraphScope by alibaba.

the class TraversalFlatMapTreeNode method buildLogicalQueryPlan.

@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
    TreeNodeLabelManager treeNodeLabelManager = contextManager.getTreeNodeLabelManager();
    LogicalVertex inputVertex = getInputNode().getOutputVertex();
    LogicalSubQueryPlan logicalQueryPlan = new LogicalSubQueryPlan(contextManager);
    logicalQueryPlan.addLogicalVertex(inputVertex);
    LogicalQueryPlan flatMapPlan = TreeNodeUtils.buildSubQueryPlan(flatMapNode, inputVertex, contextManager);
    logicalQueryPlan.mergeLogicalQueryPlan(flatMapPlan);
    LogicalVertex flatOutputVertex = logicalQueryPlan.getOutputVertex();
    LogicalVertex inputTargetVertex = flatMapPlan.getTargetVertex(inputVertex);
    if (inputTargetVertex.getProcessorFunction().getOperatorType() == QueryFlowOuterClass.OperatorType.ENTER_KEY && !(flatOutputVertex instanceof LogicalBinaryVertex)) {
        flatOutputVertex.getAfterRequirementList().add(QueryFlowOuterClass.RequirementValue.newBuilder().setReqType(QueryFlowOuterClass.RequirementType.KEY_DEL));
    }
    addUsedLabelAndRequirement(flatOutputVertex, treeNodeLabelManager);
    setFinishVertex(flatOutputVertex, treeNodeLabelManager);
    return logicalQueryPlan;
}
Also used : LogicalVertex(com.alibaba.maxgraph.compiler.logical.LogicalVertex) LogicalQueryPlan(com.alibaba.maxgraph.compiler.logical.LogicalQueryPlan) LogicalBinaryVertex(com.alibaba.maxgraph.compiler.logical.LogicalBinaryVertex) LogicalSubQueryPlan(com.alibaba.maxgraph.compiler.logical.LogicalSubQueryPlan)

Example 7 with LogicalQueryPlan

use of com.alibaba.maxgraph.compiler.logical.LogicalQueryPlan in project GraphScope by alibaba.

the class DfsRepeatGraphTreeNode method buildLogicalQueryPlan.

@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
    LogicalQueryPlan sourceSubPlan = sourceTreeNode.buildLogicalQueryPlan(contextManager);
    LogicalVertex sourceVertex = sourceSubPlan.getOutputVertex();
    ProcessorSourceFunction processorSourceFunction = ProcessorSourceFunction.class.cast(sourceVertex.getProcessorFunction());
    Message.Value.Builder argument = Message.Value.newBuilder().mergeFrom(processorSourceFunction.getArgumentBuilder().build()).setBoolValue(sourceTreeNode.getOutputValueType() instanceof VertexValueType);
    ProcessorFunction processorFunction = new ProcessorFunction(QueryFlowOuterClass.OperatorType.DFS_REPEAT_GRAPH, argument);
    processorFunction.getLogicalCompareList().addAll(processorSourceFunction.getLogicalCompareList());
    return parseSingleUnaryVertex(contextManager.getVertexIdManager(), contextManager.getTreeNodeLabelManager(), processorFunction, contextManager);
}
Also used : LogicalVertex(com.alibaba.maxgraph.compiler.logical.LogicalVertex) ProcessorFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction) VertexValueType(com.alibaba.maxgraph.compiler.tree.value.VertexValueType) LogicalQueryPlan(com.alibaba.maxgraph.compiler.logical.LogicalQueryPlan) ProcessorSourceFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorSourceFunction)

Example 8 with LogicalQueryPlan

use of com.alibaba.maxgraph.compiler.logical.LogicalQueryPlan in project GraphScope by alibaba.

the class QueryFlowBuilder method buildQueryFlow.

private void buildQueryFlow(QueryPlan.Builder queryPlanBuilder, LogicalQueryPlan queryPlan, long snapshot) {
    List<LogicalVertex> logicalVertexList = queryPlan.getLogicalVertexList();
    Map<Integer, Pair<Integer, Integer>> dfsGraphVertexMapping = Maps.newHashMap();
    for (LogicalVertex logicalVertex : logicalVertexList) {
        ProcessorFunction processorFunction = logicalVertex.getProcessorFunction();
        if (processorFunction instanceof ProcessorRepeatFunction) {
            ProcessorRepeatFunction processorRepeatFunction = ProcessorRepeatFunction.class.cast(processorFunction);
            QueryFlowBuilder repeatBuilder = new QueryFlowBuilder();
            QueryFlow queryFlow = repeatBuilder.buildQueryFlow(processorRepeatFunction.getRepeatPlan(), snapshot).build();
            RepeatArgumentProto.Builder repeatArgument = RepeatArgumentProto.newBuilder().setPlan(queryFlow.getQueryPlan()).setLoopLimit((int) processorRepeatFunction.getMaxLoopTimes()).setFeedbackId(processorRepeatFunction.getFeedbackVertex().getId()).setEmitFlag(processorRepeatFunction.isHasEmitFlag());
            if (processorRepeatFunction.getLeaveVertex() != null) {
                repeatArgument.setLeaveId(processorRepeatFunction.getLeaveVertex().getId());
            } else {
                repeatArgument.setLeaveId(-1);
            }
            logger.info("repeat argument => ");
            logger.info(TextFormat.printToString(repeatArgument));
            OperatorBase.Builder repeatBase = OperatorBase.newBuilder().setId(logicalVertex.getId()).setOperatorType(OperatorType.REPEAT).setArgument(Value.newBuilder().setPayload(repeatArgument.build().toByteString()));
            queryPlanBuilder.addUnaryOp(UnaryOperator.newBuilder().setBase(repeatBase).setInputOperatorId(queryPlan.getSourceVertex(logicalVertex).getId()));
            queryPlanBuilder.addOperatorIdList(logicalVertex.getId());
        } else if (processorFunction instanceof ProcessorChainFunction) {
            OperatorBase.Builder baseBuilder = OperatorBase.newBuilder();
            baseBuilder.setOperatorType(processorFunction.getOperatorType()).setId(logicalVertex.getId());
            if (null != logicalVertex.getEarlyStopArgument()) {
                baseBuilder.setEarlyStopArgument(logicalVertex.getEarlyStopArgument().build());
            }
            ProcessorChainFunction processorChainFunction = ProcessorChainFunction.class.cast(processorFunction);
            processorChainFunction.getChainVertexList().forEach(v -> baseBuilder.addChainedFunction(createChainedFunction(v)));
            if (logicalVertex instanceof LogicalChainSourceVertex) {
                LogicalSourceVertex logicalSourceVertex = ((LogicalChainSourceVertex) logicalVertex).getLogicalSourceVertex();
                baseBuilder.setArgument(logicalSourceVertex.getProcessorFunction().getArgumentBuilder()).addAllLogicalCompare(logicalSourceVertex.getProcessorFunction().getLogicalCompareList());
                SourceOperator.Builder sourceBuilder = SourceOperator.newBuilder().setBase(baseBuilder);
                queryPlanBuilder.setSourceOp(sourceBuilder);
            } else {
                LogicalChainUnaryVertex logicalUnaryVertex = (LogicalChainUnaryVertex) logicalVertex;
                UnaryOperator.Builder unaryOperator = UnaryOperator.newBuilder().setBase(baseBuilder).setInputOperatorId(parseInputOperatorId(queryPlan.getSourceVertex(logicalUnaryVertex).getId(), processorFunction.getOperatorType(), dfsGraphVertexMapping));
                LogicalEdge logicalEdge = queryPlan.getLogicalEdge(queryPlan.getSourceVertex(logicalUnaryVertex), logicalUnaryVertex);
                unaryOperator.setShuffleType(CompilerUtils.parseShuffleTypeFromEdge(logicalEdge)).setInputStreamIndex(logicalEdge.getStreamIndex());
                if (logicalEdge.getShuffleType() == EdgeShuffleType.SHUFFLE_BY_KEY) {
                    InputEdgeShuffle.Builder shuffleBuilder = InputEdgeShuffle.newBuilder().setShuffleType(InputShuffleType.SHUFFLE_BY_ID_TYPE);
                    if (logicalEdge.getShufflePropId() != 0) {
                        shuffleBuilder.setShuffleValue(logicalEdge.getShufflePropId());
                    }
                    unaryOperator.setInputShuffle(shuffleBuilder);
                }
                queryPlanBuilder.addUnaryOp(unaryOperator);
            }
            queryPlanBuilder.addOperatorIdList(logicalVertex.getId());
        } else if (processorFunction instanceof ProcessorLabelValueFunction) {
            ProcessorLabelValueFunction processorLabelValueFunction = (ProcessorLabelValueFunction) processorFunction;
            LogicalVertex labelValueVertex = processorLabelValueFunction.getLabelValueVertex();
            OperatorBase.Builder labelOperatorBuilder = createChainedFunction(labelValueVertex);
            int labelId = processorLabelValueFunction.getLabelId();
            OperatorBase.Builder baseBuilder = OperatorBase.newBuilder().setOperatorType(processorFunction.getOperatorType()).setId(logicalVertex.getId()).setArgument(Value.newBuilder().setIntValue(labelId).setBoolValue(processorLabelValueFunction.getRequireLabelFlag()).setPayload(labelOperatorBuilder.build().toByteString()));
            LogicalVertex inputVertex = queryPlan.getSourceVertex(logicalVertex);
            UnaryOperator.Builder unaryOperator = UnaryOperator.newBuilder().setBase(baseBuilder).setInputOperatorId(parseInputOperatorId(inputVertex.getId(), processorFunction.getOperatorType(), dfsGraphVertexMapping));
            LogicalEdge logicalEdge = queryPlan.getLogicalEdge(inputVertex, logicalVertex);
            unaryOperator.setShuffleType(CompilerUtils.parseShuffleTypeFromEdge(logicalEdge)).setInputStreamIndex(logicalEdge.getStreamIndex());
            queryPlanBuilder.addUnaryOp(unaryOperator).addOperatorIdList(logicalVertex.getId());
        } else {
            if (logicalVertex instanceof LogicalSourceDelegateVertex) {
                continue;
            }
            OperatorBase.Builder baseBuilder = OperatorBase.newBuilder();
            baseBuilder.setOperatorType(processorFunction.getOperatorType()).setId(logicalVertex.getId());
            if (processorFunction.getArgumentBuilder() != null) {
                baseBuilder.setArgument(processorFunction.getArgumentBuilder());
            }
            processorFunction.getLogicalCompareList().forEach(baseBuilder::addLogicalCompare);
            if (processorFunction.getRangeLimit() != null) {
                baseBuilder.setRangeLimit(processorFunction.getRangeLimit());
            }
            baseBuilder.addAllAfterRequirement(logicalVertex.getAfterRequirementList().stream().map(v -> v.build()).collect(Collectors.toList()));
            baseBuilder.addAllBeforeRequirement(logicalVertex.getBeforeRequirementList().stream().map(v -> v.build()).collect(Collectors.toList()));
            if (logicalVertex instanceof LogicalSourceVertex) {
                ProcessorSourceFunction processorSourceFunction = (ProcessorSourceFunction) logicalVertex.getProcessorFunction();
                SourceOperator.Builder sourceBuilder = SourceOperator.newBuilder().setBase(baseBuilder);
                if (null != processorSourceFunction.getOdpsQueryInput()) {
                    sourceBuilder.setOdpsInput(processorSourceFunction.getOdpsQueryInput()).setSourceType(SourceType.ODPS);
                } else {
                    sourceBuilder.setSourceType(SourceType.GRAPH);
                }
                queryPlanBuilder.setSourceOp(sourceBuilder);
            } else if (logicalVertex instanceof LogicalUnaryVertex) {
                EarlyStopArgument.Builder earlyStopArgument = logicalVertex.getEarlyStopArgument();
                if (null != earlyStopArgument && (earlyStopArgument.getGlobalFilterFlag() || earlyStopArgument.getGlobalStopFlag())) {
                    baseBuilder.setEarlyStopArgument(logicalVertex.getEarlyStopArgument().build());
                }
                LogicalUnaryVertex logicalUnaryVertex = LogicalUnaryVertex.class.cast(logicalVertex);
                UnaryOperator.Builder unaryOperator = UnaryOperator.newBuilder().setBase(baseBuilder).setInputOperatorId(parseInputOperatorId(queryPlan.getSourceVertex(logicalUnaryVertex).getId(), processorFunction.getOperatorType(), dfsGraphVertexMapping));
                LogicalEdge logicalEdge = queryPlan.getLogicalEdge(queryPlan.getSourceVertex(logicalUnaryVertex), logicalUnaryVertex);
                unaryOperator.setShuffleType(CompilerUtils.parseShuffleTypeFromEdge(logicalEdge)).setInputStreamIndex(logicalEdge.getStreamIndex());
                queryPlanBuilder.addUnaryOp(unaryOperator);
                if (processorFunction.getOperatorType() == OperatorType.DFS_REPEAT_GRAPH) {
                    VertexIdManager vertexIdManager = queryPlan.getPlanVertexIdManager();
                    int cmdLeftId = vertexIdManager.getId();
                    int dataRightId = vertexIdManager.getId();
                    dfsGraphVertexMapping.put(baseBuilder.getId(), Pair.of(cmdLeftId, dataRightId));
                    UnaryOperator.Builder cmdLeftOp = UnaryOperator.newBuilder().setBase(OperatorBase.newBuilder().setOperatorType(OperatorType.DFS_REPEAT_CMD).setId(cmdLeftId)).setInputOperatorId(baseBuilder.getId()).setShuffleType(InputShuffleType.FORWARD_TYPE);
                    queryPlanBuilder.addUnaryOp(cmdLeftOp);
                    UnaryOperator.Builder dataRightOp = UnaryOperator.newBuilder().setBase(OperatorBase.newBuilder().setOperatorType(OperatorType.DFS_REPEAT_DATA).setId(dataRightId)).setInputOperatorId(baseBuilder.getId()).setShuffleType(InputShuffleType.FORWARD_TYPE);
                    queryPlanBuilder.addUnaryOp(dataRightOp);
                }
            } else if (logicalVertex instanceof LogicalBinaryVertex) {
                LogicalBinaryVertex logicalBinaryVertex = LogicalBinaryVertex.class.cast(logicalVertex);
                BinaryOperator.Builder binaryOperator = BinaryOperator.newBuilder().setBase(baseBuilder).setLeftInputOperatorId(parseInputOperatorId(logicalBinaryVertex.getLeftInput().getId(), processorFunction.getOperatorType(), dfsGraphVertexMapping)).setRightInputOperatorId(parseInputOperatorId(logicalBinaryVertex.getRightInput().getId(), processorFunction.getOperatorType(), dfsGraphVertexMapping));
                LogicalEdge leftEdge = queryPlan.getLogicalEdge(logicalBinaryVertex.getLeftInput(), logicalBinaryVertex);
                LogicalEdge rightEdge = queryPlan.getLogicalEdge(logicalBinaryVertex.getRightInput(), logicalBinaryVertex);
                binaryOperator.setLeftShuffleType(CompilerUtils.parseShuffleTypeFromEdge(leftEdge)).setLeftStreamIndex(leftEdge.getStreamIndex()).setRightShuffleType(CompilerUtils.parseShuffleTypeFromEdge(rightEdge)).setRightStreamIndex(rightEdge.getStreamIndex());
                queryPlanBuilder.addBinaryOp(binaryOperator);
            } else {
                throw new IllegalArgumentException(logicalVertex.toString());
            }
            queryPlanBuilder.addOperatorIdList(logicalVertex.getId());
            Pair<Integer, Integer> dfsCmdPair = dfsGraphVertexMapping.get(logicalVertex.getId());
            if (null != dfsCmdPair) {
                queryPlanBuilder.addOperatorIdList(dfsCmdPair.getLeft());
                queryPlanBuilder.addOperatorIdList(dfsCmdPair.getRight());
            }
        }
    }
}
Also used : ProcessorLabelValueFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorLabelValueFunction) VertexIdManager(com.alibaba.maxgraph.compiler.logical.VertexIdManager) LogicalQueryPlan(com.alibaba.maxgraph.compiler.logical.LogicalQueryPlan) EdgeShuffleType(com.alibaba.maxgraph.compiler.logical.edge.EdgeShuffleType) LoggerFactory(org.slf4j.LoggerFactory) LogicalSourceVertex(com.alibaba.maxgraph.compiler.logical.LogicalSourceVertex) LogicalSourceDelegateVertex(com.alibaba.maxgraph.compiler.logical.LogicalSourceDelegateVertex) ExecuteParam(com.alibaba.maxgraph.compiler.executor.ExecuteParam) QueryFlowOuterClass(com.alibaba.maxgraph.QueryFlowOuterClass) LogicalUnaryVertex(com.alibaba.maxgraph.compiler.logical.LogicalUnaryVertex) Lists(com.google.common.collect.Lists) Pair(org.apache.commons.lang3.tuple.Pair) ProcessorChainFunction(com.alibaba.maxgraph.compiler.logical.chain.ProcessorChainFunction) ProcessorSourceFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorSourceFunction) Map(java.util.Map) LogicalVertex(com.alibaba.maxgraph.compiler.logical.LogicalVertex) LogicalChainSourceVertex(com.alibaba.maxgraph.compiler.logical.chain.LogicalChainSourceVertex) TreeConstants(com.alibaba.maxgraph.compiler.tree.TreeConstants) TextFormat(com.google.protobuf.TextFormat) LogicalEdge(com.alibaba.maxgraph.compiler.logical.LogicalEdge) Logger(org.slf4j.Logger) CompilerUtils(com.alibaba.maxgraph.compiler.utils.CompilerUtils) Message(com.alibaba.maxgraph.Message) ProcessorFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) List(java.util.List) LogicalBinaryVertex(com.alibaba.maxgraph.compiler.logical.LogicalBinaryVertex) LogicalChainUnaryVertex(com.alibaba.maxgraph.compiler.logical.chain.LogicalChainUnaryVertex) ProcessorRepeatFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorRepeatFunction) LogicalSourceVertex(com.alibaba.maxgraph.compiler.logical.LogicalSourceVertex) LogicalUnaryVertex(com.alibaba.maxgraph.compiler.logical.LogicalUnaryVertex) ProcessorFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction) LogicalEdge(com.alibaba.maxgraph.compiler.logical.LogicalEdge) LogicalSourceDelegateVertex(com.alibaba.maxgraph.compiler.logical.LogicalSourceDelegateVertex) ProcessorRepeatFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorRepeatFunction) LogicalChainUnaryVertex(com.alibaba.maxgraph.compiler.logical.chain.LogicalChainUnaryVertex) LogicalVertex(com.alibaba.maxgraph.compiler.logical.LogicalVertex) LogicalChainSourceVertex(com.alibaba.maxgraph.compiler.logical.chain.LogicalChainSourceVertex) ProcessorLabelValueFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorLabelValueFunction) VertexIdManager(com.alibaba.maxgraph.compiler.logical.VertexIdManager) Pair(org.apache.commons.lang3.tuple.Pair) LogicalBinaryVertex(com.alibaba.maxgraph.compiler.logical.LogicalBinaryVertex) ProcessorSourceFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorSourceFunction) ProcessorChainFunction(com.alibaba.maxgraph.compiler.logical.chain.ProcessorChainFunction)

Example 9 with LogicalQueryPlan

use of com.alibaba.maxgraph.compiler.logical.LogicalQueryPlan in project GraphScope by alibaba.

the class AndTreeNode method buildLogicalQueryPlan.

@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
    LogicalSubQueryPlan logicalSubQueryPlan = new LogicalSubQueryPlan(contextManager);
    LogicalVertex delegateSourceVertex = getInputNode().getOutputVertex();
    logicalSubQueryPlan.addLogicalVertex(delegateSourceVertex);
    LogicalVertex filterVertex = delegateSourceVertex;
    for (TreeNode andTreeNode : andTreeNodeList) {
        LogicalQueryPlan logicalQueryPlan = new LogicalQueryPlan(contextManager);
        logicalQueryPlan.addLogicalVertex(filterVertex);
        filterVertex = TreeNodeUtils.buildFilterTreeNode(andTreeNode, contextManager, logicalQueryPlan, filterVertex, schema);
        logicalSubQueryPlan.mergeLogicalQueryPlan(logicalQueryPlan);
    }
    LogicalVertex outputVertex = logicalSubQueryPlan.getOutputVertex();
    addUsedLabelAndRequirement(outputVertex, contextManager.getTreeNodeLabelManager());
    setFinishVertex(outputVertex, contextManager.getTreeNodeLabelManager());
    return logicalSubQueryPlan;
}
Also used : LogicalVertex(com.alibaba.maxgraph.compiler.logical.LogicalVertex) LogicalQueryPlan(com.alibaba.maxgraph.compiler.logical.LogicalQueryPlan) LogicalSubQueryPlan(com.alibaba.maxgraph.compiler.logical.LogicalSubQueryPlan)

Example 10 with LogicalQueryPlan

use of com.alibaba.maxgraph.compiler.logical.LogicalQueryPlan in project GraphScope by alibaba.

the class BranchTreeNode method buildLogicalQueryPlan.

@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
    LogicalSubQueryPlan logicalSubQueryPlan = new LogicalSubQueryPlan(contextManager);
    LogicalVertex sourceVertex = getInputNode().getOutputVertex();
    logicalSubQueryPlan.addLogicalVertex(sourceVertex);
    int optionLabelId = 0;
    boolean optionJoinFlag = true;
    UnaryTreeNode branchUnaryNode = UnaryTreeNode.class.cast(branchTreeNode);
    if (branchUnaryNode.getInputNode() instanceof SourceTreeNode) {
        if (branchUnaryNode instanceof PropertyNode) {
            String prop = PropertyNode.class.cast(branchUnaryNode).getPropKeyList().iterator().next();
            optionLabelId = SchemaUtils.getPropId(prop, schema);
            optionJoinFlag = false;
        } else if (branchUnaryNode instanceof SelectOneTreeNode) {
            optionLabelId = contextManager.getTreeNodeLabelManager().getLabelIndex(SelectOneTreeNode.class.cast(branchUnaryNode).getSelectLabel());
            optionJoinFlag = false;
        } else if (branchUnaryNode instanceof TokenTreeNode) {
            optionLabelId = contextManager.getTreeNodeLabelManager().getLabelIndex(TokenTreeNode.class.cast(branchUnaryNode).getToken().getAccessor());
            optionJoinFlag = false;
        }
    }
    if (optionJoinFlag) {
        // join the value stream to get value and set it to label
        if (branchUnaryNode instanceof JoinZeroNode) {
            ((JoinZeroNode) branchUnaryNode).disableJoinZero();
        }
        TreeNode currentBranchTreeNode = TreeNodeUtils.buildSingleOutputNode(branchTreeNode, schema);
        LogicalQueryPlan branchValuePlan = TreeNodeUtils.buildSubQueryPlan(currentBranchTreeNode, sourceVertex, contextManager);
        TreeNode branchSourceNode = TreeNodeUtils.getSourceTreeNode(currentBranchTreeNode);
        sourceVertex = branchSourceNode.getOutputVertex();
        LogicalVertex branchValueVertex = branchValuePlan.getOutputVertex();
        logicalSubQueryPlan.mergeLogicalQueryPlan(branchValuePlan);
        String valueLabel = contextManager.getTreeNodeLabelManager().createSysLabelStart("val");
        optionLabelId = contextManager.getTreeNodeLabelManager().getLabelIndex(valueLabel);
        QueryFlowOuterClass.OperatorType joinOperatorType = CompilerUtils.parseJoinOperatorType(branchTreeNode);
        ProcessorFunction joinFunction = new ProcessorFunction(joinOperatorType, Message.Value.newBuilder().setIntValue(optionLabelId));
        LogicalBinaryVertex joinVertex = new LogicalBinaryVertex(contextManager.getVertexIdManager().getId(), joinFunction, false, sourceVertex, branchValueVertex);
        logicalSubQueryPlan.addLogicalVertex(joinVertex);
        logicalSubQueryPlan.addLogicalEdge(sourceVertex, joinVertex, new LogicalEdge());
        logicalSubQueryPlan.addLogicalEdge(branchValueVertex, joinVertex, new LogicalEdge());
    }
    LogicalVertex outputVertex = logicalSubQueryPlan.getOutputVertex();
    if (optionLabelId > 0) {
        ProcessorFunction fillFunction = new ProcessorFunction(QueryFlowOuterClass.OperatorType.PROP_FILL, Message.Value.newBuilder().addIntValueList(optionLabelId));
        LogicalVertex fillPropVertex = new LogicalUnaryVertex(contextManager.getVertexIdManager().getId(), fillFunction, true, outputVertex);
        logicalSubQueryPlan.addLogicalVertex(fillPropVertex);
        logicalSubQueryPlan.addLogicalEdge(outputVertex, fillPropVertex, outputVertex.isPropLocalFlag() ? new LogicalEdge(EdgeShuffleType.FORWARD) : new LogicalEdge());
        outputVertex = fillPropVertex;
    }
    Map<Object, Message.LogicalCompare> pickCompareList = Maps.newHashMap();
    List<Message.LogicalCompare> noneCompareList = Lists.newArrayList();
    for (Map.Entry<Object, List<TreeNode>> pickEntry : optionPickList.entrySet()) {
        Object pick = pickEntry.getKey();
        Object pickValue = pick;
        if (optionLabelId == TreeConstants.LABEL_INDEX) {
            final String pickString = pick.toString();
            pickValue = schema.getElement(pickString).getLabelId();
        } else if (optionLabelId == TreeConstants.ID_INDEX) {
            pickValue = Long.parseLong(pick.toString());
        }
        Message.VariantType variantType = CompilerUtils.parseVariantType(pickValue.getClass(), pickValue);
        Message.Value.Builder valueBuilder = Message.Value.newBuilder().setValueType(variantType);
        switch(variantType) {
            case VT_INT:
                {
                    if (pickValue instanceof Integer) {
                        valueBuilder.setIntValue((Integer) pickValue);
                    } else {
                        Class<?> pickTokenKeyClazz = BranchStep.class.getDeclaredClasses()[0];
                        Number number = ReflectionUtils.getFieldValue(pickTokenKeyClazz, pickValue, "number");
                        valueBuilder.setIntValue((int) number);
                    }
                    break;
                }
            case VT_LONG:
                {
                    if (pickValue instanceof Long) {
                        valueBuilder.setLongValue((Long) pickValue);
                    } else {
                        Class<?> pickTokenKeyClazz = BranchStep.class.getDeclaredClasses()[0];
                        Number number = ReflectionUtils.getFieldValue(pickTokenKeyClazz, pickValue, "number");
                        valueBuilder.setLongValue((long) number);
                    }
                    break;
                }
            case VT_STRING:
                {
                    valueBuilder.setStrValue((String) pickValue).build();
                    break;
                }
            default:
                {
                    throw new UnsupportedOperationException(pickValue + " for branch option operator");
                }
        }
        Message.Value value = valueBuilder.build();
        pickCompareList.put(pick, Message.LogicalCompare.newBuilder().setPropId(optionLabelId).setCompare(Message.CompareType.EQ).setValue(value).setType(variantType).build());
        noneCompareList.add(Message.LogicalCompare.newBuilder().setPropId(optionLabelId).setCompare(Message.CompareType.NEQ).setValue(value).setType(variantType).build());
    }
    List<LogicalVertex> unionVertexList = Lists.newArrayList();
    for (Map.Entry<Object, List<TreeNode>> optionEntry : this.optionPickList.entrySet()) {
        List<TreeNode> optionTreeNodeList = optionEntry.getValue();
        Message.LogicalCompare logicalCompare = checkNotNull(pickCompareList.get(optionEntry.getKey()));
        ProcessorFilterFunction filterFunction = new ProcessorFilterFunction(QueryFlowOuterClass.OperatorType.FILTER, createArgumentBuilder().setBoolValue(true));
        filterFunction.getLogicalCompareList().add(logicalCompare);
        LogicalVertex filterVertex = new LogicalUnaryVertex(contextManager.getVertexIdManager().getId(), filterFunction, outputVertex);
        logicalSubQueryPlan.addLogicalVertex(filterVertex);
        logicalSubQueryPlan.addLogicalEdge(outputVertex, filterVertex, LogicalEdge.forwardEdge());
        for (TreeNode treeNode : optionTreeNodeList) {
            LogicalQueryPlan subQueryPlan = TreeNodeUtils.buildQueryPlanWithSource(treeNode, contextManager.getTreeNodeLabelManager(), contextManager, contextManager.getVertexIdManager(), filterVertex);
            unionVertexList.add(subQueryPlan.getOutputVertex());
            logicalSubQueryPlan.mergeLogicalQueryPlan(subQueryPlan);
        }
    }
    if (null != noneTreeNode) {
        ProcessorFilterFunction filterFunction = new ProcessorFilterFunction(QueryFlowOuterClass.OperatorType.FILTER, createArgumentBuilder().setBoolValue(true));
        filterFunction.getLogicalCompareList().addAll(noneCompareList);
        LogicalVertex filterVertex = new LogicalUnaryVertex(contextManager.getVertexIdManager().getId(), filterFunction, outputVertex);
        logicalSubQueryPlan.addLogicalVertex(filterVertex);
        logicalSubQueryPlan.addLogicalEdge(outputVertex, filterVertex, LogicalEdge.forwardEdge());
        LogicalQueryPlan subQueryPlan = TreeNodeUtils.buildQueryPlanWithSource(noneTreeNode, contextManager.getTreeNodeLabelManager(), contextManager, contextManager.getVertexIdManager(), filterVertex);
        unionVertexList.add(subQueryPlan.getOutputVertex());
        logicalSubQueryPlan.mergeLogicalQueryPlan(subQueryPlan);
    }
    if (null != anyTreeNode) {
        LogicalQueryPlan subQueryPlan = TreeNodeUtils.buildQueryPlanWithSource(anyTreeNode, contextManager.getTreeNodeLabelManager(), contextManager, contextManager.getVertexIdManager(), outputVertex);
        unionVertexList.add(subQueryPlan.getOutputVertex());
        logicalSubQueryPlan.mergeLogicalQueryPlan(subQueryPlan);
    }
    LogicalVertex currentUnionVertex = unionVertexList.remove(0);
    for (LogicalVertex logicalVertex : unionVertexList) {
        LogicalVertex unionVertex = new LogicalBinaryVertex(contextManager.getVertexIdManager().getId(), new ProcessorFunction(QueryFlowOuterClass.OperatorType.UNION), false, currentUnionVertex, logicalVertex);
        logicalSubQueryPlan.addLogicalVertex(unionVertex);
        logicalSubQueryPlan.addLogicalEdge(currentUnionVertex, unionVertex, LogicalEdge.forwardEdge());
        logicalSubQueryPlan.addLogicalEdge(logicalVertex, unionVertex, LogicalEdge.forwardEdge());
        currentUnionVertex = unionVertex;
    }
    addUsedLabelAndRequirement(currentUnionVertex, contextManager.getTreeNodeLabelManager());
    setFinishVertex(currentUnionVertex, contextManager.getTreeNodeLabelManager());
    return logicalSubQueryPlan;
}
Also used : LogicalUnaryVertex(com.alibaba.maxgraph.compiler.logical.LogicalUnaryVertex) ProcessorFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction) LogicalEdge(com.alibaba.maxgraph.compiler.logical.LogicalEdge) Message(com.alibaba.maxgraph.Message) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) ProcessorFilterFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorFilterFunction) LogicalVertex(com.alibaba.maxgraph.compiler.logical.LogicalVertex) PropertyNode(com.alibaba.maxgraph.compiler.tree.addition.PropertyNode) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) LogicalQueryPlan(com.alibaba.maxgraph.compiler.logical.LogicalQueryPlan) List(java.util.List) LogicalSubQueryPlan(com.alibaba.maxgraph.compiler.logical.LogicalSubQueryPlan) JoinZeroNode(com.alibaba.maxgraph.compiler.tree.addition.JoinZeroNode) QueryFlowOuterClass(com.alibaba.maxgraph.QueryFlowOuterClass) LogicalBinaryVertex(com.alibaba.maxgraph.compiler.logical.LogicalBinaryVertex) QueryFlowOuterClass(com.alibaba.maxgraph.QueryFlowOuterClass) Map(java.util.Map)

Aggregations

LogicalQueryPlan (com.alibaba.maxgraph.compiler.logical.LogicalQueryPlan)11 LogicalVertex (com.alibaba.maxgraph.compiler.logical.LogicalVertex)9 ProcessorFunction (com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction)7 LogicalEdge (com.alibaba.maxgraph.compiler.logical.LogicalEdge)6 LogicalSubQueryPlan (com.alibaba.maxgraph.compiler.logical.LogicalSubQueryPlan)6 LogicalBinaryVertex (com.alibaba.maxgraph.compiler.logical.LogicalBinaryVertex)5 LogicalUnaryVertex (com.alibaba.maxgraph.compiler.logical.LogicalUnaryVertex)5 QueryFlowOuterClass (com.alibaba.maxgraph.QueryFlowOuterClass)4 Message (com.alibaba.maxgraph.Message)3 SourceDelegateNode (com.alibaba.maxgraph.compiler.tree.source.SourceDelegateNode)3 List (java.util.List)3 VertexIdManager (com.alibaba.maxgraph.compiler.logical.VertexIdManager)2 EdgeShuffleType (com.alibaba.maxgraph.compiler.logical.edge.EdgeShuffleType)2 ProcessorSourceFunction (com.alibaba.maxgraph.compiler.logical.function.ProcessorSourceFunction)2 TreeBuilder (com.alibaba.maxgraph.compiler.tree.TreeBuilder)2 TreeManager (com.alibaba.maxgraph.compiler.tree.TreeManager)2 SourceTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode)2 VertexValueType (com.alibaba.maxgraph.compiler.tree.value.VertexValueType)2 CompilerUtils (com.alibaba.maxgraph.compiler.utils.CompilerUtils)2 Map (java.util.Map)2