Search in sources :

Example 1 with TreeNodeLabelManager

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

the class LogicalPlanBuilder method processCostModel.

private void processCostModel(TreeNode currentNode, LogicalQueryPlan queryPlan, ContextManager contextManager, GraphSchema schema, Set<Integer> labelIdList) {
    CostModelManager costModelManager = contextManager.getCostModelManager();
    TreeNodeLabelManager treeNodeLabelManager = contextManager.getTreeNodeLabelManager();
    VertexIdManager vertexIdManager = contextManager.getVertexIdManager();
    if (costModelManager.hasBestPath()) {
        CostMappingManager costMappingManager = costModelManager.getCostMappingManager();
        RowFieldManager rowFieldManager = costModelManager.getPathFieldManager();
        if (null == rowFieldManager) {
            return;
        }
        RowField rowField = rowFieldManager.getRowField();
        Set<String> birthFieldList = rowFieldManager.getBirthFieldList();
        Set<String> fieldList = rowField.getFieldList();
        for (String field : fieldList) {
            LogicalVertex outputVertex = queryPlan.getOutputVertex();
            if (birthFieldList.contains(field)) {
                buildLabelValuePlan(queryPlan, contextManager, schema, outputVertex, treeNodeLabelManager, vertexIdManager, costMappingManager, field, currentNode);
            } else {
                RowFieldManager parentFieldManager = rowFieldManager.getParent();
                if (!parentFieldManager.getRowField().getFieldList().contains(field)) {
                    buildLabelValuePlan(queryPlan, contextManager, schema, outputVertex, treeNodeLabelManager, vertexIdManager, costMappingManager, field, currentNode);
                }
            }
        }
        currentNode.setFinishVertex(queryPlan.getOutputVertex(), null);
    }
    List<LogicalVertex> logicalVertexList = queryPlan.getLogicalVertexList();
    for (LogicalVertex vertex : logicalVertexList) {
        if (vertex.getProcessorFunction() != null && vertex.getProcessorFunction().getOperatorType() == QueryFlowOuterClass.OperatorType.LABEL_VALUE) {
            if (vertex.getProcessorFunction().getArgumentBuilder().getIntValue() != 0) {
                labelIdList.add(vertex.getProcessorFunction().getArgumentBuilder().getIntValue());
            }
        } else {
            vertex.getBeforeRequirementList().removeIf(vv -> {
                if (vv.getReqType() == QueryFlowOuterClass.RequirementType.LABEL_START) {
                    List<Integer> intValueList = Lists.newArrayList(vv.getReqArgument().getIntValueListList());
                    intValueList.removeIf(labelIdList::contains);
                    if (intValueList.isEmpty()) {
                        return true;
                    }
                    vv.getReqArgumentBuilder().clearIntValueList().addAllIntValueList(intValueList);
                }
                return false;
            });
        }
    }
}
Also used : TreeNodeLabelManager(com.alibaba.maxgraph.compiler.tree.TreeNodeLabelManager) CostMappingManager(com.alibaba.maxgraph.compiler.cost.CostMappingManager) RowFieldManager(com.alibaba.maxgraph.compiler.cost.RowFieldManager) CostModelManager(com.alibaba.maxgraph.compiler.cost.CostModelManager) RowField(com.alibaba.maxgraph.compiler.cost.RowField)

Example 2 with TreeNodeLabelManager

use of com.alibaba.maxgraph.compiler.tree.TreeNodeLabelManager 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;
}
Also used : TreeNodeLabelManager(com.alibaba.maxgraph.compiler.tree.TreeNodeLabelManager) LogicalUnaryVertex(com.alibaba.maxgraph.compiler.logical.LogicalUnaryVertex) ProcessorFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction) Message(com.alibaba.maxgraph.Message) LogicalEdge(com.alibaba.maxgraph.compiler.logical.LogicalEdge) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) UnaryTreeNode(com.alibaba.maxgraph.compiler.tree.UnaryTreeNode) ProcessorFilterFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorFilterFunction) 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) PropertyNode(com.alibaba.maxgraph.compiler.tree.addition.PropertyNode) SelectOneTreeNode(com.alibaba.maxgraph.compiler.tree.SelectOneTreeNode) VertexIdManager(com.alibaba.maxgraph.compiler.logical.VertexIdManager) LogicalSubQueryPlan(com.alibaba.maxgraph.compiler.logical.LogicalSubQueryPlan)

Example 3 with TreeNodeLabelManager

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

the class SourceVertexTreeNode method buildLogicalQueryPlan.

@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
    LogicalSubQueryPlan logicalQueryPlan = new LogicalSubQueryPlan(contextManager);
    logicalQueryPlan.setDelegateSourceFlag(false);
    Message.Value.Builder argumentBuilder = Message.Value.newBuilder();
    processLabelArgument(argumentBuilder, true);
    processIdArgument(argumentBuilder, true);
    Map<String, List<Integer>> primaryKeyLabelIdList = Maps.newHashMap();
    for (GraphVertex vertexType : this.schema.getVertexList()) {
        List<GraphProperty> primaryKeyList = vertexType.getPrimaryKeyList();
        if (null != primaryKeyList && primaryKeyList.size() == 1) {
            String propertyName = primaryKeyList.get(0).getName();
            List<Integer> labelIdList = primaryKeyLabelIdList.computeIfAbsent(propertyName, k -> Lists.newArrayList());
            labelIdList.add(vertexType.getLabelId());
        }
    }
    QueryFlowOuterClass.VertexPrimaryKeyListProto.Builder primaryKeyBuilder = QueryFlowOuterClass.VertexPrimaryKeyListProto.newBuilder();
    for (HasContainer container : this.hasContainerList) {
        String key = container.getKey();
        List<Integer> labelIdList = primaryKeyLabelIdList.get(key);
        if (null != labelIdList) {
            for (Integer labelId : labelIdList) {
                if (container.getBiPredicate() instanceof Compare && container.getBiPredicate() == Compare.eq) {
                    primaryKeyBuilder.addPrimaryKeys(QueryFlowOuterClass.VertexPrimaryKeyProto.newBuilder().setLabelId(labelId).setPrimaryKeyValue(container.getValue().toString()));
                } else if (container.getBiPredicate() instanceof Contains && container.getBiPredicate() == Contains.within) {
                    for (Object val : (Collection<Object>) container.getValue()) {
                        primaryKeyBuilder.addPrimaryKeys(QueryFlowOuterClass.VertexPrimaryKeyProto.newBuilder().setLabelId(labelId).setPrimaryKeyValue(val.toString()));
                    }
                }
            }
        }
    }
    argumentBuilder.setPayload(primaryKeyBuilder.build().toByteString()).setBoolFlag(isPartitionIdFlag());
    ProcessorSourceFunction processorSourceFunction = new ProcessorSourceFunction(getCountOperator(QueryFlowOuterClass.OperatorType.V), argumentBuilder, rangeLimit);
    VertexIdManager vertexIdManager = contextManager.getVertexIdManager();
    TreeNodeLabelManager treeNodeLabelManager = contextManager.getTreeNodeLabelManager();
    LogicalSourceVertex logicalSourceVertex = new LogicalSourceVertex(vertexIdManager.getId(), processorSourceFunction);
    logicalSourceVertex.getBeforeRequirementList().addAll(buildBeforeRequirementList(treeNodeLabelManager));
    logicalSourceVertex.getAfterRequirementList().addAll(buildAfterRequirementList(treeNodeLabelManager));
    getUsedLabelList().forEach(v -> processorSourceFunction.getUsedLabelList().add(treeNodeLabelManager.getLabelIndex(v)));
    logicalQueryPlan.addLogicalVertex(logicalSourceVertex);
    setFinishVertex(logicalQueryPlan.getOutputVertex(), treeNodeLabelManager);
    return logicalQueryPlan;
}
Also used : LogicalSourceVertex(com.alibaba.maxgraph.compiler.logical.LogicalSourceVertex) TreeNodeLabelManager(com.alibaba.maxgraph.compiler.tree.TreeNodeLabelManager) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) ProcessorSourceFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorSourceFunction) GraphProperty(com.alibaba.maxgraph.compiler.api.schema.GraphProperty) GraphVertex(com.alibaba.maxgraph.compiler.api.schema.GraphVertex) Contains(org.apache.tinkerpop.gremlin.process.traversal.Contains) List(java.util.List) Compare(org.apache.tinkerpop.gremlin.process.traversal.Compare) VertexIdManager(com.alibaba.maxgraph.compiler.logical.VertexIdManager) LogicalSubQueryPlan(com.alibaba.maxgraph.compiler.logical.LogicalSubQueryPlan)

Example 4 with TreeNodeLabelManager

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

the class LogicalPlanBuilder method build.

/**
 * Build logical plan from tree manager
 *
 * @param treeManager The given tree manager
 * @return The result logical plan
 */
public LogicalQueryPlan build(TreeManager treeManager) {
    CostModelManager costModelManager = treeManager.optimizeCostModel();
    TreeNodeLabelManager labelManager = treeManager.getLabelManager();
    VertexIdManager vertexIdManager = VertexIdManager.createVertexIdManager();
    ContextManager contextManager = new ContextManager(costModelManager, treeManager.getQueryConfig(), vertexIdManager, labelManager);
    TreeNode leafNode = treeManager.getTreeLeaf();
    return buildPlan(leafNode, contextManager, treeManager.getSchema());
}
Also used : TreeNodeLabelManager(com.alibaba.maxgraph.compiler.tree.TreeNodeLabelManager) BaseTreeNode(com.alibaba.maxgraph.compiler.tree.BaseTreeNode) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) ContextManager(com.alibaba.maxgraph.compiler.optimizer.ContextManager) CostModelManager(com.alibaba.maxgraph.compiler.cost.CostModelManager)

Example 5 with TreeNodeLabelManager

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

the class LogicalPlanBuilder method buildLabelValuePlan.

private void buildLabelValuePlan(LogicalQueryPlan queryPlan, ContextManager contextManager, GraphSchema schema, LogicalVertex outputVertex, TreeNodeLabelManager treeNodeLabelManager, VertexIdManager vertexIdManager, CostMappingManager costMappingManager, String field, TreeNode currNode) {
    // Create field value here
    String parentField = costMappingManager.getValueParent(field);
    if (!StringUtils.isEmpty(parentField)) {
        BaseTreeNode nextNode = (BaseTreeNode) currNode.getOutputNode();
        nextNode.removeBeforeLabel(parentField);
        // If the field is value field, process it and remove the label requirement in vertex
        int labelIndex = treeNodeLabelManager.getLabelIndex(parentField);
        TreeNode fieldValueTreeNode = costMappingManager.getComputeTreeByValue(parentField);
        if (!(fieldValueTreeNode instanceof SourceTreeNode)) {
            for (QueryFlowOuterClass.RequirementValue.Builder reqValue : outputVertex.getAfterRequirementList()) {
                if (reqValue.getReqType() == QueryFlowOuterClass.RequirementType.LABEL_START) {
                    List<Integer> labelIndexList = reqValue.getReqArgumentBuilder().getIntValueListList().stream().filter(v -> v != labelIndex).collect(Collectors.toList());
                    reqValue.getReqArgumentBuilder().clearIntValueList().addAllIntValueList(labelIndexList);
                }
            }
            TreeNode currentFilterTreeNode = TreeNodeUtils.buildSingleOutputNode(fieldValueTreeNode, schema);
            // build filter plan, and use join direct filter vertex to filter left stream
            LogicalSubQueryPlan fieldValuePlan = TreeNodeUtils.buildSubQueryPlan(currentFilterTreeNode, outputVertex, contextManager);
            List<LogicalVertex> fieldValueVertexList = fieldValuePlan.getLogicalVertexList();
            if (fieldValueVertexList.size() == 2) {
                LogicalVertex fieldValueVertex = fieldValueVertexList.get(1);
                ProcessorLabelValueFunction labelValueFunction = new ProcessorLabelValueFunction(labelIndex, fieldValueVertex);
                LogicalVertex labelValueVertex = new LogicalUnaryVertex(vertexIdManager.getId(), labelValueFunction, outputVertex);
                queryPlan.addLogicalVertex(labelValueVertex);
                queryPlan.addLogicalEdge(outputVertex, labelValueVertex, LogicalEdge.shuffleByKey(labelIndex));
            } else {
                LogicalVertex fieldValueVertex = fieldValuePlan.getOutputVertex();
                fieldValueVertex.getAfterRequirementList().add(QueryFlowOuterClass.RequirementValue.newBuilder().setReqType(QueryFlowOuterClass.RequirementType.LABEL_START).setReqArgument(Message.Value.newBuilder().addIntValueList(labelIndex)));
                queryPlan.mergeLogicalQueryPlan(fieldValuePlan);
                LogicalVertex outputKeyVertex = new LogicalUnaryVertex(vertexIdManager.getId(), new ProcessorFunction(QueryFlowOuterClass.OperatorType.KEY_MESSAGE), fieldValueVertex);
                queryPlan.addLogicalVertex(outputKeyVertex);
                queryPlan.addLogicalEdge(fieldValueVertex, outputKeyVertex, LogicalEdge.forwardEdge());
            }
        }
    }
}
Also used : BaseTreeNode(com.alibaba.maxgraph.compiler.tree.BaseTreeNode) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) ProcessorLabelValueFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorLabelValueFunction) CompilerUtils(com.alibaba.maxgraph.compiler.utils.CompilerUtils) Set(java.util.Set) ContextManager(com.alibaba.maxgraph.compiler.optimizer.ContextManager) TreeNodeUtils(com.alibaba.maxgraph.compiler.utils.TreeNodeUtils) CostMappingManager(com.alibaba.maxgraph.compiler.cost.CostMappingManager) Message(com.alibaba.maxgraph.Message) ProcessorFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) CostModelManager(com.alibaba.maxgraph.compiler.cost.CostModelManager) Sets(com.google.common.collect.Sets) RowField(com.alibaba.maxgraph.compiler.cost.RowField) QueryFlowOuterClass(com.alibaba.maxgraph.QueryFlowOuterClass) TreeManager(com.alibaba.maxgraph.compiler.tree.TreeManager) List(java.util.List) Lists(com.google.common.collect.Lists) RowFieldManager(com.alibaba.maxgraph.compiler.cost.RowFieldManager) TreeNodeLabelManager(com.alibaba.maxgraph.compiler.tree.TreeNodeLabelManager) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) ProcessorFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) BaseTreeNode(com.alibaba.maxgraph.compiler.tree.BaseTreeNode) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) BaseTreeNode(com.alibaba.maxgraph.compiler.tree.BaseTreeNode) ProcessorLabelValueFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorLabelValueFunction)

Aggregations

TreeNodeLabelManager (com.alibaba.maxgraph.compiler.tree.TreeNodeLabelManager)6 TreeNode (com.alibaba.maxgraph.compiler.tree.TreeNode)4 SourceTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode)4 CostModelManager (com.alibaba.maxgraph.compiler.cost.CostModelManager)3 LogicalSubQueryPlan (com.alibaba.maxgraph.compiler.logical.LogicalSubQueryPlan)3 VertexIdManager (com.alibaba.maxgraph.compiler.logical.VertexIdManager)3 ProcessorFunction (com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction)3 Message (com.alibaba.maxgraph.Message)2 CostMappingManager (com.alibaba.maxgraph.compiler.cost.CostMappingManager)2 RowField (com.alibaba.maxgraph.compiler.cost.RowField)2 RowFieldManager (com.alibaba.maxgraph.compiler.cost.RowFieldManager)2 LogicalEdge (com.alibaba.maxgraph.compiler.logical.LogicalEdge)2 LogicalUnaryVertex (com.alibaba.maxgraph.compiler.logical.LogicalUnaryVertex)2 LogicalVertex (com.alibaba.maxgraph.compiler.logical.LogicalVertex)2 ContextManager (com.alibaba.maxgraph.compiler.optimizer.ContextManager)2 BaseTreeNode (com.alibaba.maxgraph.compiler.tree.BaseTreeNode)2 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